[MEL] A problem in conditions within a for loop

[MEL] A problem in conditions within a for loop

Anonymous
Not applicable
1,208 Views
3 Replies
Message 1 of 4

[MEL] A problem in conditions within a for loop

Anonymous
Not applicable

Hey all, I am writing a material swap script base form a template I found,
Here is my main procedure:

global proc vrayAllLambert()
{
	// get selected shaders
		//string $sel[] = `ls -sl -st`;
		//for ($i=0;$i<size($sel);$i=$i+2)
	
	//get shading engine
	string $sg[]= `ls -type "shadingEngine"`;

	int $num = size ($sg);
	for ( $i=0; $i<$num; ++$i ){
		// find shaders have SG
		string $mtl[] = `listConnections ($sg[$i] + ".surfaceShader")`;
		select -add $mtl;
	}
	
	string $allsgMtl[] = (`ls -sl`);
	sets -n set_allVRayMtl $allsgMtl;
	string $lambert[]= `ls -type "lambert"`;
	sets -rm set_allVRayMtl $lambert;
	select -r -add set_allVRayMtl;
	string $allVrayMtl[] = (`ls -sl`);

	//int $num2 = size ($allVrayMtl);
	
	for ($i=0; $i<size($allVrayMtl); $i=$i+2){

    	if ($allVrayMtl[$i+1] == "VRayMtl")
    		{	
    			print ("\n");
    			print ("running Lambert conversion on " + $allVrayMtl[$i] + " hold on tight");
    			vray2Lambert($allVrayMtl[$i]);
    		}
    
    	else if ($allVrayMtl[$i+1] == "VRayCarPaintMtl")
    		{		
    			print ("\n");
    			print ("running Lambert conversion on " + $allVrayMtl[$i] + " hold on tight");
    			carpaint2Lambert($allVrayMtl[$i]);
    		}
    	else if ($allVrayMtl[$i+1] == "VRayBlendMtl")
    		{	
    			print ("\n");
    			print ("running Lambert conversion on " + $allVrayMtl[$i] + " hold on tight");
    			blend2Lambert($allVrayMtl[$i]);
    		}
			
	}
}


It was working if just select the materials after my update.

 

And my second update was trying to run it globally in the scene (line7-22).
It ran without an error, but seems it didn't pick up the second for loop in line 26 anymore.
I have print a message in those conditions, none of that print out....

My guess is the $i, please help, thanks!

0 Likes
1,209 Views
3 Replies
Replies (3)
Message 2 of 4

jmreinhart
Advisor
Advisor
	select -r -add set_allVRayMtl;
	string $allVrayMtl[] = (`ls -sl`);

This part of the code may be your issue. Selecting the set, is not the same as selecting the objects within the set. 

Also, in-general, using your selection to handle lists of objects in the middle of your script is not the best way to do things. You should look into using arrays in the future.

Message 3 of 4

Anonymous
Not applicable

Thanks for the reply!

I will trying to use array instead of selection, but I think the reason I was using select is because I have no idea how to keep the $mtl outside of the first for loop, I am retrying that part, here is my updated one:

//get shading engine
string $allSG[]= `ls -type "shadingEngine"`;
string $defaultSG[] = {"initialShadingGroup" , "initialParticleSE"};
string $sg2[] = stringArrayRemove($defaultSG, $allSG);

int $num2 = size ($sg2);
for ( $i=0; $i<$num; ++$i ){
    // find shader have SG
    string $allVrayMtl[] = `listConnections ($sg2[$i] + ".surfaceShader")`;
    // working here
    //print $allVrayMtl;
}
//can't print here
print $allVrayMtl;

looks working in the for loop, but I need use the $allVrayMtl in some other proc, any idea how to do that? thanks again!

0 Likes
Message 4 of 4

jmreinhart
Advisor
Advisor

You just need to declare the variable before the for loop. If you declare it inside the for loop, then when the for loop ends Maya will "forget" about it, assuming you only wanted it for the loop. It's an easy thing to miss.

0 Likes