View Full Version : getting [0] from an array

08 August 2006, 02:16 AM
I believe I have a reasonably simple question for everyone here...

I need to run a loop that connect attributes from the first selection in an array to all of the other elements in the array.

$sel = `ls -sl`;

for ($each in $sel)

for $each[0]
//dont do anything.

for $each[1]
//connect $each[0] to $each[1];

for $each[2]
//connect $each[0] to $each[1];

and so on... Is anyone able to point me into the right direction?

Mike R

08 August 2006, 06:00 AM
that isnt proper syntax for a For statement... your going to have to put a loop inside a loop that makes sure you arent connecting the attribute to itself.
try this:

string $sel[] = `ls -sl`;
for($item in $sel)
//now we have one element
//lets loop through all elements and connect this one to it
for($otherItem in $sel)
//lets make sure the other element isnt the one we are connecting to
if($otherItem != $item)
//if it isnt hook it up here
//connect $otherItem to $item in some way

hopefully this works, I didnt test it just freehand, but it should.

08 August 2006, 06:06 AM
This is how I would approach it.

//create some nodes.
$node[0] = `shadingNode -asTexture bulge`;
$node[1] = `shadingNode -asTexture checker`;
$node[2] = `shadingNode -asTexture checker`;
$node[3] = `shadingNode -asTexture checker`;
$node[4] = `shadingNode -asTexture checker`;
$node[5] = `shadingNode -asTexture checker`;
select -r $node;

//connect the nodes.
string $selection[] = `ls -sl`;
for($counter = 0; $counter < (size($selection) - 1); $counter++)
connectAttr -f ($selection[0] + ".outColor") ($selection[($counter + 1)] + ".color1");

08 August 2006, 05:03 PM
Thanks for the help guys....! I knew there had to be an easy way to do it. It works like a charm now.

**saved to my special MEL notes area

CGTalk Moderation
08 August 2006, 05:03 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.