PDA

View Full Version : How to create and assign shaders with mel ?


NandoNkrumah
12-08-2012, 04:31 PM
Hi,

I try to write a script that simply creates multiple (2200) surface shaders and assigns them to (2200) poly planes that I have duplicated before.

I am not good in scripting so I got a error with this script:

for ($i = 1; $i < 2201; $i++)
{
hyperShadePanelCreate "shader" surfaceShader;
shadingNode -asShader surfaceShader;
sets -renderable true -noSurfaceShader true -empty -name surfaceShader($i)SG;
select -r pPlane($i);
sets -e -forceElement surfaceShader($i)SG;
};


Any ideas how to write a proper script that creates the surface shaders and assigns them to the planes. The important thing is that I want shader1 to be assigned to polyPlane1 and so on.
Thank you

DeadlyNightshade
12-08-2012, 11:16 PM
Well you are halfway there.
You just need to store a selection (the meshes) into an array first (ls -sl)
Then with a for-loop select each mesh and assign a material/shader for it.

Also turn on all echoing and error reporting in the script editor: you do this under "history" in the script editor. That way Maya will give you proper error reporting.

NandoNkrumah
12-09-2012, 08:42 AM
Hi DeadlyNightshade,
thanks for the fast reply.
Can I be sure that the list selection command (ls -sl) will select and store the polyplanes in the correct order (1,2,3,...)?
How would the script look exactly?
I am not a programmer so a detailed answer would be highly appreciated.
Thank you.


Well you are halfway there.
You just need to store a selection (the meshes) into an array first (ls -sl)
Then with a for-loop select each mesh and assign a material/shader for it.

Also turn on all echoing and error reporting in the script editor: you do this under "history" in the script editor. That way Maya will give you proper error reporting.

DeadlyNightshade
12-09-2012, 12:26 PM
string $var[] = `ls -sl`;
select $var[0];
Arrays start counting from 0 so if you store 5 meshes in an array, the last mesh is on position 4

Replace the number by the variable $i from the for-loop, and also make the for-loop start from 0 and not 1.

I'm not a programmer either but I learned MEL so that I can improve my workflow in Maya, make my own tools and so on. I have this personal goal of trying to be a TA some day and for that I need to know scripting and some programming and some other stuff... Takes time to get there I know, but some day...

NandoNkrumah
12-09-2012, 04:16 PM
Thank you.
The only thing that I have to check is the order of the array.
I already have numbered Poly Planes and for the next step it is important that the order of the shaders corresponds to the number order of the Poly Planes. I am not sure if when I select all Poly Planes those will be selected by their numerical order but I will have to check it.
I really appreciate your support and hope that you will work as an TA very soon.


string $var[] = `ls -sl`;
select $var[0];
Arrays start counting from 0 so if you store 5 meshes in an array, the last mesh is on position 4

Replace the number by the variable $i from the for-loop, and also make the for-loop start from 0 and not 1.

I'm not a programmer either but I learned MEL so that I can improve my workflow in Maya, make my own tools and so on. I have this personal goal of trying to be a TA some day and for that I need to know scripting and some programming and some other stuff... Takes time to get there I know, but some day...

DeadlyNightshade
12-09-2012, 04:28 PM
Okay that might be a problem as everytime you do your selection manually in Maya you will most likely get a different selection order. It's no point to try and order them right in the array (it's probably not impossible, just over-complicated).

If the planes are identical it would be a smarter idea to rename the meshes in the same for-loop. So you get both a material assignment and a name assignment for each mesh in the array.

NandoNkrumah
12-09-2012, 09:26 PM
that's a smart idea.
thank you


If the planes are identical it would be a smarter idea to rename the meshes in the same for-loop. So you get both a material assignment and a name assignment for each mesh in the array.

CGTalk Moderation
12-09-2012, 09:26 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.