Home Coding, Scripting, Shaders

Python Code Check - Maya

sparkyrex
polycounter lvl 9
Offline / Send Message
sparkyrex polycounter lvl 9

Hello,
I am learning python with Maya. The problem I have with the following code is that the shader is
deselected when objects are selected. If I reverse command order, objects are deselected when shader is selected and thus can’t make shader assignment. I’m sure the code is basic, but all the code I have found thus far is more complicated, and when I paste it into script editor, I get errors that I can’t seem to solve at my level. Any advice is appreciated.

CODE
import maya.cmds as mc

mc.polyCube(h=5, w=3, d=3)
mc.polySphere(n=‘bola’)
mc.move(0,7,0)
mc.duplicate()
mc.move( 0, 3, 0, relative=True)
mc.duplicate( st=True )
mc.duplicate( st=True )
mc.shadingNode(‘blinn’, asShader=True)
mc.select(‘blinn1’)
mc.select(allDagObjects=True)
#mc.sets() COULD BE HELPFUL - NOT NECESSARY
mc.hyperShade( ‘blinn1’, assign=True )
###ERROR MESSAGE #Error: line 1: No shader is selected to be assigned

CAN’T SELECT OBJECTS & SHADER AT SAME TIME - WHAT IS WORKAROUND?

Replies

  • poopipe
    Online / Send Message
    poopipe grand marshal polycounter
    You can assign the material to objects by putting them into the shading group attached to that material. 

    I forget the syntax etc. So you'll have to look it up. 
  • haiddasalami
    Offline / Send Message
    haiddasalami polycounter lvl 14
    import maya.cmds as mc
    
    mc.polyCube(h=5, w=3, d=3)
    mc.polySphere(n="bola")
    mc.move(0,7,0)
    mc.duplicate()
    mc.move( 0, 3, 0, relative=True)
    mc.duplicate( st=True )
    mc.duplicate( st=True )
    myBlinnShader = mc.shadingNode("blinn", asShader=True)
    mc.select(allDagObjects=True)
    mc.hyperShade( assign=myBlinnShader )
    Should work now. Basically you store the shading network in a variable (myBlinnShader in this case) and assign it. Hypershade will assign the shader to the meshes selected so no need to select the material. This would be the most straight forward/basic way but the better future way to do it is with sets when you need to do like multiple materials within a single object.  

    import maya.cmds as mc
    
    mc.polyCube(h=5, w=3, d=3)
    mc.polySphere(n="bola")
    mc.move(0,7,0)
    mc.duplicate()
    mc.move( 0, 3, 0, relative=True)
    mc.duplicate( st=True )
    mc.duplicate( st=True )
    mc.select(allDagObjects=True)
    selected = cmds.ls(sl=True,long=True)
    #Create a blinn material
    blinnMaterial = mc.shadingNode('blinn', asShader=1)
    #Create a shading group
    shadingGroup = mc.sets(renderable=1, noSurfaceShader=1, empty=1)
    #Connect the material to the shading group
    mc.connectAttr((material+'.outColor'),(SG+'.surfaceShader'),f=1)
    #Assign the material using sets
    mc.sets(selected, e=1, forceElement=SG)
Sign In or Register to comment.