do something for each selected object in array?

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Search this Thread Display Modes
Old 12 December 2010   #1
do something for each selected object in array?

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;

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.

Last edited by cgbeige : 12 December 2010 at 05:46 PM.
Old 12 December 2010   #2
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.
Old 12 December 2010   #3
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.
Old 12 December 2010   #4
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 :

The final code looks like this:

$mySelection = `ls -sl`;

for($each in $mySelection)
select -r $each;
textFieldGrp -e -text $each -forceChangeCommand vrayFileNamePrefix;
print ($each + "baked\n");
Old 12 December 2010   #5
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.
blog ----- portfolio
Old 12 December 2010   #6
Thread automatically closed

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.
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright ©2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 04:43 PM.

Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.