Wednesday, August 13, 2014

Some Maya scripting hints

Hi,
Hope you enjoy some of these scripting hints i wrote.

using wild card selection ex: to possibly remove skinned joints for face rigging
//select skinned joints, helpful to remove, does not support actually checking skin clusters
//assumes skin is in name of bound joints
global proc na_selectSkin(string $faceArea){
//ex: select all eye skin joints select "*eye*skin*";
select "*"+$faceArea+"*skin*";
select -r `ls -sl -type joint`; //need this so dealing with joints only
}
print("run this --\n"+"na_selectSkin(\"eye\")"); 
having animation graph editor be opened automatically, example seeing set driven key curve for face blendshapes for modification
//author: Nathaniel Anozie (ogbonnawork at gmail dot com)
//
//

//modify at own risk

// updated 08-13-2014 nate ----- initial commit


global proc na_showBSCurve( string $object[] ){
    
    //show blendshape curve given object hooked up to it
    //object -- animator control
    
    if(size($object) >= 1){
        string $result[] = {};
        
        //assumes straight forward to get input from animator control
        for($arg in $object){
            string $curves[] = {};
            $curves = `listConnections -plugs true -destination true -source false $arg`;
            $result = stringArrayCatenate($result,$curves);
        }
        
        //show graph of set driven key for editing
        select -r $result;
        GraphEditor;
        FrameSelected;
        fitPanel -selected;
    }
    else{print("Please select object hooked up to blendshape");}

}

//usage
//show blendshape node sdk curve for editing given a selected object hooked up to it
eval( "source \"na_showBSCurve.mel\";" );
string $sel[] = {};
$sel = `ls -sl`;
na_showBSCurve($sel); 
ability to quickly select all input of an object ex: easy export of selection including materials etc
 (before and after script)

 
#select all history and materials of selected, helpful for exporting in Maya
#author: Nathaniel Anozie (ogbonnawork at gmail dot com)
#
#
#Inspired by Alex Martelli, learning about expand list of lists in python from his online tutorials
#Inspired by Irakli Khakhviashvili, learning about selecting shaders from his online tutorials
#Inspired by Patrick Westerhoff, learning about list uniqueness in python from his online tutorials

#modify at own risk

# updated 08-13-2014 nate ----- initial commit

import maya.cmds as cmds

def na_niceSelect():
    """
    select all history and materials of selected, helpful for exporting in Maya
    """
    result = []
    
    
    sel = cmds.ls(selection = True)
    print "start --- calculating"
    tmpResult = []
    for _arg in sel:
        tmpResult.append( _arg )#important to have everything in lists
        #ex 'openJaw1:new_topology53'
    
        #compute past
        past = []
        past = cmds.listHistory(_arg)
        for _past in past:
            tmpResult.append(_past)  
            #if its a mesh also adds its materials
            shader = []
            shader = cmds.listConnections( _past, type = "shadingEngine")
            if shader is not None:
                tmpResult.append( shader )
    print "done --- calculating"                  
    #
    #concluding 
    #result = [item for sublist in tmpResult for item in sublist]  
    #result = list(set(result)) #remove possible duplicates
    #cmds.select(result, replace = True, ne = True) #selecting results

    #selecting
    cmds.select(clear = True)
    for _result in tmpResult:
        cmds.select(_result, add = True, ne = True) #ne is so can select shaders
        
    result = cmds.ls(selection = True)
    return result 
 
Hope this was helpful.
cheers,
Nate