API: Get assigned Material



does anyone know how I can get the to an Object assigned material… (eg. Lambert1)??? (In API)



this worked on a quick test I did but I can’t imagine it would work on a more coplex structure.
I think the way to go around this is to check the connections of the shape of the object until you find the shaderEngine and then list the connections again to find the material that is connected to that shaderEngine.

 // get selection
 string $sel[] = `ls -sl -fl`;
 // check if selection is the shape, if not get the shape of the selection
 if(`nodeType $sel[0]` == "transform")
 	$sel = `pickWalk -d down`;
 // list connections of type "shadingEngine"
 string $connections[] = `listConnections -t "shadingEngine"`;
 // if no shading engines found quit script.
 	error "no shaders attached";
 string $shaderEng = $connections[0];
 // get the node that is connected to the shading engine's surfaceShader attribute
 $connections = `listConnections -source 1 -destination false ($shaderEng + ".surfaceShader")`;
 // $shNode is the name of the material attached to the object
 string $shNode = $connections[0];

any questions feel free to ask.


What tomViolet wrote looks like it will work, of course this is using MEL, which you are able to do through the API, but it can be slow. I would recommend trolling through the API docs to find out how to do this strictly in a plugin.



thanks ^^ but i need it in API anyway… won’t use MEL via API :slight_smile:

I’ve done it allready trough looking at the Conenctions via API and so I can get the SG… but i hope there’s still a better way ^^



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.