comparing two lists, to do something?

224 views
Skip to first unread message

Jeremy Beauchamp

unread,
Oct 9, 2017, 1:43:35 PM10/9/17
to Python Programming for Autodesk Maya
I'm still fairly new to Maya and python, so i have a few questions. 

I have two lists:
1. Materials
2. Locators
I want to compare each list and if the locator has the same name as the material then:
getAttr transforms from the locator and setAttr color to the material.


If I define the name of the material and locator as 'someMaterial', it works. 
How do I make it so I don't have to define the names of the material or locator as that can be completely random?
Also, the locators will always have the same name as the material by default, unless there is a better way to compare?

-------------------------------------------

locator = cmds.ls(type='locator')
someMaterialLoc=[]
for x in locator:
if x == 'someMaterialShape1':
someMaterialLoc.append(x)
print someMaterialLoc

allShaders = cmds.ls(type='VRayMtl')
someMaterial =[]
for x in allShaders:
if x =='someMaterial':
someMaterial.append(x)
print someMaterial

if str(someMaterial[0]) == someMaterialLoc[0][0:12]:
red = cmds.getAttr(someMaterialLoc[0], '.translateX')
green = cmds.getAttr(someMaterialLoc[0], '.translateY')
blue = cmds.getAttr(someMaterialLoc[0], '.translateZ')
cmds.setAttr ((someMaterial[0] + '.color'), red, green, blue, type = 'double3' )

-------------------------------------------

Any recommendations would be greatly appreciated.
Thanks,
Jeremy

Andres Weber

unread,
Oct 9, 2017, 3:47:57 PM10/9/17
to Python Programming for Autodesk Maya
To do matching like this at a more basic level usually people do some sort of token within the name of each object that represents the object type.  For example:

target_locator
target_shader
target2_locator
target2_shader

something like that.  This way you can do really quick string replacements to try to query the relationship of the names of two objects.  Otherwise you'd probably need to query some sort of relationship between the two, use sets or a myriad of ways of defining relationships between objects in Maya.  You're also running into the issue of matching the shape node because you can't actually name two nodes the exact same string.

So for example (if you now name your nodes in some way like above) you could refactor your code to something like this:

Message has been deleted

Jeremy Beauchamp

unread,
Oct 9, 2017, 5:40:26 PM10/9/17
to Python Programming for Autodesk Maya
Thank you Andres!
This is exactly what I was looking for:)
I changed a few things:

1. some commands as cmds, not mc.
2. added listRelatives as I was getting the shape node in the list of locators, which gave me the incorrect vray_shader_target.
3. defined locators variable. 
4. matches.append(locator, shader) errored out, stating it needed one argument, but two given. It was missing [].

The changes are below:

----------------------------------------------
import maya.cmds as mc

vray_shaders = mc.ls(type='VRayMtl') # We store the materials here and not within the for loop because it would be wasted computations for every locator.
locators = mc.listRelatives(mc.ls(type='locator'), p=True)

matches = []
for locator in locators:
vray_shader_target = locator.replace('locator', 'shader')
if vray_shader_target in vray_shaders:
# You can shorten your query code if you use list unpacking
position = mc.getAttr(locator + '.translate')[0] # Here we obtain a list of positions, and retrieve the first entry (translate)
mc.setAttr(vray_shader_target + '.color', *position, type='double3') # Using * we can unpack the list of length 3 unto all three rgb entry args
matches.append([locator,vray_shader_target])
# We stored the matches in the format list(list(locator, shader)) so now you can run through that list like this if you wanted to use it later:
for locator, vray_shader_target in matches:
print(locator, vray_shader_target)

Andres Weber

unread,
Oct 11, 2017, 11:05:35 AM10/11/17
to Python Programming for Autodesk Maya
That's what I get for not actually running my code!  Glad to hear it worked out and you fixed the bugs.
Reply all
Reply to author
Forward
0 new messages