Exporting selected nodes only without their parents.

1,453 views
Skip to first unread message

Macbeth R.

unread,
Oct 19, 2015, 6:13:15 AM10/19/15
to Python Programming for Autodesk Maya
Is there a way to export selected nodes only without a top parent group above them??

For example if I have one top group/transform named ALL, then inside some other groups/transforms named FW and BG, and I only want to export BG and FG with all their children without getting the top group ALL in the resulting scene.

Greetings.

Marcus Ottosson

unread,
Oct 19, 2015, 7:28:39 AM10/19/15
to python_in...@googlegroups.com

You can if you’re exporting to Alembic, it’s got a flag specifying which part you’d like to be root, but likely not other formats.

You could also unparent things prior to export, maybe with a context manager like this one.

import contextlib
from maya import cmds

@contextlib.contextmanager
def unparent(root):
    """Temporarily unparent `root`"""
    parent = cmds.listRelatives(root, parent=True)
    if parent:
        cmds.parent(root, world=True)
        yield
        cmds.parent(root, parent)

You could use it like this.

with unparent(cmds.ls(sl=True)[0]):
    cmds.file("c:/my/path.ma", exportSelected=True, type="mayaAscii")

And make it a meal-deal like this.

import contextlib
from maya import cmds

@contextlib.contextmanager
def unparent(root):
    """Temporarily unparent `root`"""
    parent = cmds.listRelatives(root, parent=True)
    if parent:
        cmds.parent(root, world=True)
        yield
        cmds.parent(root, parent)

# Retrieve an output path
filters = "Maya Files (*.ma *.mb);;Maya ASCII (*.ma);;Maya Binary (*.mb)"
path = cmds.fileDialog2(fileFilter=filters,
                        dialogStyle=2,  # Use Maya-style file dialog
                        fileMode=0)     # Any file, whether it exists or not

# Perform export, without parenthood
if not path:
    cmds.warning("Cancelled")
else:
    selection = cmds.ls(sl=True)[0]
    file_type = {"ma": "mayaAscii", "mb": "mayaBinary"}[path[0].rsplit(".")[-1]]

    with unparent(selection):
        cmds.file(path[0], exportSelected=True, type=file_type)

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/48a6f735-81a8-4b99-8eae-0c0d3ba5ba54%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Reply all
Reply to author
Forward
0 new messages