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)

On Monday, October 9, 2017 at 12:47:57 PM UTC-7, Andres Weber wrote:
>
> 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:
>
> https://gist.github.com/AndresMWeber/788e203f4500039a12bf38dfc086b904
>
> On Monday, October 9, 2017 at 1:43:35 PM UTC-4, Jeremy Beauchamp wrote:
>>
>> 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
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/6c104d96-136d-408a-9918-5808eaa928ed%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to