Tasks/MatchAuxiliaryEffectors.py
 
 
 
Tasks/MatchAuxiliaryEffectors.py
# Copyright 2010 Autodesk, Inc.  All rights reserved.
# Use of this software is subject to the terms of the Autodesk license agreement 
# provided at the time of installation or download, or which otherwise accompanies
# this software in either electronic or hard copy form.
#
# Topic: Auxiliary effectors
#

from pyfbsdk import *
from pyfbsdk_additions import *

# Function
def IsAuxIKPivot(pModel):
    if pModel and pModel.Is(FBModelMarker_TypeInfo()):
        lIKSyncProp = pModel.PropertyList.Find("IKSync",False)
        lIKDrawLink = pModel.PropertyList.Find("DrawLink",False)
        if lIKSyncProp and lIKDrawLink and lIKSyncProp.Data and lIKDrawLink.Data:
            return True
    return False

# Clear all auxiliary on given effector 
def ClearAuxEffectors(pCharacter, pEffectorId):
    lAuxToClear = []
    for lSetId in FBEffectorSetID.values.values():
        # Default set should not be deleted.
        if lSetId == FBEffectorSetID.FBEffectorSetDefault: continue

        lAuxEffector = pCharacter.GetEffectorModel(pEffectorId,lSetId)
        if lAuxEffector: 
            lAuxToClear.append(lAuxEffector)
        else:  break
    
    # Clear the list
    for lModel in reversed(lAuxToClear):
        lModel.FBDelete()

# Match auxiliary on dst to src character for given effector
def MatchEffector(pSrcCharacter, pDstCharacter, pEffectorId):
    lDstEffectorModel = pDstCharacter.GetEffectorModel(pEffectorId)
    if lDstEffectorModel:
        ClearAuxEffectors(pDstCharacter,pEffectorId)
        for lSetId in FBEffectorSetID.values.values():
            if lSetId == FBEffectorSetID.FBEffectorSetDefault: continue
            
            lSrcAuxEffectorModel = pSrcCharacter.GetEffectorModel(pEffectorId,lSetId)
            if lSrcAuxEffectorModel:
                lIsAuxPivot = IsAuxIKPivot(lSrcAuxEffectorModel)
                if pDstCharacter.CreateAuxiliary(pEffectorId,lIsAuxPivot) == False:
                    print "Error!"
                    break
                    
                if lIsAuxPivot:
                    pDstCharacter.GetEffectorModel(pEffectorId,lSetId).IKPivot = lSrcAuxEffectorModel.IKPivot
            else:
                break

# Match dst character auxiliary effectors to src character
def MatchAuxiliary(pSrcCharacter, pDstCharacter):
    if pSrcCharacter and pDstCharacter:
        for lEffectorId in FBEffectorId.values.values():
            if pSrcCharacter.GetEffectorModel(lEffectorId): 
                MatchEffector(pSrcCharacter, pDstCharacter,lEffectorId)
        return True
    else:
        print "Missing params"
    return False

# Tool
gToolName = "MatchAuxiliaries"
gSystem = FBSystem()
gScene = gSystem.Scene
gDevelopment = True

class MatchAuxiliariesTool(FBTool):
    def SrcDrop(self,pEvent,pControl):
        if pEvent.State == FBDragAndDropState.kFBDragAndDropDrag:
            if pEvent.Components[0].Is(FBCharacter_TypeInfo()):
                pEvent.Accept()
        elif pEvent.State == FBDragAndDropState.kFBDragAndDropDrop:
            self.mSrcChar = pEvent.Components[0]
            pControl.Items.removeAll()
            pControl.Items.append(self.mSrcChar.Name)
            
    def DstDrop(self,pEvent,pControl):
        if pEvent.State == FBDragAndDropState.kFBDragAndDropDrag:
            if pEvent.Components[0].Is(FBCharacter_TypeInfo()):
                pEvent.Accept()
        elif pEvent.State == FBDragAndDropState.kFBDragAndDropDrop:
            self.mDstChar = pEvent.Components[0]
            pControl.Items.removeAll()
            pControl.Items.append(self.mDstChar.Name)
            
    def __init__(self):
        FBTool.__init__(self,gToolName)
        FBAddTool(self)

        self.mSrcChar = None
        self.mDstChar = None
        
        lLblSrcCharacter = FBLabel()
        lLblDstCharacter = FBLabel()
        lCntSrcCharacter = FBVisualContainer()
        lCntDstCharacter = FBVisualContainer()
        lBtnMatch = FBButton()
        
        lLblSrcCharacter.Caption = "Source Character:"
        lLblDstCharacter.Caption = "Destination Character:"
        lBtnMatch.Caption = "Match auxiliary"
        
        lVLt = FBVBoxLayout()
        x = FBAddRegionParam(5,FBAttachType.kFBAttachLeft,"")
        y = FBAddRegionParam(5,FBAttachType.kFBAttachTop,"")
        w = FBAddRegionParam(-5,FBAttachType.kFBAttachRight,"")
        h = FBAddRegionParam(-5,FBAttachType.kFBAttachBottom,"")
        self.AddRegion("Main" ,"Main" , x, y, w, h)
        self.SetControl("Main" ,lVLt)
        
        lHLt = FBHBoxLayout(FBAttachType.kFBAttachLeft)
        lHLt.AddRelative(lLblSrcCharacter,0.5)
        lHLt.AddRelative(lCntSrcCharacter,0.5)
        lVLt.Add(lHLt,20)
        lHLt = FBHBoxLayout(FBAttachType.kFBAttachLeft)
        lHLt.AddRelative(lLblDstCharacter,0.5)
        lHLt.AddRelative(lCntDstCharacter,0.5)
        lVLt.Add(lHLt,20)
        lVLt.Add(lBtnMatch,20)
        
        lCntSrcCharacter.OnDragAndDrop.Add(lambda control = None, event = None : self.SrcDrop(event,control))
        lCntDstCharacter.OnDragAndDrop.Add(lambda control = None, event = None : self.DstDrop(event,control))
        lBtnMatch.OnClick.Add(lambda control = None, event = None : MatchAuxiliary(self.mSrcChar,self.mDstChar))
        
        if len(gScene.Characters):
            self.mSrcChar = gScene.Characters[0]
            lCntSrcCharacter.Items.append(self.mSrcChar.Name)
            if len(gScene.Characters)>1:
                self.mDstChar = gScene.Characters[1]
                lCntDstCharacter.Items.append(self.mDstChar.Name)

        self.StartSizeX = 250
        self.StartSizeY = 115
        
# Display
if gDevelopment:
    FBDestroyToolByName(gToolName)

if gToolName in FBToolList:
    tool = FBToolList[gToolName]
    ShowTool(tool)
else:
    tool=MatchAuxiliariesTool()
    if gDevelopment:
        ShowTool(tool)