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

09 September 2010, 05: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:

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)

09 September 2010, 06:14 AM
This should work.
mc.sets( e=True, forceElement= myBlinn + 'SG' )

09 September 2010, 06:34 AM
works, thanks!

10 October 2010, 09: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 ( command:mc.hyperShade( myBlinn, assign=True )advantages:

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

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!

06 June 2011, 07:05 AM
Doesn't work here :(

Am I getting this right?

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.

08 August 2011, 07: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 *

CGTalk Moderation
08 August 2011, 07: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.