Pass array of arguments into command

Pass array of arguments into command

bphoenix22YC2
Explorer Explorer
1,284 Views
4 Replies
Message 1 of 5

Pass array of arguments into command

bphoenix22YC2
Explorer
Explorer

Hey folks, I'm new to Mel here so please excuse the beginner question. I'm writing a simple script to run polySeparate on the user's actively selected shells (returned from polyEvaluate).

 

What I'm stuck on is that I'm not able to get the -sss argument to accept a list, it seems to need an explicit index (or an explicit int more accurately). Looking at the output of the command executed manually, it adds a separate -sss flag for each selected shell, and each argument is a separate int. Since I want to run this command on all of $shells[], entering them separately obviously won't work. I haven't found a solution to this so far in the documentation, but I may be looking in the wrong way.

 

The following script will return an error, but if I replace $shells[] with $shells[0] it executes correctly, though not as I intend.

// Separate selected shells
{
int $shells[]; 
$shells = `polyEvaluate -as`;

string $a[];
$a = `polySeparate -sss $shells[] Bone_1_Start_Mesh`;
print($a);
}

Thanks in advance for any help!

 

P.S. Bone_1_Start_Mesh is just a test object, but will be replaced with a variable.

0 Likes
Accepted solutions (2)
1,285 Views
4 Replies
Replies (4)
Message 2 of 5

Kahylan
Advisor
Advisor
Accepted solution

Hi!

 

Flags in MEL can't change type, if they are designed to take a single value, they will only take a single value, if they are designed to take an array with 3 values they will only take an array with 3 values.

You can however, loop through your values like this:

{
int $Shells[]; 
$Shells = `polyEvaluate -as`;

string $a[];
string $obj = "pCube1";
for($s = 0; $s < `size $Shells`; $s++){
    print($s);  
    $a = `polySeparate -sss ($Shells[$s]-$s) $obj`; 
    $obj = $a[1];
    
}

}

I had to introduce the Mesh to separate from as it's own variable (in my case "pCube1" in yours "Bone_1_Start_Mesh"), since this value changes when the mesh gets separated. Also I had to indicate the shell that was separated as ($Shells[number of loop] - number of loop) because with every polySeparate the Shell ID drops by 1, since there is one Shell less in the Object.

 

I hope this helps!

 

0 Likes
Message 3 of 5

BurkhardRammner
Collaborator
Collaborator
Accepted solution

The correct way of doing what you want here is to create a string with your command, then evoking it using eval:

 

 

int $shells[] = polyEvaluate( "-as" );
string $sel[] = ls( "-sl" );

if( size($shells) && size($sel) )
{
    string $obj = plugNode( $sel[0] );
    string $c = "polySeparate ";
    for( $i=0; $i<size($shells); $i++ )
    {$c+=" -sss "+$shells[$i]+" ";}
    $c+=$obj;
    evalEcho( $c );
}

 

 

0 Likes
Message 4 of 5

bphoenix22YC2
Explorer
Explorer

Thanks Kahylan! This seems to work, though it is sort of the case I was hoping to avoid by passing a simpler argument. Seems like Mel isn't well set up for that unfortunately. I've had a lot of trouble with the polySeparate command being convoluted in general (as you mentioned, the object gets renamed, and there isn't a distinction in the output between the original and new meshes), and as a result I've reimplemented this in my script by duplicating the object entirely with a selection set, inverting the selection and deleting. It ends up seeming to be much simpler.

0 Likes
Message 5 of 5

bphoenix22YC2
Explorer
Explorer

Thanks Burkhard! I wasn't aware of the echo command, but it seems helpful more generally as well, so I appreciate the tip.

0 Likes