Skip to content

Instantly share code, notes, and snippets.

@AndresMWeber
Last active October 9, 2017 19:47
Show Gist options
  • Save AndresMWeber/788e203f4500039a12bf38dfc086b904 to your computer and use it in GitHub Desktop.
Save AndresMWeber/788e203f4500039a12bf38dfc086b904 to your computer and use it in GitHub Desktop.
import maya.cmds as mc
"""
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?
"""
vray_shaders = cmds.ls(type='VRayMtl') # We store the materials here and not within the for loop because it would be wasted computations for every locator.
matches = []
for locator in cmds.ls(type='locator'):
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, shader in matches:
print(locator, shader)
import maya.cmds as cmds
# Running through this list by list is going to end up being ineffective. Think about the steps in pseudo code:
# 1. "I want to store all locators"
# 2. "I want to store all vray materials"
# 3. "Now I want to compare and see if each locator has a correspondingly named material"
# 4. "Depending on that we will now set the attributes for the shader from the locator"
# What you ended up doing is saying something more like
# 1. "Store all locators with this specific name"
# 2. "Store all materials with a different specific name"
# 3. "Now set the attributes of only the first match"
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
"""
NOTES:
Below it seems like you're trying to compare the material and locator's first letter but this would've been unncessary anyways since you already
know the names are the same from where you compared above. Also someMaterialLoc[0][0:12] means that you get the first letter and then try
to get a slice until position 12...which will still just be the first letter. Try running and compare the results:
print('asdfjkl;')
print('asdfjkl;'[0])
print('asdfjkl;'[0][0:12])
print('asdfjkl;'[0:12])
"""
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' )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment