View Full Version : do something for each selected object in array?


cgbeige
12 December 2010, 04:55 PM
I'm pretty new to programming and loops so this is probably going to sound like a newb question. I want to run a series of commands on each selected object separately, so that it cycles through all the selected objects, runs a bake render and then selects the next object until it's done with the array. This doesn't seem to work:

$mySelection = `ls -sl`;

for($each in $mySelection)
{
select -r $each;
textFieldGrp -e -text $mySelection -forceChangeCommand vrayFileNamePrefix;
RedoPreviousRender;
}

It just renders once (but renders the correct thing and file name). If you're wondering why I'm doing a crazy ass-backwards bake like this when V-Ray has batch baking, it's because the batch bake doesn't do AO passes and it's much slower.

onetime
12 December 2010, 05:26 PM
Hi cgbeige,

I'm pretty new to programming as well, so consider yourself warned, because I don't want to lead you astray. That said, I've been using this type of script quite a bit lately. It's very handy for a LOT of different things.

I don't have V-Ray, so I'm not sure what you're trying to do exactly. But here's what I would suggest:

Remove the "select -r $each;" line. It seems redundant since the object is already selected (or objects, since you've defined each object in your selection, but only one at a time in this case).

Also, try defining your variable like this:string $mySelection[] = `ls -sl`; I don't know what those [] brackets do, but they have something to do with defining an array (or a matrix… I honestly have no clue yet).

Well, worse case scenario is that you're back to square one. I'm curious what the coding gods will recommend. :)

zoharl
12 December 2010, 05:29 PM
Few options:

1. Locate the problem better. Add:

print $each

to test that the loop actually executes.

2. Just try to use another renderer.

3. Code without a loop two renders. Maybe the redo doesn't do the trick.

4. See if you have access to the code of the render itself to debug it with prints.

cgbeige
12 December 2010, 06:30 PM
ok - I found out the problem. It was the -text field name. It was the same for all objects so it was overwriting the file. Now it works :

http://grab.by/grabs/3355c67e278377d777a596658fdb6f4c.png

The final code looks like this:

$mySelection = `ls -sl`;

for($each in $mySelection)
{
select -r $each;
textFieldGrp -e -text $each -forceChangeCommand vrayFileNamePrefix;
RedoPreviousRender;
print ($each + "baked\n");
}

pixelranger
12 December 2010, 10:22 PM
That 'shouldn't' work.
I tried it and it DID work, but be mindful that, like onetime said, you should declare your variable with brackets '[]'. That tells Maya that you're expecting an array.
Also it is alway good practice to declare the variable data type:
string $mySelection[] = `ls -sl`;

You're going to run into a lot of "// Error: Line 1.23: Cannot cast data of type string to string[]. //" - errors if you don't, plus it is easier for you to keep an overview over your own variables.
There is a big difference between a string and a string array.

CGTalk Moderation
12 December 2010, 10:22 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.


1