PDA

View Full Version : Why does this half work?


animatedfox
07-13-2012, 02:27 AM
I am trying to figure out why this script only half works.
It runs through a random number of points and then stops working with the warning;

// Warning: line 7: Only one item can be selected. Using the first selection. //

I've tried it as a while loop and adding select -cl throughout...but don't know why it is clunking out at seemingly random points. I am sure there are better ways of doing this. And I am game for suggestions. What started as just playing with what I thought would be a good MEL teaching example script has started driving me crazy.
Any thoughts would be greatly appreciated.

{
// select verts and run

string $allVerts[] = `ls -sl`;
string $eachVert;
int $i = 1;

for ($eachVert in $allVerts)
{
//Get vertex world position
float $vertPos[] = `pointPosition -w $eachVert`;

//Create a cube for each vert
string $cube[] = `polyCube -name ("vertCube_"+$i+"_PLY")`;

//Sets the translates of the locators to the stored variables
setAttr ($cube[0] + ".translateX") $vertPos[0];
setAttr ($cube[0] + ".translateY") $vertPos[1];
setAttr ($cube[0] + ".translateZ") $vertPos[2];
$i++;
}
}

haggi
07-13-2012, 09:24 AM
What is line 7? Is it the line where you do `pointPosition`?

If this fails, I'd try to print the $eachVert so see why its more than one item.
Maybe it has something to do with the way maya lists the vertices. If you do a "ls -sl" with vertices, you will get something like vtx[1], vtx[234], vtx[234:456], where the last one contains a vertex range. But you can use the -fl Flag with ls what shuld flatten the selected vertices and outptus only single vertices. But maybe it's something else.

NaughtyNathan
07-13-2012, 09:26 AM
you are using `ls -sl` alone to list your selected vertices. This returns clumps as a single item (e.g. "pCube1.vtx[23:28]") and that's why it's failing, as you're passing multiple verts (as a clump) into the pointPosition command.

use `ls -sl -fl` or `filterExpand -sm 31` instead to get your selected verts as individual items.

For future it's always helpful to print out the variables you are using to sanity check what you have and what you get during script development (as MEL has no debugger).

animatedfox
07-13-2012, 12:55 PM
Brilliant!
Thanks so much for the tips.
Line 7 was the point position line...and flattening definitely did the trick.
Onward.

cgbeige
07-15-2012, 07:10 PM
ya, the filterexpand thing got me once too when I was making something for Facer. I made a script to check what type of component is selected and then expand the list accordingly:

global proc string determiner( string $compType )
{
string $namey[] = `ls -sl`;
string $buffer[];
$numTokens = `tokenize $namey[0] "." $buffer`;
string $bufferToo[];
$numTokensToo = `tokenize $buffer[1] "[" $bufferToo`;
//print $bufferToo[0];
string $componentTypey;
if ($bufferToo[0] == "vtx")
{
return "vtx";
}
else if ($bufferToo[0] == "e")
{
return "edge";
}
else if ($bufferToo[0] == "f")
{
return "face";
}
return $componentTypey;
}

then I run determiner on the selected component:

global string $compStore;
global string $selectedCompent[];
string $selectedCompent[] = `ls -sl`;
global string $expandedCmpArray[];

//if selected type is put through determiner
$compStore = `determiner $selectedCompent[0]`;
if ($compStore == "vtx" )
{
$expandedCmpArray = `filterExpand -sm 31 -expand true`;
$cmpCountt = (`size $expandedCmpArray`);
}
else if ($compStore == "edge" )
{
$expandedCmpArray = `filterExpand -sm 32 -expand true`;
$cmpCountt = (`size $expandedCmpArray`);
}
else if ($compStore == "face" )
{
$expandedCmpArray = `filterExpand -sm 34 -expand true`;
$cmpCountt = (`size $expandedCmpArray`);
}

maybe Nathan knows a better way.

NaughtyNathan
07-15-2012, 07:46 PM
I'm not sure I understand why you'd need or want to do this Dave. (did that sound like HAL? :p )

In Maya it is possible to select multiple types of components at the same time (i.e a face AND an edge) so even if your script needed to operate differently based on the type of selection you'd still have to prioritize it. filterExapand also allows you to specify multiple component types:

string $allSelComponents[] = `filterExpand -sm 31 -sm 32 -sm 34`;

// .. or operate based on type selected?
string $vert[] = `filterExpand -sm 31`;
string $edge[] = `filterExpand -sm 32`;
string $face[] = `filterExpand -sm 34`;
print "You have ";
if (size($vert)) // if any vertices are selected, do:
{
print "Vertices / ";
polySplitVertex $vert;
}
if (size($edge)) // if any edges are selected do:
{
print "Edges / ";
polySoftEdge $edge;
}
if (size($face)) // finally, if any faces are selected, do:
{
print "Faces ";
polyExtrudeFacet $face;
}
if ( !size($vert) && !size($edge) && !size($face) ) print "nothing ";
print "selected.\n";

CGTalk Moderation
07-15-2012, 07:46 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.