Home Technical Talk

Help with mel - tokenizing selection

Alex_Andrews
polycounter lvl 2
Offline / Send Message
Pinned
Alex_Andrews polycounter lvl 2
Hi,

I'm working on a script to place cylinders on selected faces for booleans. I need to place the first selected object into a token so I can call it up, but am confused on how I can bring the selection into a string to tokenize between the "."

The script is meant to allow me to select adjacent components on a poly surface to create a cylinder on the the center of those selected pieces.

global proc Circle()
{
    string $array[] = `ls -selection`;
    print $array;
    for ($item in $array)
        {
        string $currentTool = `currentCtx`;
        setToolTo moveSuperContext;
        vector $pos = `manipMoveContext -q -p Move`;
        string $loc[] = `polyCylinder -r 1 -h 2 -sx 10 -sy 1 -sz 1 -ax 0 1 0 -rcp 0 -cuv 3 -ch 1 -n "cutter#"`;
        move -ws -a ($pos.x) ($pos.y) ($pos.z) $loc;
        setToolTo $currentTool;
        select $array;
        select -tgl $loc;
        normalConstraint -weight 1 -aimVector 0 1 0 -upVector 0 1 0 -worldUpType "scene";
        select $array;
        }
}

so I need to tokenize the selection so that the script only works on the first selected object (for example, if I have pSphere1 and pSphere2 selected, I need it to only work on pSphere1). I also need at the end to select the original selection again from components up, probably with a polyListcomponentConversion.

Does anyone have any pointers?

Replies

  • PolyHertz
    Options
    Offline / Send Message
    PolyHertz polycount lvl 666
    You probably want to use `ls -sl -fl` to make sure each item in the array has its own index before performing the loop. Tokenize works like this:

    $selectionArray = `ls -sl -fl`;
    string $newArray [];
    tokenize $selectionArray[0] "." $newArray;
    print $newArray;
  • Alex_Andrews
    Options
    Offline / Send Message
    Alex_Andrews polycounter lvl 2
    Does anyone have anything to isolate it to just 1 object? So if I choose some faces on psphere 1, but want to keep it to psphere1 and not have it include psphere 2 if selected to trap it, what do I do in the For/If command? And if anyone can help plug in a trap so it only works on component selection, that would also be fantastic

    global proc circleCut()
    {
        string $mySelection[] = `ls -selection -flatten` ;
        
        
        for ($item in $myselection);
                {
                if ( "." )
                
                }
        

        string $buffer[] ;
        int $numTokens = `tokenize $mySelection[0] "." $buffer` ;
        print $buffer
     
        string $currentTool = `currentCtx`;
        setToolTo moveSuperContext;
        vector $pos = `manipMoveContext -q -position Move`;
        string $loc[] = `polyCylinder -radius 1 -height 2 -subdivisionsX 10 -subdivisionsY 1 -subdivisionsZ 1 -axis 0 1 0 -rcp 0 -createUVs 3 -constructionHistory 0 -name "cutter#"`;
        move -worldSpace -absolute ($pos.x) ($pos.y) ($pos.z);
        string $cylinderCutter[] = `ls -selection`;
        setToolTo $currentTool;
        select $buffer[0];
        select -add $cylinderCutter[0];
        normalConstraint -weight 1 -aimVector 0 1 0 -upVector 0 1 0 -worldUpType "scene";       
        polyPerformBooleanAction 2 o 0;
    }
Sign In or Register to comment.