Tuesday, November 30, 2010

Mel : How to find center of 2 selected vertex


 given 2 vectors cast a point on the line.
so if we know 2 points out  in 3d space and if you want to shoot a point along the line anywhere in 3d space you can achieve this using vector equation of a line.you can also find center of 2 Selected vertex.



using vector equation of line
r = ro + a
a = t*(v)
r = ro + t*(v)

t*(v) will be a vector that lines along the line and it tells us how far from the original point that we should move. if t is positive we move away from the original point in direction of vector v and if t is negative we move away from the original point in the opposite direction of vector v. note vector v can be anywhere in 3D as long as it is parallal to a.

string $verts[] = `ls -sl -fl`;
vector $ro = `pointPosition $verts[0]`;
vector $r = `pointPosition $verts[1]`;
vector $v = $r - $ro;
float $result[] = $ro + (0.5*$v);
$sl = `spaceLocator`;
move $result[0] $result[1] $result[2] $sl;

above script will create a locator in the center of 2 selected vertex as i used t as 0.5 here.

imagine your first point is camera position and second point is 2d pattern looking thru camera. if you want to extend the ray from these 2 points far enough you can use above script with the t value whatever you like. you can go t over 1 so the third point goes beyond your second point.

Mel : How to return multiple values in procedure?

you can do this using string array

global proc string[] myproc() {
    string $r[];
    string $sel[] = `ls -sl`;
    float $pos[] = `xform -q -ws -t $sel[0]`;
    $r[0] = $sel[0];
    $r[1] = $pos[0];
    $r[2] = $pos[1];
    $r[3] = $pos[2];
   
    return $r;
}


myproc();


so for example if you want to store selected object's transform node and shape node name along with translate x,y,z in a string array, you can plug all of them in string array inside the proc.
here i am forcing my script to store values only if shape node is camera.

global proc string[] camtr(string $myselection[]) {
    string $return[];
    string $shp[] = `listRelatives -s $myselection[0]`;
                for ( $s in $shp )
                {
                string $cam = `nodeType $s`;
                    if ($cam == "camera")
                    {
                    print ( "\n YES " + $s + " is camera\n");
                    $pos =`xform -q -ws -t $myselection[0]`;
                    $return[0] = $myselection[0];
                    $return[1] = $shp[0];
                    $return[2] = $pos[0];
                    $return[3] = $pos[1];
                    $return[4] = $pos[2];
                    }
                    else
                    {
                    print ( "\n No " + $s + " is not a camera. It is a " + $cam +"\n");
                    }
            }

    return $return;
}

  
to see result of above proc

string $sel[] = `ls -sl -fl`;
string $b[] =camtr($sel);

Integration/Tracking Demo Reel 2018 on Youtube