English 中文(简体)
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, name="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 )




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签