motionTraceCmd.py
import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx
import math, sys
kPluginCmdName="spMotionTrace"
kStartFlag = "-s"
kStartLongFlag = "-startFrame"
kEndFlag = "-e"
kEndLongFlag = "-endFrame"
kByFlag = "-b"
kByLongFlag = "-byFrame"
class motionTrace(OpenMayaMPx.MPxCommand):
def __init__(self):
OpenMayaMPx.MPxCommand.__init__(self)
self.__start = 1
self.__end = 60
self.__by = 1
def doIt(self, args):
"""
This method is called from script when this command is called.
It should set up any class data necessary for redo/undo,
parse any given arguments, and then call redoIt.
"""
argData = OpenMaya.MArgDatabase(self.syntax(), args)
if argData.isFlagSet(kStartFlag):
self.__start = argData.flagArgumentInt(kStartFlag, 0)
if argData.isFlagSet(kEndFlag):
self.__end = argData.flagArgumentInt(kEndFlag, 0)
if argData.isFlagSet(kByFlag):
self.__by = argData.flagArgumentInt(kByFlag, 0)
self.redoIt()
def redoIt(self):
"""
This method performs the action of the command.
This method iterates over all selected items and
prints out connected plug and dependency node type
information.
"""
picked = OpenMaya.MObjectArray()
dependNode = OpenMaya.MObject()
slist = OpenMaya.MSelectionList()
OpenMaya.MGlobal.getActiveSelectionList(slist)
iter = OpenMaya.MItSelectionList(slist)
while not iter.isDone():
iter.getDependNode(dependNode)
picked.append(dependNode)
iter.next()
pointArrays = [ OpenMaya.MPointArray() for i in range(picked.length()) ]
time = self.__start
while (time <= self.__end):
timeval = OpenMaya.MTime(time)
OpenMaya.MGlobal.viewFrame(timeval)
for i in range(picked.length()):
dependNode = picked[i]
fnDependNode = OpenMaya.MFnDependencyNode(dependNode)
txAttr = fnDependNode.attribute("translateX")
txPlug = OpenMaya.MPlug(dependNode, txAttr)
tx = txPlug.asDouble()
tyAttr = fnDependNode.attribute("translateY")
tyPlug = OpenMaya.MPlug(dependNode, tyAttr)
ty = tyPlug.asDouble()
tzAttr = fnDependNode.attribute("translateZ")
tzPlug = OpenMaya.MPlug(dependNode, tzAttr)
tz = tzPlug.asDouble()
print "adding", tx, ty, tz, "\n"
pointArrays[i].append(OpenMaya.MPoint(tx, ty, tz))
time += self.__by
for i in range(picked.length()):
self.__jMakeCurve(pointArrays[i])
def __jMakeCurve(self, cvs):
"""
Make a degree 1 curve from the given CVs.
Note that in Python, a double underscore in front of a member name
make the method "private" to the class through name-mangling
"""
deg = 1
knots = OpenMaya.MDoubleArray()
for i in range(cvs.length()):
knots.append(i)
nullObj = OpenMaya.MObject()
curveFn = OpenMaya.MFnNurbsCurve()
curveFn.create(cvs, knots, deg, OpenMaya.MFnNurbsCurve.kOpen, False, False, nullObj)
def cmdCreator():
return OpenMayaMPx.asMPxPtr(motionTrace())
def syntaxCreator():
syntax = OpenMaya.MSyntax()
syntax.addFlag(kStartFlag, kStartLongFlag, OpenMaya.MSyntax.kLong)
syntax.addFlag(kEndFlag, kEndLongFlag, OpenMaya.MSyntax.kLong)
syntax.addFlag(kByFlag, kByLongFlag, OpenMaya.MSyntax.kLong)
return syntax
def initializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject, "Autodesk", "1.0", "Any")
try:
mplugin.registerCommand(kPluginCmdName, cmdCreator, syntaxCreator)
except:
sys.stderr.write("Failed to register command: %s\n" % kPluginCmdName)
raise
def uninitializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)
try:
mplugin.deregisterCommand(kPluginCmdName)
except:
sys.stderr.write("Failed to unregister command: %s\n" % kPluginCmdName)
raise