View Full Version : For Loop only works for 1st object

11 November 2011, 09:19 AM
It's me again :D

I'm sure there's a simple explanation for this.

I'm trying to query the translations of multiple items by using a for loop. It works for the first item, but it returns 0 0 0 for everything else. I haven't frozen transforms or anything, so I'm not sure what's going on.

Here's everything I have so far, but I've highlighted my problem area in red.


global proc getPos(){

//list everything selected

string $list[] = `ls -sl`;

//get first selected object or "source" object

string $sourceObj[] = `ls -sl -head 1`;

//get everything else or "target" objects

string $targets[] = stringArrayRemove($sourceObj, $list);

//get transformation info of source

vector $sourceTran = `xform -ws -q -translation $sourceObj`;

float $sourceTX = $sourceTran.x;

float $sourceTY = $sourceTran.y;

float $sourceTZ = $sourceTran.z;

//get roation info of source

vector $sourceRot = `xform -ws -q -rotation $sourceObj`;

float $sourceRX = $sourceRot.x;

float $sourceRY = $sourceRot.y;

float $sourceRZ = $sourceRot.z;

//get translation of all target objects

for ($i=0; $i < size($targets); $i++){

vector $targetTran[] = `xform -ws -q -translation $targets[$i]`;

//print ($targets[$i] + "\n");

print ($targetTran[$i] + "\n"); }


11 November 2011, 10:10 AM
You don't want to use the object's index ($i) to loop through the result, you should loop through the resulting array in order to print it (or simply print the result as it's a vector)

for ($i=0; $i < size($targets); $i++){
vector $targetTran[] = `xform -ws -q -translation $targets[$i]`;
print $targetTran;

11 November 2011, 01:57 PM
Yep, that was it! Thanks so much!

CGTalk Moderation
11 November 2011, 01:57 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.