I have a piece of python code to use in Maya that is supposed to take a colour from the user through an UI colorSliderGrp and use it to set a material and that said colour to some cylinders.
This is my colour slider, used in the createUI function that I have:
colourControl=cmds.colorSliderGrp( label='Blue', rgb=(0, 0, 1) )
And then I have my setMaterial function:
def setMaterial(objName, colour, materialType='lambert'):
setName=cmds.sets(name='_MaterialGroup_', renderable=True, empty=True)
shaderName = cmds.shadingNode(materialType, asShader=True)
cmds.setAttr(shaderName+'.color', colour[0], colour[1], colour[2], type='double3')
cmds.surfaceShaderList(shaderName, add=setName)
cmds.sets(objName, edit=True, forceElement=setName)
I access setMaterial with both objName and colour parameters in a third function and I get an error for the line where I defined colourControl:
'NoneType' object has no attribute '__getitem__'
I have tried this after following an example for a default colour:
def setMaterial(objName, materialType='lambert', colour=(0, 0, 1)):
setName=cmds.sets(name='_MaterialGroup_', renderable=True, empty=True)
shaderName = cmds.shadingNode(materialType, asShader=True)
cmds.setAttr(shaderName+'.color', colour[0], colour[1], colour[2], type='double3')
cmds.surfaceShaderList(shaderName, add=setName)
cmds.sets(objName, edit=True, forceElement=setName)
I tried other sliders as well and tried to print colourControl to see what I'm working with, but I got
a|columnLayout73|colorSliderGrp58
where a was the name of my UI window and the numbers after columnLayout and colorSlider will keep changing at every run of my code.
Have a good day!
I have a simple working version of a code which should work as you expected. You need to use partial unless you reuse your float slider group. If so you will see how you can use your slider and query the color value.
import maya.cmds as cmds
from functools import partial
def setMaterial(objName, myColorSlider, matType, *args):
colorValue = cmds.colorSliderGrp(myColorSlider, q=True, rgb=True)
setName=cmds.sets(name='_MaterialGroup_', renderable=True, empty=True)
shaderName = cmds.shadingNode(matType, asShader=True)
cmds.setAttr(shaderName+'.color', colorValue[0], colorValue[1], colorValue[2], type='double3')
cmds.surfaceShaderList(shaderName, add=setName)
cmds.sets(objName, edit=True, forceElement=setName)
cmds.window()
cmds.columnLayout()
myColorSlider = cmds.colorSliderGrp( label='Blue', rgb=(0, 0, 1))
cmds.colorSliderGrp(myColorSlider, edit=True, cc=partial(setMaterial, "pSphere1", myColorSlider, "lambert") )
cmds.showWindow()