View Full Version : MEL Shader converter

01 January 2013, 09:22 PM
I am working on a shader converter .Like if there is Blinn shader with transparency and displacement map attached to it, it will convert it to Lambert with same nodes intact.
My question is - what if there is multile shaders attached to a single object (my script doesn't work in that case).
So what is the command or procedure to find out a Shader is attached to a limited faces of an object ??
Or there are multiple shaders to a single object .How do we select faces from those shaders ??
I am starting like this -

string $sEngine[] = `ls -typ shadingEngine`;
for ($Engine in $sEngine)
string $material[] = `listConnections -d 0 $Engine`;

if (`attributeExists "transparency" $material[0]` == 1 )
if (`connectionInfo -id ($material[0]+".transparency")` == 1)
.....and so on....

Plz anyone help me out.

01 January 2013, 10:15 PM
you don't really need to do any of that work. Just re-use the existing Shader's ShadingGroups.

Take your new shader, and connect it's outColor Attr to the original shader's SG surfaceShader attr.

import maya.cmds as cmds
shaderA = 'blinn1'
shaderB = 'lambert2'
originalSGs = cmds.listConnections(shaderA, type='shadingEngine')
for sg in originalSGs:
cmds.connectAttr('{0}.outColor'.format(shaderB), '{0}.surfaceShader'.format(sg), force=True)

01 January 2013, 10:20 PM
To determine how many shaders are applied to a mesh, find the connections between the shape node and the shadingGroups.

import maya.cmds as cmds
shape = 'pSphereShape1'
sgs = cmds.listConnections(shape, type='shadingEngine')
sgs = list(set(sgs))
materials = []
for sg in sgs:
material = cmds.listConnections('{0}.surfaceShader'.format(sg))
print materials

Note: Text formatting may be messed up, and code may contain syntax errors...

01 January 2013, 05:51 PM
Hi rgkovach123 and thanks for the reply.
I got your point but real issue is that here I am trying to get faces of a mesh selected from a Shading Engine .
For eg : If a mesh has Blinn1 shader applied on limited faces , the faces can be selected by this - select blinn1SG;
pickWalk -d down blinn1SG;

Or if we go in Hypershade :> Right click on any shader :> there we get an option "select Object with Material". So hows maya recognising the no of faces to select.

I am not satisfied with pickWalk ,so is there any other smart way ?

01 January 2013, 06:55 PM
Shading Groups connected to materials contain a mix of objects and faces across the entire scene.

To limit the query to a single object, you'll need to iterate through all the contents of the shading group and filter out anything not related to the source mesh.

def getMaterialOnObject(shape, material):
result = []
shadingGroup = cmds.listConnections(material, type='shadingEngine')[0]
members = cmds.sets(shadingGroup, q=True)
for m in members:
if shape in m:
return result

There's a little bit more to it, error checking and using long names, etc... but thats the basic idea.

CGTalk Moderation
01 January 2013, 06:55 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.