简体   繁体   中英

Python & Maya : Namespaces

I've been searching on google how to deal with namespaces in Maya and all the results are for how to remove them. I don't want to remove them.

I'm referencing my character into the scene, I need to run a script on a specific locator, but they all have namespaces due to being referenced in.

For example: shldr = cmds.xform('L_shldrFK_match_LOC', ws = True, t = True, q = True)

That will get the translation value of the locator in worldspace, which is what I want. But it wont be able to find the locator due to the namespace.

How do I make it ignore namespaces in the code, but retain them inside maya?

The other thing that would be nice is for the code to recognize if there are no namespaces (eg the scene wasn't referenced in).

Another way to go about this would be to skip the dependency of string naming and adopt retrieving nodes via UUID instead. See cmds.ls in conjunction with the -uuid flag found here .

Namespace are really just string name prefixes appended onto an object's name. As long as the namespace exists, you can rename an object to add/remove it from a namespace. For instance:

import maya.cmds as cmds

loc = cmds.spaceLocator()
cmds.ls(loc)[0]  # Result: u'locator1' # 

cmds.namespace(add='foobar')  # Result: u'foobar' # 
loc = cmds.rename(loc, ':foobar:{}'.format(loc))  # Result: u'foobar:my_locator' #

cmds.rename(loc, ':my_locator')  # Result: u'my_locator' # 

The idea behind UUID is that nodes get tagged with a special string attribute which is unique to the node and does not change when the node is renamed. For example, notice the name changes while the UUID remains the same:

import maya.cmds as cmds

loc = cmds.spaceLocator()
cmds.ls(loc, uuid=True)[0]  # u'60446AC7-4398-E3CB-4C27-6BA417626E41' # 
cmds.ls(loc)[0]  # Result: u'locator1' # 


loc = cmds.rename(loc, 'my_locator')
cmds.ls(loc, uuid=True)[0]  # Result: u'60446AC7-4398-E3CB-4C27-6BA417626E41' #
cmds.ls(loc)[0]  # Result: u'my_locator' # 

Armed with this info, we now know that a node will always have the same, unchanging UUID for the entirety of its lifespan, regardless of what namespace it is moved (via naming) to/from.

Putting this all together, here is how we use the ls command in conjunction with uuid :

# Query the UUID of our locator
cmds.ls(loc, uuid=True)[0]  # Result: u'60446AC7-4398-E3CB-4C27-6BA417626E41' #

# Retrieve the locator name string via UUID
cmds.ls(u'60446AC7-4398-E3CB-4C27-6BA417626E41')[0]  # Result: u'my_locator' # 

You can use cmds.namespaceInfo

This command can list all available namespaces and its containting objects.

Read more about here: namespaceInfo

when im coding something like that i generally use an asterix for where the namespace is like so.

shldr = cmds.xform('*:L_shldrFK_match_LOC', ws = True, t = True, q = True)

it depends if your going to have other things in your scene under other namespace with the same name though because then it won't work

we need a list with all namespaces to find the correct one:

namespaces = cmds.namespaceInfo(lon=True)
current_namespace = cmds.namespaceInfo(currentNamespace=True)
references = cmds.ls(type="reference")

you can work with this information but ignore the namespace is not really good, because the main function of referencing is to have a environment/character/prop asset multiply times with the last update in you scene...so

shldr = cmds.xform('*:L_shldrFK_match_LOC', ws = True, t = True, q = True)

will work only if you have only one character in you scene.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM