Website Youtube GitHub

Script print list of selections


#1

Hello. So this is just a genral scripting question, not really a mGear question, but if anyone could help that would be great!
I would like to make a script to make my geo selection matt (in arnold in attriubtes). So i know how to make a specific object matt (cmds.setAttr(“pCubeShape1.aiMatte”,1)), but I need to be able to make a selection in the viewport and then run the script on that selection, and do it again for a diffrent selection. I am just not quite sure how to do this?


#2

Hey,

If you go through the selection using cmds.ls(sl=True) you get the transforms, which you have to get the shape from. And run the setAttr on those.

I haven’t tested this but should be something like this:

import maya.cmds as cmds

selection = cmds.ls(sl=True)
matte_value = 1  # Change value if desired

for node in selection:
    # Only get the renderable shapes
    shapes = cmds.listRelatives(node, noIntermediate=True, shapes=True)
    for shape in shapes:
        cmds.setAttr("{}.aiMatte".format(shape), matte_value)

#3

Ah thank you so much! I will test it out now and let you know if its working!


#4

It works perfectly! all i had to do was change matte_value = 1 to matteValue = 1 , as you use the name later in the script.


#5

Would i be able to use this if i want to change a attribute in a aiStandardHair shader. So instead of getting the shpe i would need to get the shader right?


#6

Ah right! Forgot I changed the matteValue to matte_value later :slight_smile:

Would i be able to use this if i want to change a attribute in a aiStandardHair shader.

Yeah, you won’t have to search for shapes or anything.

import maya.cmds as cmds

selection = cmds.ls(sl=True)  # works on any selection, even shaders.
new_value = 1  # Change value if desired

for node in selection:
    cmds.setAttr("{}.attributeName".format(node), new_value)

Just note that some attribute types need different arguments to set the value.
The example wouldn’t work on strings or vectors for instance.


#7

Ah thank you so much!!

I have one last question. I want to connect .outMesh to .inMesh for my selection, so i want the first object to be the outMesh and the second object to be the inMesh. is this possible?


#8

Sure thing :slight_smile:. You need the shapes of your selection again for this.

selection = cmds.ls(sl=True)
first_obj = selection[0]
second_obj = selection[1]

first_shape = cmds.listRelatives(first_obj, ni=True, s=True)[0]  # abreviated version of the first script
second_shape = cmds.listRelatives(second_obj, ni=True, s=True)[0]

cmds.connectAttr("{}.outMesh".format(first_shape), "{}.inMesh".format(second_shape))

The script assumes your selections are transform nodes and have mesh shapes.


#9

Ah thank you so so much! you are a real life saver!:grin:


#10

I tried it now and i get an error that says for line “second_shape = cmds.listRelatives(second_obj, ni=True, s=True)[1]” list index out of range?


#11

Oh right, that has to be [0] also :slight_smile:


#12

That worked! thank you so so much! You have no idea how much this has helped me!:grin: