import sys
import manipulatorMath
import maya.OpenMaya as OpenMaya
import maya.OpenMayaUI as OpenMayaUI
import maya.OpenMayaMPx as OpenMayaMPx
import maya.OpenMayaRender as OpenMayaRender
filmMoveManipId = OpenMaya.MTypeId(0x8104B)
contextCmdName = "spFilmMoveManipCtxCmd"
nodeName = "spFilmMoveManip"
class manipulatorGeometry:
scale = 3
def horizontalStart(self):
p = OpenMaya.MPoint(0,0,0) * self.scale
return p
def horizontalEnd(self):
p = OpenMaya.MPoint(1,0,0) * self.scale
return p
def verticalStart(self):
p = OpenMaya.MPoint(0,0,0) * self.scale
return p
def verticalEnd(self):
p = OpenMaya.MPoint(0,1,0) * self.scale
return p
class filmMoveManip(OpenMayaMPx.MPxManipulatorNode):
horizontalIndex = 0
verticalIndex = 0
glHorizontalName = 0
glVerticalName = 0
translation = OpenMaya.MPoint()
glRenderer = OpenMayaRender.MHardwareRenderer.theRenderer()
glFT = glRenderer.glFunctionTable()
def __init__(self):
OpenMayaMPx.MPxManipulatorNode.__init__(self)
def postConstructor(self):
try:
su = OpenMaya.MScriptUtil()
su.createFromInt(0)
iptr = su.asIntPtr()
self.addDoubleValue( "horizontalValue", 0, iptr )
self.horizontalIndex = su.getInt( iptr )
except:
sys.stderr.write("Failed to add horizontal double value\n")
raise
try:
su = OpenMaya.MScriptUtil()
su.createFromInt(0)
iptr = su.asIntPtr()
self.addDoubleValue( "verticalValue", 0, iptr )
self.verticalIndex = su.getInt( iptr )
except:
sys.stderr.write("Failed to add vertical double value\n")
raise
def draw(self,view,dagpath,displayStyle,displayStatus):
mo = manipulatorGeometry()
vs = mo.verticalStart()
ve = mo.verticalEnd()
hs = mo.horizontalStart()
he = mo.horizontalEnd()
view.beginGL()
self.glFT.glPushMatrix()
self.glFT.glTranslated(self.translation.x,self.translation.y,self.translation.z)
su = OpenMaya.MScriptUtil()
su.createFromInt(0)
iptr = su.asUintPtr()
self.glFirstHandle( iptr )
startIndex = su.getUint( iptr )
self.glHorizontalName = startIndex
self.colorAndName( view, self.glHorizontalName, True, self.mainColor() )
self.glFT.glBegin( OpenMayaRender.MGL_LINES )
self.glFT.glVertex3d( hs.x, hs.y, hs.z )
self.glFT.glVertex3d( he.x, he.y, he.z )
self.glFT.glEnd()
self.glVerticalName = self.glHorizontalName + 1
self.colorAndName( view, self.glVerticalName, True, self.mainColor() )
self.glFT.glBegin( OpenMayaRender.MGL_LINES )
self.glFT.glVertex3d( vs.x, vs.y, vs.z )
self.glFT.glVertex3d( ve.x, ve.y, ve.z )
self.glFT.glEnd()
self.glFT.glPopMatrix()
view.endGL()
def doPress(self,view):
self.translation = OpenMaya.MPoint()
self.updateDragInformation(view)
def doDrag(self,view):
self.updateDragInformation(view)
def doRelease(self,view):
self.updateDragInformation(view,True)
def getPlaneForView(self,view):
mo = manipulatorGeometry()
vs = mo.verticalStart()
ve = mo.verticalEnd()
hs = mo.horizontalStart()
he = mo.horizontalEnd()
v1 = he - hs
v2 = vs - ve
normal = v1 ^ v2
normal.normalize()
plane = manipulatorMath.planeMath()
plane.setPlane( vs, normal )
return plane
def updateDragInformation(self,view,updateAttribute = False):
localMousePoint = OpenMaya.MPoint()
localMouseDirection = OpenMaya.MVector()
self.mouseRay( localMousePoint, localMouseDirection )
plane = self.getPlaneForView( view )
mouseIntersect = OpenMaya.MPoint()
(worked,mouseIntersect) = plane.intersect( localMousePoint, localMouseDirection )
if not worked:
print "Failed to intersect plane"
return
su = OpenMaya.MScriptUtil()
su.createFromInt(0)
uintptr = su.asUintPtr()
self.glActiveName( uintptr )
active = su.getUint( uintptr )
mo = manipulatorGeometry()
start = None
end = None
if active == self.glHorizontalName:
start = mo.horizontalStart()
end = mo.horizontalEnd()
elif active == self.glVerticalName:
start = mo.verticalStart()
end = mo.verticalEnd()
else:
raise "Unknown GL active component"
vstart_end = start - end
line = manipulatorMath.lineMath()
line.setLine( start, vstart_end )
closestPoint = OpenMaya.MPoint()
(worked,closestPoint) = line.closestPoint( mouseIntersect )
if not worked:
print "Failed to find closest point to line"
return
self.translation = closestPoint
if updateAttribute:
m = manipulatorMath.maxOfAbsThree(closestPoint.x,closestPoint.y,closestPoint.z)
m = m / 10.0
if active == self.glHorizontalName:
self.setDoubleValue(self.horizontalIndex,m)
elif active == self.glVerticalName:
self.setDoubleValue(self.verticalIndex,m)
def connectToDependNode(self, node):
nodeFn = OpenMaya.MFnDependencyNode(node)
try:
hPlug = nodeFn.findPlug("filmTranslateH")
vPlug = nodeFn.findPlug("filmTranslateV")
su = OpenMaya.MScriptUtil()
su.createFromInt(0)
iptr = su.asIntPtr()
self.connectPlugToValue( hPlug, self.horizontalIndex, iptr )
self.connectPlugToValue( vPlug, self.verticalIndex, iptr )
except:
sys.stderr.write( "Error finding and connecting plugs\n" )
raise
try:
OpenMayaMPx.MPxManipulatorNode.finishAddingManips(self)
OpenMayaMPx.MPxManipulatorNode.connectToDependNode(self,node)
except:
sys.stderr.write( "Error when finishing node connection\n" )
raise
def filmMoveManipCreator():
return OpenMayaMPx.asMPxPtr( filmMoveManip() )
def filmMoveManipInitialize():
print "Initializing film move manipulator"
class filmMoveManipContext(OpenMayaMPx.MPxSelectionContext):
def __init__(self):
OpenMayaMPx.MPxSelectionContext.__init__(self)
def toolOnSetup(self,event):
updateManipulators(self)
OpenMaya.MModelMessage.addCallback(OpenMaya.MModelMessage.kActiveListModified, updateManipulators, self)
def updateManipulators(clientData):
clientData.deleteManipulators()
selectionList = OpenMaya.MSelectionList()
OpenMaya.MGlobal.getActiveSelectionList(selectionList)
selectionIter = OpenMaya.MItSelectionList(selectionList, OpenMaya.MFn.kInvalid)
while not selectionIter.isDone():
dependNode = OpenMaya.MObject()
selectionIter.getDependNode(dependNode)
if dependNode.isNull() or not dependNode.hasFn(OpenMaya.MFn.kCamera):
print "depend node is null or not a camera"
selectionIter.next()
continue
dependNodeFn = OpenMaya.MFnDependencyNode(dependNode)
hPlug = dependNodeFn.findPlug("filmTranslateH", False)
vPlug = dependNodeFn.findPlug("filmTranslateV", False)
if hPlug.isNull() or vPlug.isNull():
print "One of filmTranslate H,V plugs are null"
selectionIter.next()
continue
manipObject = OpenMaya.MObject()
manipulator = OpenMayaMPx.MPxManipulatorNode.newManipulator(nodeName, manipObject)
if manipulator is not None:
clientData.addManipulator(manipObject)
manipulator.connectToDependNode(dependNode)
selectionIter.next()
class filmMoveManipCtxCmd(OpenMayaMPx.MPxContextCommand):
def __init__(self):
OpenMayaMPx.MPxContextCommand.__init__(self)
def makeObj(self):
return OpenMayaMPx.asMPxPtr( filmMoveManipContext() )
def contextCmdCreator():
return OpenMayaMPx.asMPxPtr( filmMoveManipCtxCmd() )
def initializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)
try:
mplugin.registerContextCommand( contextCmdName, contextCmdCreator )
except:
print "Failed to register context command: %s" % contextCmdName
raise
try:
mplugin.registerNode(nodeName, filmMoveManipId, filmMoveManipCreator, filmMoveManipInitialize, OpenMayaMPx.MPxNode.kManipulatorNode)
except:
print "Failed to register node: %s" % nodeName
raise
def uninitializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)
try:
mplugin.deregisterContextCommand(contextCmdName)
except:
print "Failed to deregister context command: %s" % contextCmdName
raise
try:
mplugin.deregisterNode(filmMoveManipId)
except:
print "Failed to deregister node: %s" % nodeName
raise