3D animation,rigging,coding, math ,fun stuff welcome !

getOrientation.py

"""
Copyright (C) 2009 Rick Fronek
melvin.3d@gmail.com

getOrientation.py
Python Script File
MODIFY THIS AT YOUR OWN RISK

Update Date:    Jan 08 2010

Description:
    grabs selected models in the scene and based on an argument
    returns position,rotation or both.
    No error checking , works technically on any transforms.

    -1  = returns translation
     1  = returns rotation
     0  = returns both rotation and translation

Function list:
     1. rf_getSelectedObjects() - grabs selected moddels and returns them
     2. rf_getObjectOrientation(arg,) - queries position,rotation and returns them.

NOTE: make use of import function in python to use these anywhere,anytime.
"""

from pyfbsdk import *

def rf_getSelectedObjects():
    # instance the modelList here
    returnList = []
    lSelectedNodes = FBModelList()
    # pass the list to global function
    lmodelList = FBGetSelectedModels(lSelectedNodes)
    if not len(lSelectedNodes):
        print "Must select objects to continue!"
        return None
    else:
        for eachNode in lSelectedNodes:
            returnList.append(eachNode)
    # get them back
    return returnList


def rf_getObjectOrientation(arg,lNodes=rf_getSelectedObjects()):
    """
     -1  = returns translation
      1  = returns rotation
      0  = returns both rotation and translation

      It's extremelly important to include the "," there when assigning
      the results of the above function directly to the list in
      rf_getObjectOrientation(arg,lNodes=rf_getSelectedObjects())
      To execute you must have correct syntax.
      rf_getObjectOrientation(0,)
    """
    # logic
    if arg < 0:
        # translation
        lGrabPosition = []
        # go slick man...
        for node in lNodes:
            # instance FBVector3d class
            lPosition3d = FBVector3d()
            # pass it to the GetVector()
            node.GetVector(lPosition3d,FBModelTransformationMatrix.kModelTranslation)
            # now the value is stored in lPosition3d object
            tmpHolder = [lPosition3d[0],lPosition3d[1],lPosition3d[2]]
            lGrabPosition.append(tmpHolder)
        # look at the values , get rid of it if you dont want to use the print statement
        print "POSITIONS :"
        for v,n in enumerate(lNodes):
            print n.Name,">>>",lGrabPosition[v]
        return lGrabPosition
        # delete the variable and its value clean the scene.
        del(lGrabPosition)

    elif arg > 0:
        # rotation
        lGrabRotation = []
        # go baby ..
        for node in lNodes:
            # instance FBVector3d class
            lRotation3d = FBVector3d()
            # pass it to the GetVector()
            node.GetVector(lRotation3d,FBModelTransformationMatrix.kModelRotation)
            # now the value is stored in lPosition3d object
            tmpHolder = [lRotation3d[0],lRotation3d[1],lRotation3d[2]]
            lGrabRotation.append(tmpHolder)
        # look at the values
        print "ROTATIONS :"
        for v,n in enumerate(lNodes):
            print n.Name,">>>",lGrabRotation[v]
        return lGrabRotation
        # again clean the scene
        del(lGrabRotation)

    else:
        # both - translation and rotation
        lGrabPosition = []
        lGrabRotation = []
        for node in lNodes:
            lPosition3d = FBVector3d()
            lRotation3d = FBVector3d()
            node.GetVector(lPosition3d,FBModelTransformationMatrix.kModelTranslation)
            node.GetVector(lRotation3d,FBModelTransformationMatrix.kModelRotation)
            tmpPosHold = [lPosition3d[0],lPosition3d[1],lPosition3d[2]]
            tmpRotHold = [lRotation3d[0],lRotation3d[1],lRotation3d[2]]
            lGrabPosition.append(tmpPosHold)
            lGrabRotation.append(tmpRotHold)
        # report the values
        print "POSITIONS/ROTATIONS :"
        for v,n in enumerate(lNodes):
            print n.Name,">>>",lGrabPosition[v],"\n"+(9*" "),lGrabRotation[v]