UI_Shader attribute change based on object selection


Hello I need to write a script to change the ambient occlusion attributes on any mia material , bassically I need to change Samples , Distance, Ambient Shadow Color and Ambient Light color to all selected objects., Im new to Mel so I’m having a lot of problems finding the best way to do so, I have been reading a lot of tutorials and the maya help files, but I can’t make it work , sounds easy but for me is taking for ever.

I just need to know how to list materials for the selected objects
this is my approach:

string $sel[] =ls -sl -mat;
// Result: //

(nothing results of this) “I need to get the following result but just for the selected objects”:

[b]string $sel[] =ls -mat;
// Result: lambert1 particleCloud1 Green red Blue Yellow Purple mip_matteshadow1 phong1 phong2 phong3 phong4 //

[/b]this way I can change any attribute for the selected materials…



Maya is a lot about connections of nodes.
If you select an node. ls -sl will give you what you have selected.
If your node is a material you will get it with ls -sl -mat of course!

But if you have a transform node selected (which is most often referred as “object”) you have no material selected. You need to browse the connections to find the material.
Ok. Transforms have shape children, these are connected to shadingGroups which are connected to a material.

so try this:

global proc string[] getMaterialFromSel()
	string $materials[];
	string $shadingGroups[];
	// will give you shapes no matter if shape, transform or components are selected
	string $selShapes[] = `ls -sl -o -dag -s`;
	for ($shape in $selShapes)
		$shadingGroups= `listConnections -type "shadingEngine" $shape`;
		for ($shadingGroup in $shadingGroups)
			// give me all connected things and filter them by materials (not all materials are derrived from "lambert")
			$materials = stringArrayCatenate($materials,ls("-mat",`listConnections initialShadingGroup`));
	return (stringArrayRemoveDuplicates($materials));

if you want to change the attributes in your controls depending on the shaders in the selection. I guess you need a scriptJob, that controls selectionChange.
You can make it belong to your window via -parent “AO_WIN” so you don’t need to worry about scriptJob IDs and killing them …

you are actually able to connect a control to multiple shader attributes
see connectControl command


one could actually have this inline…

stringArrayRemoveDuplicates(ls("-mat",listConnections(listConnections("-type","shadingEngine",`ls -sl -o -dag -s`))));



Thank you man, I’m very new to MEL so Im not very sure how to use this.
bassically what I need is to list the name of my shaders not the shaders shape.I need to get the names in an array:

[b]// Result: green red blue yellow purple //

[/b]when I change an attribute on the shaders this is the result I get in the command line example:

[b]setAttr "purple.ao_on" 1;

[/b]so if I called the shapes instead of the names my resulting script would say this:[b]

setAttr "[/b][b]mia_material_x_passes5SG[/b][b].ao_on" 1;
[size=2]this would not work because mel uses the name to change attributes and not the shapes.

so when I run this script:

[b][size=3]string $sel[] =`ls  -selection  -materials`;
        for ($obj in $sel) {
        print ($obj + "

setAttr ($obj + “.ao_on”) 0 ;
setAttr ($obj + “.ao_samples”) 64;

[/b]it should take the name of each shader: yellow, purple, blue etc and add the attribute + the new value, as specified above.

My only problem is that doing it like this I can’t get any results to print so the script does not work. :frowning: ANY IDEAS ?

Thanks in advance :).


OHH man this is it, thank you Dude , I did not see this before.

thanks you thank you thank you…



the other one does exactly the same only in a proc! ;]

And I strongly recommend to avoid heavy inlinestuff like that! Because it doesn’t help your understanding. It just makes things worse I guess. The inline one was only for fun! I would not use such a line myself! I’d refer to the proc:

string $shaders[] = getMaterialFromSel();

And please reread my first post!
ls -selection -materials only works if you selected materials! It does literally what its supposed to: It lists (ls) -materials from the -selection. If you have no materials in your selection there will be no output. ls is no command that traverses any connections.

Welcome under the hood of Maya.


Welcome under the hood of Maya.[/QUOTE]

[b]Thank you man, this is all so confusing!

I have to keep reading and studieng mel.

how long did it take you to master MEL ?[/b]


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.