— Sphere Intersect in Maya/MEL

Function to return location of intersect with poly mesh and spherical object moving in the positive direction on the Y axis.

mel source

global proc intersectSphereY()
{
    print(". . . . . go go go\n");

int $iter = 50;
float $start[3] = {0, -0.5, 0};
float $limit[3] = {0, 5.0, 0};
    $mesh = "test_mesh";    
$tmpCN = "cpom";


$obj = "rod1";
    float $radius = 0.5;
    float $curPos[3] = {0, 0, 0};

for ($i = 0; $i <= $iter; $i++)
{    
    $mesh = "test_mesh";
    $shape = `listRelatives -shapes $mesh`;

    createNode -n $tmpCN closestPointOnMesh;
    connectAttr -f ($shape[0] + ".outMesh") ($tmpCN + ".inMesh");
    setAttr ($tmpCN + ".inPosition") $curPos[0] $curPos[1] $curPos[2];

    $cpom = `getAttr ($tmpCN + ".position")`;

    if ( pointDist($curPos, $cpom) <= $radius ) {
        return ". . bonk\n";
    }

    $curPos[1] = ($limit.y) / $iter * $i;
    setAttr ($obj + ".translateY") $curPos[1];

    delete $tmpCN;
}
return ". nope\n";

}

global proc float pointDist(float $p1[], float $p2[]) { return sqrt( (($p1[0] - $p2[0]) * ($p1[0] - $p2[0])) + (($p1[1] - $p2[1]) * ($p1[1] - $p2[1])) + (($p1[2] - $p2[2]) * ($p1[2] - $p2[2]))); }

intersectSphereY;

— MEL Notepad

mel notepad with various code snippets

process selection list

string $select[] = ls -sl;
for ( $node in $select ) // process each
{
   /* ... */
}


if node exists

string $node = "object";
if ( objExists $node )
{
   // The node exists
}

regexp

Strip component

string $node = "pCube1.f[2]";
string $no_component = match "^[^\.]*" $node;
// Result: "pCube1" //


Extract component or attribute, with '.'

string $node = "pCube1.f[2]";
string $component = match "\\..*" $node;
// Result: ".f[2]" //

string $nodeAttr = "blinn1.color"; string $attrName = match "\\..*" $nodeAttr; // Result: ".color" //


Extract attribute name, without '.'

string $node = "pCube1.f[2]";
string $component = `substitute "^[^.]*\\." $node ""`;
// Result: "f[2]" //

string $nodeAttr = "blinn1.color";
string $attrName = `substitute "^[^.]*\\." $nodeAttr ""`;
// Result: "color" //


Extract parent UI control from full path

string $uiControl = "OptionBoxWindow|formLayout52|formLayout55|button6";
string $uiParent = substitute "|[^|]*$" $uiControl "";
// Result: OptionBoxWindow|formLayout52|formLayout55 //


Strip trailing Line Break (\n), if any.

This is useful when processing text input read from a file using fgetline.

string $input = "line\n";
$string $line = match "^[^(\r\n)]*" $input;
// Result: "line" //


Extract directory from path.

Keep the trailing slash for ease of use.

string $path = "C:/AW/Maya5.0/bin/maya.exe";
string $dir = match "^.*/" $path;
// Result: "C:/AW/Maya5.0/bin/"


Extract file from path

string $path = "C:/AW/Maya5.0/bin/maya.exe";
string $filepart = match "[^/\\]*$" $path;
// Result: "maya.exe"


Strip numeric suffix

string $node = "pCube1|pCubeShape223"; string $noSuffix = match ".*[^0-9]" $node; // Result: "pCube1|pCubeShape"


Extract numeric suffix

string $node = "pCube1|pCubeShape223";
string $suffix = match "[0-9]+$" $node;
// Result: "223" //


Extract short name of DAG or control path

string $dagPath = "pCube1|pCubeShape223";
string $shortName = match "[^|]*$" $dagPath;
// Result: pCubeShape223 //

other reference

↑ index