Sunday, September 29, 2013

Save and Reload Selection [Maya Python]

This is a Python script for Maya that can save your selection and reload it at a later point in your session.
 ## --------------------------------------------------------------------  
 """   
 Title: AC_RememberSelection.py  
 Author: Andrew Kin Fun Chan  
 Email: AndrewChan1985@gmail.com  
 Date: Sept 2013  
 Version: 1.0  
 Compatibility: Maya 2011+ (It will probably work in older versions as well. Contact me if you have issues.)  
 Function:        Commands:  
 Load Selection:     reloadSel = AC_RememberSelection(1)  
 Reload Selection:    AC_RememberSelection(0)  
 """  
 ## --------------------------------------------------------------------  
 import maya.cmds as mc  
 def AC_RememberSelection(x):  
   if x == 1:  
     print 'Storing Selection'  
     loadSel = mc.ls(sl=True)  
   else:  
     print 'Reloading Selection.'  
     mc.select (reloadSel)  
   return (loadSel)  

Saturday, September 28, 2013

Zero Transforms with Groups. [Maya Python]

When it comes to rigging, it's important to keep controllers clean while keeping it's orientation. Since freeze transformations destroys the orientation data, this is a little script I wrote to zero out object transforms using groups.

Video Demo:


Source Code:

 ## --------------------------------------------------------------------  
 """   
 Title: AC_ZeroTransformsWithGroup.py  
 Author: Andrew Kin Fun Chan  
 Email: AndrewChan1985@gmail.com  
 Date: Sept 2013  
 Version: 1.0  
 Compatibility: Maya 2011+ (It will probably work in older versions as well. Contact me if you have issues.)  
 Install Instructions:  
      1. Copy the mel script (ZeroTransformsWithGroup.py) to your local user/scripts folder:  
        Example path to icons folder:   
        C:\Documents and Settings\*USERNAME*\My Documents\maya\#.#\scripts        
      2. Copy the icon file (ZeroTransformsWithGroup.png) to your user/prefs/icons folder:  
           Example path to icons folder:   
           C:\Documents and Settings\*USERNAME*\My Documents\maya\#.#\prefs\icons        
        3. Then type the below code into a Maya Python tab:                                 
           import AC_ZeroTransformsWithGroup as zero  
           zero.AC_ZeroTransformsWithGroup()  
      4. Create the command on the shelf and so you have a button.   
       You should now have a new shelf button.   
       When you click the button it will create a group above   
       your selection to zero out the transforms.  
 Description:  
     A script that will take your selection and zero out the   
     transforms by putting an empty group above it with matching  
     transforms. This script only works on single selection.   
 """  
 ## --------------------------------------------------------------------  
 import maya.cmds as mc  
 def AC_ZeroTransformsWithGroup():  
   #Getting the Long and Short names of my selection.  
   zeroSelection = mc.ls (sl = True, long = True)  
   zeroSelectionShort = mc.ls (sl = True, shortNames = True)  
   #Check if anything is referenced.  
   if zeroSelection == None:  
     print 'Please select something with transforms to zero out.'  
   else:  
   #Check if list is empty.  
     if not zeroSelection:  
       print 'Please select something with transforms to zero out.'  
     else:  
       print 'cool'  
       for obj in enumerate(zeroSelection):  
         i = obj[0]  
         zeroParent = mc.listRelatives (obj[1], parent = True)  
   #Create the empty group respecting the hierarchy. Checks to see if there is a parent or not.  
         if zeroParent == None:  
           mc.group ( obj[1], n = (zeroSelectionShort[i] + '_ZERO'), em = True, r = True, p = obj[1])  
           mc.parent ((zeroSelectionShort[i] + '_ZERO'), w = True)  
           mc.parent ( obj[1], (zeroSelectionShort[i] + '_ZERO'))  
           print 'Created empty zero group in world space.'  
         else:  
           mc.group ( obj[1], n = (zeroSelectionShort[i] + '_ZERO'), em = True, r = True, p = obj[1])  
           mc.parent ( (obj[1] + '|' + zeroSelectionShort[i] + '_ZERO'), zeroParent[i] )  
           mc.parent ( obj[1], (zeroSelectionShort[i] + '_ZERO'))  
           print 'Created empty zero group hierarchy.'