开发者

Creating a simple hierarchy structure in Maya using mel/python

So I want to create a very simple structure out of group and locator nodes in Maya which will then be exported for use in my game level.

e.g.

Group_Root
  group_parent
    - group1
      - locator1
    - group2
      - locator2
    - group3

There is only one Group_Root in the file, there are many group_parents ( each uniquely named ) However all group_parent's have the same three sub-group names( "group1", "group2", "group3" ) and all group1 have a locator called locator1

What I have so far is:

group_parent = c.group( em=True, na开发者_如何学运维me="group_parent", parent="Group_Root")
modes =  ["group1", "group2", "group3"]
for mode in modes:
    mode_group = c.group( em=True, n=mode, parent=group_parent )
    if mode == "group1":
            s = c.spaceLocator(name="locator1")
            c.parent( mode_group ) 
    elif mode == "group3":
            s = c.spaceLocator(name="locator2")
            c.parent( mode_group )

However I get this error at "c.parent(mode_group)"

# Error: Object group1 is invalid

Presumably because there are more than one node called "group1" so it doesn't know which one to parent.

Any idea how I do this with full paths? e.g. "Group_Root|group_parent|group1"


Have you seen VFX Overflow? It's Q&A for visual effects, so I'd expect a number of the watchers to be quite familiar with Maya/MEL and Python. That said, it is fairly new so the user base is still small...


Names can be a bit annoying with MEL. In general, it's good practice to never trust a name to be what you're specifying.

This is a good example of how *not to do things:

group -n myGroup1 circle1 sphere1;

..because that is in no way guaranteed to result in something named "group1". The better way to do it is to run your command and capture the result in a string variable, such as:

string $result = `group -n myGroup circle1 sphere1`;

Then, use $result to refer to the resulting group. That will still work, even if the group ended up being called 'myGroup23'.

I'm not sure how the above looks in Python, as I'm mainly familiar with straight MEL, but the same principles should apply.

Another thing to look at is the namespace functionality (namespace and namespaceInfo), which could be used to define a new namespace for the unique, top-leve group at hand.

Hope that helps


I'm guessing that it being over two years, you've figured this one out by now.. but for posterity, there were two issues - firstly, you were spot on with the need for absolute paths, but there was also a slight bug in the way you were applying the maya.cmds.parent() call. I've just done some light rewriting to illustrate - mainly you could use the fact that when you create things they become selected by default, and maya.cmds.ls() is smart enough to return you what you need.. Ergo:

c.group( em=True, name="group_parent", parent="Group_Root")
group_parent = c.ls(sl=True)[0]

modes =  ["group1" , "group2", "group3"]
for mode in modes:
    c.group( em=True, n=mode, parent=group_parent )
    mode_group = c.ls(sl=True)[0]
    if mode == "group1":
            c.spaceLocator(name="locator1")
            s = c.ls(sl=True)[0]
            # maya.cmds.parent() with something selected will actually
            # parent the specified object to the selected object. 
            # You don't want that.


            # We might as well use the explicit syntax to be sure 
            # (parent everything specified to the last item in the list)
            c.parent( s, mode_group ) 
    elif mode == "group3":
            c.spaceLocator(name="locator2")
            s = c.ls(sl=True)[0]
            c.parent( s, mode_group )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜