Published using Google Docs
PositionCalculator.txt
Updated automatically every 5 minutes

"""

provide automatic snapping by position, pivot, and rotation between two geometric objects

supports

    transforms

    cvs

    vtxs

###

#Author: Nathaniel Anozie

#ogbonnawork at gmail dot com

#Date Last Modified: Nov 2011

#Version: 1.0.1

###

"""

import maya.cmds as mc

class PositionCalculator:

    """

    """

    def __init__(self):

        """

        """

        pass

    def assertIsDriverAndDrivenListOnStage(self, driver, driven):

        """

        the dirver and driven have to be created somewhere

        """      

        isNamesValid = self.__isAllOnStage([driver,driven])

        assert isNamesValid == True, 'Names not found! Please check name of: %s and %s' %(driver,driven)        

   

    def matchPivot(self, driver, driven):

        """

        driver has pivot we want driven to have

        note -- match pivot of transform non joint to any joint in a joint hierarchy causes movement of driven object

        --and default sets the pivot to the uppermost parent of hierarchy

        """

        self.assertIsDriverAndDrivenListOnStage(driver,driven)

       

        #get world position of driver rotatePivot, and scalePivot

        driverWorldPvtRotate = mc.xform(driver, rotatePivot = True, query=True, ws=True)

        driverWorldPvtScale = mc.xform(driver, scalePivot = True, query=True, ws=True)

       

        #move pivot of driven to the pivot of driver

        mc.xform(driven, preserve =True, rotatePivot=driverWorldPvtRotate, scalePivot=driverWorldPvtScale, ws=True )

       

       

    def matchPosition(self, driver, driven):

        """

        the driver has the position we want the driven to have

        """

        self.assertIsDriverAndDrivenListOnStage(driver,driven)  

        #get world position of driver rotatePivot, and scalePivot

        driverWorldTranslate = mc.xform(driver, translation = True, query=True, ws=True)

        #move driven to the position of driver

        mc.xform(driven,translation=driverWorldTranslate, ws=True )

           

    def matchOrientation(self, driver, driven):

        """

        the driver has the orientation we want the driven to have

        """

        self.assertIsDriverAndDrivenListOnStage(driver,driven)  

        #get world position of driver rotatePivot, and scalePivot

        driverWorldRotate = mc.xform(driver, rotation = True, query=True, ws=True)

        #move driven to the position of driver

        mc.xform(driven,rotation=driverWorldRotate, ws=True )

       

       

    def matchPositionAndOrientation(self, driver, driven):

        """

        the driver has the orientation we want the driven to have

        """

        self.assertIsDriverAndDrivenListOnStage(driver,driven)

        self.matchPosition(driver,driven)

        self.matchOrientation(driver,driven)

                       

    def __isAllOnStage(self,array):

        """

        needs argumenst

            list of names

       

        return boolean

        """

        for element in array:

            if not mc.objExists( element ):

                return False

        return True