Main
|
Resume
|
Showreel
|
Blog
|
Script
|
Contact
connexion
go to footer
expand all
|
collapse all
languages
class
All
JScript|XSI
Python|XSI
JScript|WEB
Python|Maya
All
Memo
learning
DDD
php
tool
keyboard tooltips
rig
ch4_Bake_Selected_UI()
- tool - felixlecha
import maya.cmds as mc ''' author : felixlechA - Bake selected object animation on a locator and reApply animation on object - Copy selected object animation on a locator and paste animation on object - Snapshot the current pose of selected obj on a locator ''' #------------------------------------------------------------------------------ # Commun def #------------------------------------------------------------------------------ def ch4_CheckCreate_Group( sName ): ''' Check if a group exist and Create it if not exist --- sGroupName : string | Define the name of Group to check existance ''' # Test if RIG_TRASH exist bRT_Exist = mc.objExists(sName) # If not Exist we create it if not bRT_Exist: mc.group( em=True, name=sName ) return sName def ch4_CreateLocator( sName=None, sParent=None, ws=False, lPos=[0,0,0], lRot=[0,0,0], lScl=[1,1,1] ): ''' Create Locator --- sName : string | The Locator Name sParent : string | The Parent name lPos : list | [0,0,0] A three input list define the Translation lRot : list | [0,0,0] A three input list define the Rotation lScl : list | [1,1,1] A three input list define the Scale ''' sCurrent = mc.spaceLocator() mc.xform( sCurrent, ws=ws, t=lPos, ro=lRot, s=lScl ) if sParent: mc.parent( sCurrent, sParent ) if sName: mc.rename(sCurrent, sName ) mc.setAttr( sName + '.v', lock=True, keyable=False, channelBox=False ) return sName def ch4_GetTransform( inObj ): ''' Get the transform of the input object and return Transform in a dictionnary ''' dDict = dict() dDict['pos'] = mc.xform( inObj, q=True, ws=True, t=True ) dDict['rot'] = mc.xform( inObj, q=True, ws=True, ro=True ) dDict['scl'] = mc.xform( inObj, q=True, s=True, r=True ) return dDict def ch4_SetTransform( inObj, ws=False, lPos=[0,0,0], lRot=[0,0,0], lScl=[1,1,1] ): ''' Set transform, by default reset Transform ''' mc.xform( inObj, ws=ws, t=lPos, ro=lRot, s=lScl ) return inObj def ch4_Match_To_Target( inObj, inTarget, ws=True, bTranslate=True, bRotate=True, bScale=True): ''' Match inObj to inTarget ''' dTrans_Target = ch4_GetTransform( inTarget ) if bTranslate: dTrans_Object = ch4_GetTransform( inObj ) ch4_SetTransform( inObj, ws=ws, lPos=dTrans_Target['pos'], lRot=dTrans_Object['rot'], lScl=dTrans_Object['scl'] ) if bRotate: dTrans_Object = ch4_GetTransform( inObj ) ch4_SetTransform( inObj, ws=ws, lPos=dTrans_Object['pos'], lRot=dTrans_Target['rot'], lScl=dTrans_Object['scl'] ) if bScale: dTrans_Object = ch4_GetTransform( inObj ) ch4_SetTransform( inObj, ws=ws, lPos=dTrans_Object['pos'], lRot=dTrans_Object['rot'], lScl=dTrans_Target['scl'] ) return inObj def ch4_SetKeyFrame( inObj, bTranslate=True, bRotate=True, bScale=True ): ''' Key Flag Transform ''' if bTranslate: mc.setKeyframe( inObj, at='translate' ) if bRotate: mc.setKeyframe( inObj, at='rotate' ) if bScale: mc.setKeyframe( inObj, at='scale' ) return inObj def ch4_Bake_Obj(obj, time=None, merge_anim=False): ''' bake given obeject over time. obj : list() time : (int, int) ''' #--- filter list to bake obj = list(set(obj)-set([None])) #--- get time range if not time : time = (mc.playbackOptions(q=True, min=True), mc.playbackOptions(q=True, max=True)) #ch4_Get_Selected_Anim_Range() all_layers = mc.ls(type = 'animLayer') or list() mc.refresh(su=True) if merge_anim : if len(all_layers)>1 : print '--- MERGE ANIM LAYER : %s'%all_layers mergeAnimLayers(items=all_layers) mc.bakeResults( obj, t=time, sampleBy=1 ,simulation=True , disableImplicitControl=True, preserveOutsideKeys=False, sparseAnimCurveBake=False, removeBakedAttributeFromLayer=False, bakeOnOverrideLayer=False, minimizeRotation=True,controlPoints=False,shape=True) mc.refresh(su=False) def ch4_copy_key( source, target, attribut=None ): ''' ''' if not attribut: mc.copyKey( source ) else: mc.copyKey( source, at=attribut ) mc.pasteKey( target, o='replace') #------------------------------------------------------------------------------ # Bake Animation in WS #------------------------------------------------------------------------------ def ch4_Animation_Bake_Copy_exe(*args): ''' Bake the animation of selected obj on a locator in WS ''' sLog = '[^^] Bake Selected' # Get Current selection cSelection = mc.ls( sl = True ) # If selection is empty abord if not cSelection: sLog = '[^^] Selection is empty !\n > Select some ctrls to bake on a locator' print( sLog ) mc.headsUpMessage( sLog, verticalOffset=350, time=2.0 ) return # Create a new group to Store the Objects sRoot = ch4_CheckCreate_Group( 'BAKE_ANIM_TRASH' ) for sSel in cSelection: # Rename to remove NameSpace sName = str(sSel).replace( ':', '_0_' ) # Create locator # Test if locator already exist bBake_Obj_Exist = mc.objExists('bake___' + sName ) # If not Exist we create it sLocator = 'bake___' + sName if not bBake_Obj_Exist: ch4_CreateLocator( sName=sLocator ,sParent = sRoot) else: mc.cutKey( sLocator ) # Create Cstr sScale_Cstr = mc.scaleConstraint( sSel, sLocator ) sParent_Cstr = mc.parentConstraint( sSel, sLocator ) # Bake Locator ch4_Bake_Obj( [sLocator] ) # Remove Cstr mc.delete( sScale_Cstr ) mc.delete( sParent_Cstr ) sLog = sLog + '\n > %s was bake on a locator' %(sSel) # Print log print( sLog ) mc.headsUpMessage( sLog, verticalOffset=350, time=5.0 ) def ch4_Animation_Bake_Paste_call(*args): ''' ''' # Get Values from UI bBakeMode_T = mc.checkBox( 'cb_BakeMode_T', q=True, v=True ) bBakeMode_R = mc.checkBox( 'cb_BakeMode_R', q=True, v=True ) bBakeMode_S = mc.checkBox( 'cb_BakeMode_S', q=True, v=True ) ch4_Animation_Bake_Paste_exe( bT=bBakeMode_T, bR=bBakeMode_R, bS=bBakeMode_S ) def ch4_Animation_Bake_Paste_exe( bT=True, bR=True, bS=True ): ''' ''' sLog = '[^^] Reapply Bake on Object :' # Test if no Transform are checked if not bT and not bR and not bS: sLog = sLog + 'No transform checked' print( sLog ) mc.headsUpMessage( sLog, verticalOffset=350, time=2.0 ) return # Test if the group exist bBake_Grp_Exist = mc.objExists( 'BAKE_ANIM_TRASH' ) if not bBake_Grp_Exist: sLog = sLog + ' > No Object found to Bake' print( sLog ) mc.headsUpMessage( sLog, verticalOffset=350, time=2.0 ) return lBake_Locator = mc.listRelatives( 'BAKE_ANIM_TRASH', c=True, type='transform' ) lAttribute = [] if bT: lAttribute = lAttribute + [ 'translateX', 'translateY', 'translateZ' ] if bR: lAttribute = lAttribute + [ 'rotateX', 'rotateY', 'rotateZ' ] if bS: lAttribute = lAttribute + [ 'scaleX', 'scaleY', 'scaleZ' ] for sLocator in lBake_Locator: sName = sLocator sName = str(sName).replace( 'bake___', '' ) sName = str(sName).replace( '_0_', ':' ) # Test if the group exist bObj_Exist = mc.objExists( sName ) if not bObj_Exist or not 'bake___' in sLocator: if 'bake___' in sLocator: sLog = sLog + '\n > No object corresponding to %s was found' %(sName) pass else: mc.cutKey( sName , attribute = lAttribute, option='keys' ) # Create Cstr if bS: sScale_Cstr = mc.scaleConstraint( sLocator, sName ) if bT and bR: sParent_Cstr = mc.parentConstraint( sLocator, sName ) elif bT and not bR: sParent_Cstr = mc.parentConstraint( sLocator, sName, sr=['x','y','z'] ) elif not bT and bR: sParent_Cstr = mc.parentConstraint( sLocator, sName, st=['x','y','z'] ) # Bake Object ch4_Bake_Obj( [sName] ) # Remove Cstr if bS: mc.delete( sScale_Cstr ) if bT or bR: mc.delete( sParent_Cstr ) sLog = sLog + '\n > %s was bake based on the locator' %(sName) # Print log print( sLog ) mc.headsUpMessage( sLog, verticalOffset=350, time=5.0 ) #------------------------------------------------------------------------------ # Copy Paste Animation #------------------------------------------------------------------------------ def ch4_Animation_Copy_exe(*args): ''' Copy the animation of selected obj on a locator ''' sLog = '[^^] Bake Selected' # Get Current selection cSelection = mc.ls( sl = True ) # If selection is empty abord if not cSelection: sLog = '[^^] Selection is empty !\n > Select some ctrls to Copy Animation on a locator' print( sLog ) mc.headsUpMessage( sLog, verticalOffset=350, time=2.0 ) return # Create a new group to Store the Objects sRoot = ch4_CheckCreate_Group( 'BAKE_ANIM_TRASH' ) for sSel in cSelection: # Rename to remove NameSpace sName = str(sSel).replace( ':', '_0_' ) # Create locator # Test if locator already exist bAnim_Obj_Exist = mc.objExists('anim___' + sName ) # If not Exist we create it sLocator = 'anim___' + sName if not bAnim_Obj_Exist: ch4_CreateLocator( sName=sLocator ,sParent = sRoot) else: mc.cutKey( sLocator ) #--- Copy Anim on Locator ch4_copy_key( source=sSel, target=sLocator ) sLog = sLog + '\n > %s animation was copy on a locator' %(sSel) # Print log print( sLog ) mc.headsUpMessage( sLog, verticalOffset=350, time=5.0 ) def ch4_Animation_Paste_call(*args): ''' ''' # Get Values from UI bAnimMode_T = mc.checkBox( 'cb_AnimMode_T', q=True, v=True ) bAnimMode_R = mc.checkBox( 'cb_AnimMode_R', q=True, v=True ) bAnimMode_S = mc.checkBox( 'cb_AnimMode_S', q=True, v=True ) ch4_Animation_Paste_exe( bT=bAnimMode_T, bR=bAnimMode_R, bS=bAnimMode_S ) def ch4_Animation_Paste_exe( bT=True, bR=True, bS=True ): ''' ''' sLog = '[^^] Paste Animation on Object :' # Test if no Transform are checked if not bT and not bR and not bS: sLog = sLog + 'No transform checked' print( sLog ) mc.headsUpMessage( sLog, verticalOffset=350, time=2.0 ) return # Test if the group exist bBake_Grp_Exist = mc.objExists( 'BAKE_ANIM_TRASH' ) if not bBake_Grp_Exist: sLog = sLog + ' > No Object found to paste animation' print( sLog ) mc.headsUpMessage( sLog, verticalOffset=350, time=2.0 ) return lBake_Locator = mc.listRelatives( 'BAKE_ANIM_TRASH', c=True, type='transform' ) lAttribute = [] if bT: lAttribute = lAttribute + [ 'translateX', 'translateY', 'translateZ' ] if bR: lAttribute = lAttribute + [ 'rotateX', 'rotateY', 'rotateZ' ] if bS: lAttribute = lAttribute + [ 'scaleX', 'scaleY', 'scaleZ' ] for sLocator in lBake_Locator: sName = sLocator sName = str(sName).replace( 'anim___', '' ) sName = str(sName).replace( '_0_', ':' ) # Test if the obj exist bObj_Exist = mc.objExists( sName ) if not bObj_Exist or not 'anim___' in sLocator: if 'anim___' in sLocator: sLog = sLog + '\n > No object corresponding to %s was found' %(sName) pass else: print lAttribute mc.cutKey( sName , attribute = str(lAttribute), option='keys' ) print 'delete keys' ch4_copy_key( source=sLocator, target=sName, attribut=lAttribute ) sLog = sLog + '\n > %s animation was paste based on the locator' %(sName) # Print log print( sLog ) mc.headsUpMessage( sLog, verticalOffset=350, time=5.0 ) #------------------------------------------------------------------------------ # SnapShot pose #------------------------------------------------------------------------------ def ch4_Pose_Copy_exe(*args): ''' Snapshot the current pose of selected obj on a locator ''' sLog = '[^^] SnapShot Pose Selected' # Get Current selection cSelection = mc.ls( sl = True ) # If selection is empty abord if not cSelection: sLog = '[^^] Selection is empty !\n > Select some ctrls to SnapShot Pose on a locator' print( sLog ) mc.headsUpMessage( sLog, verticalOffset=350, time=2.0 ) return # Create a new group to Store the Objects sRoot = ch4_CheckCreate_Group( 'BAKE_ANIM_TRASH' ) for sSel in cSelection: # Rename to remove NameSpace sName = str(sSel).replace( ':', '_0_' ) # Create locator # Test if locator already exist bPlot_Obj_Exist = mc.objExists('pose___%s' %(sName)) # If not Exist we create it sLocator = 'pose___%s' %(sName) if not bPlot_Obj_Exist: ch4_CreateLocator( sName=sLocator ,sParent = sRoot) else: mc.cutKey( sLocator ) # Create Cstr sScale_Cstr = mc.scaleConstraint( sSel, sLocator ) sParent_Cstr = mc.parentConstraint( sSel, sLocator ) # Key Locator mc.setKeyframe( sLocator ) # Remove Cstr mc.delete( sScale_Cstr ) mc.delete( sParent_Cstr ) sLog = sLog + '\n > %s pose was SnapShot on a locator' %(sSel) # Print log print( sLog ) mc.headsUpMessage( sLog, verticalOffset=350, time=5.0 ) def ch4_Pose_Paste_call(*args): ''' ''' # Get Values from UI bPoseMode_T = mc.checkBox( 'cb_PoseMode_T', q=True, v=True ) bPoseMode_R = mc.checkBox( 'cb_PoseMode_R', q=True, v=True ) bPoseMode_S = mc.checkBox( 'cb_PoseMode_S', q=True, v=True ) ch4_Pose_Paste_exe( bT=bPoseMode_T, bR=bPoseMode_R, bS=bPoseMode_S ) def ch4_Pose_Paste_exe( bT=True, bR=True, bS=True ): ''' ''' sLog = '[^^] Reapply Pose on Object :' # Test if no Transform are checked if not bT and not bR and not bS: sLog = sLog + 'No transform checked' print( sLog ) mc.headsUpMessage( sLog, verticalOffset=350, time=2.0 ) return # Test if the group exist bBake_Grp_Exist = mc.objExists( 'BAKE_ANIM_TRASH' ) if not bBake_Grp_Exist: sLog = sLog + ' > No Object found to paste' print( sLog ) mc.headsUpMessage( sLog, verticalOffset=350, time=2.0 ) return lPose_Locator = mc.listRelatives( 'BAKE_ANIM_TRASH', c=True, type='transform' ) lAttribute = [] if bT: lAttribute = lAttribute + [ 'translateX', 'translateY', 'translateZ' ] if bR: lAttribute = lAttribute + [ 'rotateX', 'rotateY', 'rotateZ' ] if bS: lAttribute = lAttribute + [ 'scaleX', 'scaleY', 'scaleZ' ] for sLocator in lPose_Locator: sName = sLocator sName = str(sName).replace( 'pose___', '' ) sName = str(sName).replace( '_0_', ':' ) # Test if the group exist bObj_Exist = mc.objExists( sName ) if not bObj_Exist or not 'pose___' in sLocator: if 'pose___' in sLocator: sLog = sLog + '\n > No object corresponding to %s was found' %(sName) pass else: # Match Object to Target ch4_Match_To_Target( sName, sLocator, ws=True, bTranslate=bT, bRotate=bR, bScale=bS) # Key Object ch4_SetKeyFrame( inObj=sName, bTranslate=bT, bRotate=bR, bScale=bS ) sLog = sLog + '\n > %s was key based on the locator' %(sName) # Print log print( sLog ) mc.headsUpMessage( sLog, verticalOffset=350, time=5.0 ) #------------------------------------------------------------------------------ # Clean System #------------------------------------------------------------------------------ def ch4_Clean_System_exe(*args): ''' Remove the Bake Anim Object to clean the scene ''' sLog = '[^^] Clean Bake System :' # Test if the group exist bBake_Grp_Exist = mc.objExists( 'BAKE_ANIM_TRASH' ) if bBake_Grp_Exist: mc.delete( 'BAKE_ANIM_TRASH' ) sLog = sLog + '\n > BAKE_ANIM_TRASH Remove' else: sLog = sLog + '\n > scene already clean :)' print( sLog ) mc.headsUpMessage( sLog, verticalOffset=350, time=5.0 ) #------------------------------------------------------------------------------ # UI #------------------------------------------------------------------------------ def ch4_Bake_Selected_UI(): ''' Interface UI ''' # Define new Window name sWindow = 'Bake_Animation_Tool' # Test if Window already existe, if is True Delete it if mc.window( sWindow, q=True, ex=True ): mc.deleteUI( sWindow ) # Create Window mc.window( sWindow, t='Bake Animation on Selected' ) # Create Layout sLay = mc.columnLayout( columnAttach=('both', 5), rowSpacing=3, adj=True, p = sWindow ) mc.frameLayout(bs='etchedIn', mh=5, mw=5, bgc=[0.26, 0.65, 0.24], l='Animation in WS', cll=True) mc.button( label='Bake', h=30, w=180, c=ch4_Animation_Bake_Copy_exe, annotation='Bake the animation of selected obj on a locator in WS' ) mc.rowLayout( numberOfColumns=3 ) mc.checkBox( 'cb_BakeMode_T', label='Translate', v=True ) mc.checkBox( 'cb_BakeMode_R', label='Rotate', v=True ) mc.checkBox( 'cb_BakeMode_S', label='Scale', v=True ) mc.setParent('..') mc.button( label='Apply', h=30, w=180, c=ch4_Animation_Bake_Paste_call, annotation='reApply locator baked animation on corresponding object in WS' ) mc.setParent('..') mc.separator( style='none', height=5 ) mc.frameLayout(bs='etchedIn', mh=5, mw=5, bgc=[0.26, 0.65, 0.24], l='Animation', cll=True, cl=True) mc.button( label='Copy', h=30, w=180, c=ch4_Animation_Copy_exe, annotation='Copy the animation of selected obj on a locator' ) mc.rowLayout( numberOfColumns=3 ) mc.checkBox( 'cb_AnimMode_T', label='Translate', v=True ) mc.checkBox( 'cb_AnimMode_R', label='Rotate', v=True ) mc.checkBox( 'cb_AnimMode_S', label='Scale', v=True ) mc.setParent('..') mc.button( label='Paste', h=30, w=180, c=ch4_Animation_Paste_call, annotation='reApply locator animation on corresponding object' ) mc.setParent('..') mc.separator( style='none', height=5 ) mc.frameLayout(bs='etchedIn', mh=5, mw=5, bgc=[0.9, 0.4, 0.1], l='Pose in WS', cll=True) mc.button( label='Copy', h=30, w=180, c=ch4_Pose_Copy_exe, annotation='Snapshot the current pose of selected obj on a locator in WS' ) mc.rowLayout( numberOfColumns=3 ) mc.checkBox( 'cb_PoseMode_T', label='Translate', v=True ) mc.checkBox( 'cb_PoseMode_R', label='Rotate', v=True ) mc.checkBox( 'cb_PoseMode_S', label='Scale', v=True ) mc.setParent('..') mc.button( label='Paste', h=30, w=180, c=ch4_Pose_Paste_call, annotation='reApply pose on corresponding object at the current frame in WS' ) mc.setParent('..') mc.separator( style='none', height=5 ) mc.button( label='Clean Scene', h=40, w=180, c=ch4_Clean_System_exe, annotation='Remove the Bake Anim Object to clean the scene' ) mc.separator( style='none', height=2 ) # Show Window mc.showWindow( sWindow ) ch4_Bake_Selected_UI()