Assigning a Material using Pymel

Hey all, hope all is well.

I am trying to convert this python code into pymel code.


# Gears Material
metal = mc.shadingNode("blinn", asShader=True, name ="metalMat")
mc.setAttr(metal + ".color", 0.667,0.317,0.131)
mc.setAttr(metal + ".eccentricity", 0.421)
mc.setAttr(metal + ".specularRollOff", 0.972)
mc.setAttr(metal + ".specularColor", 0.703,0.703,0.703)
mc.setAttr(metal + ".reflectivity", 0.538)
mc.setAttr(metal + ".reflectedColor", 0.310,0.310,0.310)

# Assing Material
# Create Sureface Shader
mc.sets( renderable=True, noSurfaceShader=True, empty=True, name="gearShader" )
# Connect material to shader
mc.connectAttr("metalMat.outColor", "gearShader.surfaceShader")
# Assign shader to objects
mc.sets("smallGearGeo", edit=True, forceElement="gearShader")
mc.sets("largeGearGeo", edit=True, forceElement="gearShader")

This is what I have so far in pymel

# Gears Material
metal = shadingNode("blinn", asShader=True, name ="metalMat")
metal.color.set([0.667,0.317,0.131])
metal.eccentricity.set(0.421)
metal.specularRollOff.set(0.972)
metal.specularColor.set([0.703,0.703,0.703])
metal.reflectivity.set(0.538)
metal.reflectedColor.set([0.310,0.310,0.310])

# Assing Material
# Create Sureface Shader
gearShader = sets( renderable=True, noSurfaceShader=True, empty=True, name="gearSurfaceShader" )
# Connect material to shader
metal.outColor >> gearShader.surfaceShader
# Assign shader to objects
sets(smallGear, edit=True, forceElement="gearSurfaceShader")
sets(largeGear, edit=True, forceElement=gearShader)

The last two lines are where I’m having trouble, the two lines are different from each other as I was trying different methods to get it to work.

smallGear and largeGear refer to polyCylinder objects created earlier in the script.


largeGear = polyCylinder(radius=10, height=1, subdivisionsX=40,subdivisionsZ=0, name="largeGearGeo")[0]
smallGear = polyCylinder(radius=5, height=1, subdivisionsX=20,subdivisionsZ=0, name="smallGearGeo")[0]

Everytime I try to assign to run the last two lines of the pymel code to assign the materials to the two gears objects I get an error saying:

Error: RuntimeError: file C:\Program Files\Autodesk\Maya2011\Python\lib\site-packages\pymel-1.0.0-py2.6.egg\pymel\internal\pmcmds.py line 98: The argument provided is NOT a set

Its probably something really simple but I just cant see it, can anyone help me out.

Cheers.

1 Like

Make that something like:

sets(gearShader, forceElement=‘largeGearGeo’)

or

sets(‘gearSurfaceShader’, forceElement=smallGear)

Many thanks that worked like a charm.

Also allowed me to do both in one line


sets(gearShader, edit=True, forceElement=[smallGear,largeGear])

Cheers.

I’m having problems with this:
Pymel 1.0 Maya 2009



myObj=pma.ls(sl=1)[0]
faceList=pma.polyListComponentConversion(myObj.getShape(), toFace=1)
faceList=pma.ls(faceList,flatten=1)
pma.sets('proxyPropShader_SG',forceElement=faceList)

Results in:


# Warning: Node 'Band_SawShape.instObjGroups[0].objectGroups[0]': cannot make assignment to 'proxyPropShader_SG' shader. # 
# Error: Connection not made: 'Band_SawShape.instObjGroups[0].objectGroups[-1]' -> 'proxyPropShader_SG.dagSetMembers[-1]'.  Source node will not allow the connection.
# Error while parsing arguments.
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
#   File "...\maya\2009\python\pymel\core\general.py", line 1268, in sets
#     result = cmds.sets( *args, **kwargs )
#   File "...\maya\2009\python\pymel\internal\pmcmds.py", line 98, in wrappedCmd
#     res = new_cmd(*new_args, **new_kwargs)
# RuntimeError: Connection not made: 'Band_SawShape.instObjGroups[0].objectGroups[-1]' -> 'proxyPropShader_SG.dagSetMembers[-1]'.  Source node will not allow the connection.
# Error while parsing arguments. # 

if I use the Shader instead of the ShaderEngine:
If I pass the transform or the shape I get this error.


pma.sets('proxyPropShader',edit=1,forceElement=faceList)


# Error: The argument provided is NOT a set
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
#   File "...maya\2009\python\pymel\core\general.py", line 1268, in sets
#     result = cmds.sets( *args, **kwargs )
#   File "...\maya\2009\python\pymel\internal\pmcmds.py", line 98, in wrappedCmd
#     res = new_cmd(*new_args, **new_kwargs)
# RuntimeError: The argument provided is NOT a set # 

Mel would be:


sets -forceElement $shadingGroup $faceList;

which would indicate:


pma.sets(faceList,forceElement='proxyPropShader_SG')

which results in:


pma.sets(faceList,forceElement='proxyPropShader_SG')
# Error: No object matches name: (u'Band_SawShape.f[0]', u'Band_SawShape.f[1]', u'Band_SawShape.f[2]', u'Band_SawShape.f[3]', u'Band_SawShape.f[4]',.... )
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
#   File "...\maya\2009\python\pymel\core\general.py", line 1268, in sets
#     result = cmds.sets( *args, **kwargs )
#   File "...\maya\2009\python\pymel\internal\pmcmds.py", line 98, in wrappedCmd
#     res = new_cmd(*new_args, **new_kwargs)
# TypeError: No object matches name: (u'Band_SawShape.f[0]', u'Band_SawShape.f[1]', u'Band_SawShape.f[2]', u'Band_SawShape.f[3]', ...)


HUH??

Strikes me that this is not good and needs a suitable fix if it is this complicated to figure out how to use…

In the end this worked:


def shaderCreateAssignSelected():
    
    myObj=pma.ls(sl=1)[0]
    faceList=myObj.f
    
    shader='ass'
    shader=pma.shadingNode('lambert',name=shader,asShader=1)
    sg=pma.sets(renderable=1,noSurfaceShader=1,empty=1,name=shader+'_SG')
    pma.connectAttr( shader + ".outColor",sg + ".surfaceShader",force=1)
    pma.sets(sg,edit=1,forceElement=faceList)



def shaderCreationFail_01():    
    myObj=pma.ls(sl=1)[0]
    faceList=myObj.f
    shader='ass'
    shader=pma.createNode('lambert', name=shader)    
    sg = pma.createNode('shadingEngine', name=shader+'_SG')
    pma.connectAttr( shader + ".outColor",sg + ".surfaceShader",force=1)
    pma.sets(sg,edit=1,forceElement=faceList)

Errors:
# Warning: Cannot add the following items to the set since the set has restrictions on membership: ass_SG # 
# Warning: None of the items can be added to the set #

def molType(pcl, element, texture = []):
print(texture)
if (element == ‘C’):
setAttr(pcl.radius, 1.70)
select(pcl)
sets(texture[0], edit = True, forceElement= pcl)
elif (element == ‘N’):
setAttr(pcl.radius, 1.55)
select(pcl)
sets(texture[1], edit = True, forceElement= pcl)
elif(element == ‘O’):
setAttr(pcl.radius, 1.52)
select(pcl)
sets(texture[1], edit = True, forceElement= pcl)
elif(element == ‘H’):
setAttr(pcl.radius, 1.20)
select(pcl)
sets(texture[3], edit = True, forceElement= pcl)
elif(element == ‘P’):
setAttr(pcl.radius, 1.80)
select(pcl)
sets(texture[4], edit = True, forceElement= pcl)
elif(element == ‘Fe’):
setAttr(pcl.radius, 2.00)
select(pcl)
sets(texture[4], edit = True, forceElement= pcl)

HIVmol(10)

texture is a list of shading groups.
The code for that is here
def HIVmol(count= 10):
texture = []

textureC = shadingNode("lambert", asShader = True)
setAttr(textureC.color, [0.78,0.78,0.78])#grey
textureCSG = sets( renderable = True, noSurfaceShader = True, empty = True, name = 'textureCSG')
connectAttr( textureC + ".outColor", textureCSG + ".surfaceShader", force = True)    
texture.append(textureCSG)

textureO = shadingNode("lambert", asShader = True)
setAttr(textureO.color, [0.94,0,0])#red    
textureOSG = sets( renderable = True, noSurfaceShader = True, empty = True, name = 'textureOSG')
connectAttr( textureO + ".outColor", textureOSG + ".surfaceShader", force = True)
texture.append(textureOSG)

textureN = shadingNode("lambert", asShader = True)
setAttr(textureN.color, [0.56,0.56,0.99])#dark blue
textureNSG = sets( renderable = True, noSurfaceShader = True, empty = True, name = 'textureNSG')
connectAttr( textureN + ".outColor", textureNSG + ".surfaceShader", force = True)    	
texture.append(textureNSG)

textureH = shadingNode("lambert", asShader = True)
setAttr(textureH.color, [0.99,0.99,0.99])#white
textureHSG = sets( renderable = True, noSurfaceShader = True, empty = True, name = 'textureHSG')
connectAttr( textureH + ".outColor", textureHSG + ".surfaceShader", force = True)   
texture.append(textureHSG)

textureP_Fe = shadingNode("lambert", asShader = True)
setAttr(textureP_Fe.color,[0.99,0.64,0])#orange
textureP_FeSG = sets( renderable = True, noSurfaceShader = True, empty = True, name = 'textureP_FeSG')
connectAttr( textureP_Fe + ".outColor", textureP_FeSG + ".surfaceShader", force = True)
texture.append(textureP_FeSG)

I’m new to pymel and the lack of proper documentation is hurting me… do help

The error im getting is

Error: MayaAttributeError: file C:\Program Files (x86)\Autodesk\Maya 2011 Subscription Advantage Pack\Python\lib\site-packages\pymel\core\general.py line 548:

Here is a snippet that assigns new random colored shaders to each selected object.
One shader per object.


from pymel.core import *
import random   

def shaderCreateAssignSelected(myObj):    
    faceList=myObj.f        
    shader=shadingNode('lambert',name='generatedShader'+myObj,asShader=1)
    shader.color.set([random.random(),random.random(),random.random()])
    sg=sets(renderable=1,noSurfaceShader=1,empty=1,name=shader+'_SG')
    connectAttr( shader + ".outColor",sg + ".surfaceShader",force=1)
    sets(sg,edit=1,forceElement=faceList)
    
for each in selected():
    shaderCreateAssignSelected(each)

1 Like

hello, I want to assign the material to the selected object ,but Warning: line 1018: Cannot add the following items to the set since the set has restrictions on membership: displacementShader1 //
// Warning: line 1018: None of the items can be added to the set //

anyone can help me? thanks!!!

C)ZR@O6)KZ@{%N_D{GMR)MG