[MEL] "No object matches name:" in a loop

string $selection[];
$selection=ls-sl;
print $selection[0];

for ($i=0; $i<4; $i++)
{
float $rot[] = xform -q -ro $selection[$i];
}

its a simple script however it can’t find $selection for some odd reason or it won’t if you guys could give me a few pointers it woudl be much apperciated

// Error: line 3: No object matches name: // =|

This would work if your selection size == 4, else it will try storing rotation values for objects that doesn’t exist in the array yet, or only store the rotation of the first 4 objects in the array anyway.

Add a size checker and this should work better.

int $selSize = size($selection);
for ($i=0; $i<$selSize; $i++)

Cheers

[QUOTE=Emil Claeson;16236]

int $selSize = size($selection);
for ($i=0; $i<$selSize; $i++)
[/QUOTE]

The ‘size’ cmd can be simplified down to be in line although arguable slows things down as
it would check the size of the array at each pass, however… if an array was shrinking this can be useful.
Also declare your array outside the loop if you need the returned data outside the loop.

float $rot ;
for ($i=0; $i<size $selection; $i++)
{
$rot = xform -q -ro $selection[$i];
}
print $rot;

Along these lines size $myArray can be used to increment an array size and add a new element to the array, a little thought will tell you why this works, think about what size returns (an array of size/length 1 has an element at index [0]) :

$myArray[size $myArray]= $addme;

finally, mels great and fast an all but python is much easier on the mind and has longer term benefits, however mel is still handy in heavy iteration places.

You could also use the “objExists” command to test existence before doing the xform

In python this would be something like :


import maya.cmds as cmds
selection = cmds.ls(selection=True)
for obj in selection :
    if cmds.objExists(obj) :
        print cmds.xform(obj, query=True, rotation=True)

Also if i’m not mistaken querying the rotate attribute using getAttr is much faster than xform if you don’t need specific options !