March 2019: is getting relocated to, please update your bookmarks.

MEL How-To #104

Back · Previous · Next Maya

How do I assign an object or face component to a material?

Maya uses Shading Groups to control the association of objects and materials. Shading Groups are created as a "renderable" set. For example:

// Create a Shading Group
sets -renderable true -noSurfaceShader true -empty -name "myShadingGroup";

Logically then, a Shading Group's membership is controlled like other Maya sets - via the "sets" command.

To assign an object or a face component to a material, you simply add that object to the Shading Group which controls membership for that material. If you attempt at assign objects to the material instead, Maya will politely inform you that a material node is not a set:

// Error: The argument provided is NOT a set //

Note that Shading Group assignments are mutually exclusive - you cannot assign an object to more than one Shading Group. If an object is already a member of one Shading Group when you attempt to assign it to another, then Maya will issue an error:

sets -add blinn1SG pSphere1;
// Error: Cannot add the following items to the set since they would break the
          exclusivity constraint //

To instruct Maya that you know what you're doing and, yes, you do want to re-assign your object to a new Shading Group, specify the '-forceElement' flag with the sets command. You'll commonly see this flag by its short form: '-fe'.

sets -forceElement blinn1SG pSphere1;
// Result: blinn1SG //

The same method is used for assigning individual face components to Shading Groups:

// Assign face 0 to a blinn shader.
sets -fe blinn1SG pSphere1.f[0];
// Result: blinn1SG //

// Assign faces 7-11 to a phongE shader.
sets -fe phongE1SG pSphere1.f[7:11];
// Result: phongE1SG //

Related How-To's

04 Sep 2004