Script to connect all mia_materials in a Scene with Colorbuffers


Hello everyone,

yesterday i looked for scripts wich can create 21 Custom Color Buffers, name them (add_result, refl_level, refr_level…) and connect them to WriteToColor Nodes.

Because i didn’t find any i tried it on my own. I’m not a coder and don’t have any experiences with mel, but i could realize this first step by recording/and copying the commands.

The next and maybe more difficult step would be, to connect all Materials in the scene (type Mia_material_x_passes) or at least named like this “Mia_material” with the right colorbuffers.

So that i can use a lot of Mia_Material_x_passes in my scene, after that scan the entire scene for them and connect for example “indirect_raw” to the colorbuffer “Indirect_raw_buffer” for all of them.

The recorded command is:
connectAttr -f mia_material_x_passes3.indirect_raw indirect_raw_buffer.color;
connectAttr -f mia_material_x_passes3.indirect_raw indirect_raw_buffer.evaluationPassThrough;

But of course it only works for the material named “mia_material_x_passes3”.

And that is what i’m trying to achieve.

select -r “mia_material*”;
string $miaNode[] = ls -selection;

connectAttr -f $miaNode.indirect_raw indirect_raw_buffer.color;
connectAttr -f $miaNode_x_passes3.indirect_raw indirect_raw_buffer.evaluationPassThrough;

Of course it’s not that easy :-/
Today i found a great (closed) thread with the same topic but with an different approach.

I think my problem could be, that i only have this one variable ($miaNode) right now??

I would be really happy, if you can show me how to solve this problem.

Thank you!
Best wishes


try this :

 string $miaNode[] = `ls -type "mia_material_x_passes"`;
 for ($node in $miaNode)
 	connectAttr -f ($node + ".indirect_raw") indirect_raw_buffer.color;
 	connectAttr -f ($node + ".indirect_raw") indirect_raw_buffer.evaluationPassThrough;

hope this helps…
cheers! :slight_smile:


Hey! Thank you, immediate neighbor :smiley:
I will try it right now! :slight_smile:


Edit: Super! Das sieht jetzt wirklich schon sehr vielversprechend aus! Auf der nächsten Mediaworks party geb ich dir dann ein Bier aus! :wink:


So, generally it works great! There’s only one problem left. After the first Mia_material _x_passes was connected to the colorbuffers, the others won’t connect. No errors or anything. It seems like he stops doing it after the first mia $node has been connected.

Is there something like for each? Or $nodes?
I think it would be nice to have a thick book about mel! :smiley:


i’m not completely sure what you’re trying to do … but could it be that every material needs it’s own raw buffer nodes? … if that’s the case you could do something like this:

  string $miaNode[] = `ls -type "mia_material_x_passes"`;
  string $buffer;
  for ($node in $miaNode)
  	$buffer = `mrCreateCustomNode -asUtility "" writeToColorBuffer`;
  	$buffer = `rename $buffer ("indirect_raw_buffer_" + $node)`;
  	connectAttr -f ($node + ".indirect_raw") ($buffer + ".color");
  	connectAttr -f ($node + ".indirect_raw") ($buffer + ".evaluationPassThrough");
  	// you could repeat that for different buffer nodes if needed
  	$buffer = `mrCreateCustomNode -asUtility "" writeToColorBuffer`;
  	$buffer = `rename $buffer ("diffuse_raw_buffer_" + $node)`;
  	connectAttr -f ($node + ".diffuse_raw") ($buffer + ".color");
  	connectAttr -f ($node + ".diffuse_raw") ($buffer + ".evaluationPassThrough");

that would create & connect a writeToColorNode for every x_passes shader …
if you need more help maybe UL the scene or an example scene … guess i could help you with the code, but i’m not to much into the shading stuff. - sry …

beer sound good, though :wink:


Just some minutes ago i had the same thoughts… could it be, that it needs a color buffer for every material… and yes, so it is. :smiley:

That’s really a mess!

Thank you for the new code! I will try it tomorrow! :slight_smile:

In 3dsmax it’s possible to select all these renderpasses (diffuse_level, refl_raw, relf_result…)directly in the passes window and thats it. All Mia_Materials become splittet into these pre-defined color buffers and you can comp them together with a maximum of flexibility.

In Maya it’s not that easy. You can only generate Custom Color buffers (name them diffuse_level for example) and WriteToColor Nodes. Now it’s possible to connect the Mia Materials_x_passes with this Write to Color buffers and write the informations from the shader directly in the Custom color buffers. So you need 21 Custom Color Buffers to write the Informations (reflection_raw, refraction_raw, indirect_raw etc.) from the Material_x_passes shader in the colorbuffer.

I think and hope the code works for this :slight_smile:
It’s easy to imagine what kind of mess you get with 20 mia_material_x_pass Materials * 21 Colorbuffers.

But with a script, it would be easier than doing this stuff by hand.

I have to go know. I hope i have success with your script! :slight_smile:



I tried your code and it seems to work but i think there is one problem left.

$buffer = rename $buffer ("indirect_raw_buffer_" + $node);

After renaming the buffer for example refl_level_buffer_2 he needs also to know in wich pass to write. I think it’s something like this??

connectAttr “indirect_raw.message” “indirect_raw_buffer_.renderPass”;

Of course it’s not that easy and it doesn’t work this way… :frowning:
I don’t have a example scene. I only use 2 Spheres, 2 Mia Shader add those 21 Renderpasses and 21 WriteToColorbuffer with a script, take your third script to connect the Mia to buffers, render and try out if he split the shader informations correctly.

I took some screenshots to show you what i want to do and wich “scripts” i have for the first two steps (Renderpass generation, renaming) and Wirte To Colorbuffer renaming and connecting to the Framebuffer.

I hope its kind of “easy” to do. :slight_smile:



ok, i looked a bit more into this render pass stuff in general to understand what you’re trying to do. and from this plus your posted Scriptablauf.jpg i think i figured out what you’re after and how it can be solved :slight_smile:

to split your materials into separate passes, every shader will need just as many buffer nodes as you have passes (e.g. a scene with 3 shaders & 4 passes will need 12 buffer nodes). 
at first you need to create a custom color scene pass and associate it with a render layer. 

to feed stuff into this scene pass you will have to split up your shaders into buffer nodes and the buffer nodes need an input connection from the scene pass.

here's a little procedure that does all that for you:

    global proc RS_createRenderPasses()
    	string $buffer, $pass, $shader;
    	string $miaNodes[] = `ls -type "mia_material_x_passes"`;
    	// you can add passes to the array
    	// passes have to be after the attribute for this script to work!
    	string $passes[] = {"refl_level", "refl_raw", "add_result", "indirect_raw"};
    	// loop through all mia_material_x_passes shaders
    	for ($shader in $miaNodes)
    		// loop through passes
    		for ($pass in $passes)
    			// create scene pass if not existing
    			if (!`objExists $pass`)
    				// create custom color scene pass
    				createNode -n $pass renderPass;
    				applyAttrPreset $pass "C:/Program Files/Autodesk/Maya2011/presets/attrPresets/renderPass/customColor.mel" 1;
    				// associate scene pass with default render layer 
    				connectAttr -nextAvailable defaultRenderLayer.renderPass ($pass + ".owner");
    			// create buffer if not existing (every shader will need as many buffer nodes as you have passes!)
    			if (!`objExists ($pass + "_buffer_" + $shader)`)
    				// buffer will be named "passName_buffer_shaderName" (e.g. "refl_level_buffer_mia_material_x_passes1")
    				$buffer = `mrCreateCustomNode -asUtility "" writeToColorBuffer`;
    				$buffer = `rename $buffer ($pass + "_buffer_" + $shader)`;
    				// connect scene pass to buffer
    				connectAttr -f ($pass + ".message") ($buffer + ".renderPass");
    				// connect shader to buffer
    				connectAttr -f ($shader + "." + $pass) ($buffer + ".color");
    				connectAttr -f ($shader + "." + $pass) ($buffer + ".evaluationPassThrough");
it creates all render passes defined in the $passes array (so you can add more, read comments in script for details) and splits up all mia_material_x_passes shader into these passes. 

hope that’s what you’re looking for :slight_smile:


Wow, thats awesome! Really great work! Thank you very much!
I think this script could be interesting for a lot of maya 2011/2012 users!

It’s impressive how a few lines of code can save several hours per scene or just make it possible to do those stuff! I think i should start with learning the basics of MEL or Python! :slight_smile:

I hope someday i can return the favor!

Best wishes,


you’re welcome - glad i could help! :slight_smile:

… and yes, knowing how to script stuff can speed up your workflow exponentially. i for one want to look more into python since it seems more flexible & efficient plus it gives you access to the maya api. but still, knowing some MEL can’t hurt if you’re working with maya :wink:


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.