Published using Google Docs
naGeneral.txt

//Author: Nathaniel Anozie

//ogbonnawork at gmail dot com

//

////description: Used for finding nodes like skinning blendshape etc

//for getting selections like get vertices from a polygon selection

//

//dependencies

//

//supports:

//

//How to Install: Source:

//  naGeneral.mel

//date created: Nov 23, 2011

//date last revised: Mar 09, 2012

//Modify at your own risk

/**given string array of node and node type return array for name first occurence of node type

@param string array of node ex: [blendNode, tweakNode, shapeNode]

@param string node type ex: tweak, blendShape

*/

global proc string[]

getFirstHasNodeType(string $nodeArray[], string $nodeType)

{

    string $result[];

    int $i= 0;

    string $tempType;//store node type of loop element,cleared on every step

    //loop nodes of input

   

    for ($i=0;$i< size($nodeArray); $i++)

    {

        $tempType = `nodeType $nodeArray[$i]`;

        //if found matching node return it

        if( $tempType == $nodeType  )

        {

            $result[0] = $nodeArray[$i];

            break;

        }

    }

   

    return $result;

}

/**return vertices given a polygon selection

@param string polygon name

@note often user may need to user vertices from a polygon selection

this script helps with returning that

@result string list all vertices of polygon

*/

global proc string[]

getVtxFromPoly(string $poly)

{

    string $result[];

    string $sel[] = `ls -sl`;

   

    //will be altering user scene

    //with constaint command

    //so added check necessary

   

    if(`objExists $poly`){

        select -cl;

        select -r $poly;

        polySelectConstraint -type 0x0001 -shell true -m 3;//mode no constraint used

        string $expanded[] = `ls -sl`;

        $result = `filterExpand -sm 31 -expand true`;

        polySelectConstraint -shell false;

       

        select -cl;

        select -r $sel;

    }

    else

    {

        warning("no poly exists");

    }

    return $result;

}

////get poly name given any poly component

/*supports any component vtx,face,edge,vFace ..

*/

global proc string[] getPolyFromComponent(string $component)

{

    string $result[];

   

    string $parts[];

    tokenize($component,".",$parts);

    $result[0] = $parts[0];  

   

    return $result;

}

/**given vertex return its skin cluster name

@param string a vertex name ex: afsf.vtx[5]

*/

global proc string[]

getSkinClusterGivenVertex(string $_vtx)

{

    string $sel[] = `ls -sl`;

    string $skinClusterName[];

   

    //if vertex is on scene

    if( `objExists $_vtx` == 1 )

    {

       

       

        //temporary fix to builtin error if no skin cluster on vertex

        //get the shape of this vertex check if shape has greater 2 items if it does we

        //assume can call skincluster cmd

        string $poly[] = getPolyFromComponent($_vtx);

        if(size($poly) > 0 ){

           

            select -clear;

            select -r $poly;

            pickWalk -d down;

            string $shapePoly[] = `ls -sl`;

            string $targets[] = `listConnections -s false -d true -c true $shapePoly[0]`;

            if( size($targets) > 2 ){

               

                string $jointArray[] = `skinCluster -q -inf $_vtx`;//get joint names using skin cluster node

               

                //if the vertex had multiple influences use the first one

                string $firstJoint = $jointArray[0];

                string $destinationList[] = `listConnections -s false -d true -c true $firstJoint`;

               

                $skinClusterName = getFirstHasNodeType($destinationList, "skinCluster");

            }

           

        }

       

        //restore user selection

        select -r $sel;  

       

    }

   

    return $skinClusterName;

}

/**give me the first index where input value matches input search array (returns int array)

@param $value           string  what to look for

@param $searchArray     string array

@note it behaves alot like pythons list.index(value) function, where search array acts as list

@note it returns empty array if no index match found

*/

global proc int[]

naIndex(string $value, string $searchArray[] )

{

    int $result[];

   

    //loop search array

    for($i=0; $i < size($searchArray); $i++){

        //if match found store index and break out of loop

        int $matchValue = strcmp($value, $searchArray[$i]);

        if(  $matchValue == 0 )

        {

            $result[0] = $i;

            break;

        }

    }

   

    return $result;

}

/////given cluster name and vertex selection, return subset of vertex selection that also has cluster name

/*

in making user selections in skinning

theres a chance that multiple skin clusters may be involved

this make sures a set of vertices all come from the same skin cluster

returning empty means none of vertex has the input argument skincluster name

*/

global proc

string[]

getVertexWithThisSkinCluster(string $_skinClusterName, string $_vtxArray[])

{

    string $result[];

    string $skinClusterName = $_skinClusterName;

    string $vtxArray[] = $_vtxArray;

   

    int $i=0;

   

    //loop vertices

    for($i=0; $i < size($vtxArray); $i++)

    {

        //should we return this vertex

        string $candidateVertex = $vtxArray[$i];

        string $skinClusterNameArray[] = getSkinClusterGivenVertex($candidateVertex );

        //vertex has same skin cluster

        if(  `strcmp $skinClusterNameArray[0] $skinClusterName` == 0 )

        {

            //so save it

            $result[size($result)] = $candidateVertex ;

        }

    }

   

    return $result;

}

/**get list all curves

@result string list all curves

*/

global proc string[] getListAllCurves(){

    string $allCurves[];

   

    //get all curve non transform

    string $allNonTransformCurves[] = getListAllObjectsByType(9);

   

    //get all curve transform

    //  some curves become a transform when an object is parented to it, like a joint parented to a curve

    string $allTransformCurves[] = transformsWithAChildShapeOfType("nurbsCurve");

   

    //glue names together

    appendStringArray($allCurves, $allNonTransformCurves, size($allNonTransformCurves) ) ;

    appendStringArray($allCurves, $allTransformCurves, size($allTransformCurves) ) ;

   

    return $allCurves;

}

//get transforms that has a specific type of child

/*

    nurbsCurve  youre interested in something like a curve s.t it has a joint as child, like for rigging

*/

global proc string[] transformsWithAChildShapeOfType(string $type){

   

    string $transformCurves[];

     //get list of all transform nodes

    string $allTransformNodes[] = getListAllObjectsByNodeType("transform");

   

    int $counterForTransformParent=0;

    int $counterForTransformChildren=0;

    int $shapeNodeChildIsFound = 0;

    string $transformParent;

    string $transformChildren[];

    string $transformChild;

    //loop names of transforms

    for($counterForTransformParent=0; $counterForTransformParent< size($allTransformNodes); $counterForTransformParent++)

    {

        $transformParent = $allTransformNodes[$counterForTransformParent];

        $transformChildren = `listRelatives -children -shapes $transformParent`;

       

        //if transform node has at least one child that is a shape node,

       

       

        if( size( $transformChildren ) > 0 )

        {

           

            //loop children of each transform

            while( ($counterForTransformChildren < size($transformChildren)) && ( $shapeNodeChildIsFound == 0) ){

               

                $transformChild = $transformChildren[$counterForTransformChildren];

                //and that shape node is a nurbs curve, the transform is a curve

               

                if( (`objectType -isType $type $transformChild `) == 1 )

                {

                    $shapeNodeChildIsFound = 1;

                    $transformCurves[ size($transformCurves) ] = $transformParent;

                    break;

                }

                $counterForTransformChildren++;

            }

           

        }

    }

   

    return $transformCurves;

}

/**get list all polygon mesh

@result string list all polygons in scene

*/

global proc string[] getListAllPolygonMesh(){

    string $allPolygonMesh[] = getListAllObjectsByType(12);

    return $allPolygonMesh;

}

/**get list all nurbs surfaces

@result string list all nurbs surfaces in scene

*/

global proc string[] getListAllNurbs(){

    string $allNurbs[] = getListAllObjectsByType(10);

    return $allNurbs;

}

/**get a list of all created objects by list

@param  a maya name indicates what thing is selected

@result string list of object names

@note    transform   any transform node

@note    joint       any joint

@note    lambert     any material

@note    lambert     any material

*/

global proc string[] getListAllObjectsByNodeType(string $type){

    if( ($type == "lambert") || ($type == "transform") || ($type == "joint") || ($type == "displayLayer") )

        { print(selectSuccessMessage()); }

    else{ error(selectErrorMessage()); }

    string $allObjectOfType[];

    string $allObjectOfType[] = `ls -type $type`;

    return $allObjectOfType;

}

/**get a list of all created objects by selection mask

@param selection mask

@note  12  polygons

@note 9   curve

@note 22  locators  

@note 0   ikhandles

@note 10 nurbs

*/

global proc string[] getListAllObjectsByType(int $mask_arg){

int $mask = $mask_arg;

if( ($mask == 12) || ($mask == 9) || ($mask == 22) || ($mask == 0) || ($mask == 10)  ){ print(selectSuccessMessage()); }

else{ exit(selectErrorMessage()); }

//store current selection

string $currentSelection[] = `ls -sl`;

//select every thing on scene

select -cl;

string $allSceneObjects[] = `ls`;

select $allSceneObjects;

//filter selection

string $filtered[] = `filterExpand -selectionMask $mask`;

//remove duplicate names

string $allPolygonObjects[] = stringArrayRemoveDuplicates($filtered);

//restore current selection

select $currentSelection;

return $allPolygonObjects;

}

//get list of all joint objects

//  no arguments needed

global proc string[] getListAllJoints(){

    string $allJoints[] = getListAllObjectsByNodeType("joint");

    return $allJoints;

}

//get list of all lambert materials

//  no arguments needed

global proc string[] getListAllCreatedLambertMaterials(){

   

    string $allLambertMaterial[];

    string $allLambert[] = getListAllObjectsByNodeType("lambert");

   

    //remove first element lambert1

    //first element is not always lambert1

   

    int $i = 0;

    for ($i=0; $i< size($allLambert); $i++)

    {

        if($allLambert[$i] != "lambert1"){

                     $allLambertMaterial[size($allLambertMaterial)] = $allLambert[$i];

             }

    }

   

   

    return $allLambertMaterial;

}

/**get list of all display layers

@results string list of layer names

*/

global proc string[] getListAllCreatedLayers(){

   

    string $allCreatedLayers[];

    string $allLayers[] = getListAllObjectsByNodeType("displayLayer");

   

   

    //remove first element defaultLayer

    //first element is not always defaultLayer

   

    int $i = 0;

    for ($i=0; $i< size($allLayers); $i++)

    {

        if($allLayers[$i] != "defaultLayer"){

                     $allCreatedLayers[size($allCreatedLayers)] = $allLayers[$i];

             }

    }

   

   

    return $allCreatedLayers;

}

/**get list of all material info

@param $mat string list of materials

@result string list correspond to input material and requested type

*/

global proc string []

getListAllMaterialInfo(string $mat[]){

    string $result[];

    $type = "materialInfo";

    $result = getListAllConnectOfName($mat,$type);

    return $result;

}

/**get list of all shading engine

@param $mat string list of materials

@result string list correspond to input material and requested type

*/

global proc string []

getListAllShadingEngine(string $mat[]){

    string $result[];

    $type = "shadingEngine";

    $result = getListAllConnectOfName($mat,$type);

    return $result;

}

/**get list of all things connected to node of given type both input and output

@param $mat string list of any transform: poly,nurb,null

@param $type string  of any connection type

@result string list correspond to input object and requested type

@note only works one level, that is if something is connected to thing connected to this it doesnt check that

*/

global proc string []

getListAllConnectOfName(string $object[],string $type){

    string $result[];

    $result = `listConnections -type $type $object`;

    return $result;

}

//get list of all group objects

//  no arguments needed

global proc string[] getListAllCreatedGroupObjects(){

   

    string $allGroupObjects[];

    string $allTransform[] = getListAllObjectsByNodeType("transform");

   

    //a group has no shape node as children, check this

    int $i = 0;

    for ($i=0; $i< size($allTransform); $i++)

    {

        if( size(`listRelatives - shapes $allTransform[$i]`) == 0){

                     $allGroupObjects[size($allGroupObjects)] = $allTransform[$i];

             }

    }

   

    return $allGroupObjects;

}

//error messaging

//  no arguments needed

global proc string selectSuccessMessage()

{

    string $message = "returning all objects\n";

    return $message;

}

//error messaging

//  no arguments needed

global proc string selectErrorMessage()

{

    string $message = "cannot return all objects - check argument for selection\n";

    return $message;

}