PDA

View Full Version : Python, how to assign a material to an object


misterwolfy
09-25-2010, 06:04 AM
How can I assign a material to an object using python?

In MEL it seems to be 'set'. While the geometry is selected this seems to work:


//MEL
sets -e -forceElement ($myBlinn + "SG");


I've tried a few things with Python, but it does not work.


##Python, not working
mc.sets( (myBlinn + "SG") )
## or
mc.sets( (myBlinn + "SG") e=True, forceElement=True)

nathansims100
09-25-2010, 07:14 AM
This should work.
mc.sets( e=True, forceElement= myBlinn + 'SG' )

misterwolfy
09-25-2010, 07:34 AM
works, thanks!

ewerybody
10-06-2010, 10:23 AM
If you don't say what to assign to I assume you do this with the selection only. So you could also use the hyperShade (http://download.autodesk.com/us/maya/2009help/CommandsPython/hyperShade.html#flagassign) command:mc.hyperShade( myBlinn, assign=True )advantages:


you can use the shader name directly
ShadingGroups get also created when the shader is standalone yet
disadvantage:

ONLY works with selection
not available in batch mode
I recently figured that out myself: I mean if you need it, its simply as with all py commands: the stuff you want to assign to comes first:mc.sets(thatMembers, e=1, forceElement=thisSG)But you know there "could" exist a shader without a shadingGroup!

somian
06-25-2011, 08:05 AM
Doesn't work here :(

Am I getting this right?


maya.select(objectname)
maya.hyperShade(shadername,a=True)


i'm getting a "# Error: No shader is selected to be assigned #", but the Shadername is equal to the string in the attribute editor when I select the shader.

ewerybody
08-26-2011, 08:57 PM
Whoar I've just checked what I've written there! :D
bullsht!! But its from the original docs! Its wrong in the docs!
The hyperShade call works like in MEL:

from maya.cmds import *
hyperShade(a=shaderName)

CGTalk Moderation
08-26-2011, 08:57 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.