1 from __future__
import division
79 from builtins
import range
80 import maya.OpenMaya
as OpenMaya
81 import maya.OpenMayaUI
as OpenMayaUI
82 import maya.OpenMayaRender
as OpenMayaRender
83 import maya.OpenMayaMPx
as OpenMayaMPx
87 glRenderer = OpenMayaRender.MHardwareRenderer.theRenderer()
88 glFT = glRenderer.glFunctionTable()
90 kSwissArmyLocatorName =
"spSwissArmyLocator"
92 kSwissArmyLocatorManipName =
"spSwissArmyLocatorManip"
101 centre = [ [ 0.10, 0.0, 0.10 ],
102 [ 0.10, 0.0, -0.10 ],
103 [ -0.10, 0.0, -0.10 ],
104 [ -0.10, 0.0, 0.10 ],
105 [ 0.10, 0.0, 0.10 ] ]
106 state1 = [ [ 1.00, 0.0, 1.00 ],
110 [ 1.00, 0.0, 1.00 ] ]
111 state2 = [ [ 1.00, 0.0, -1.00 ],
112 [ 1.00, 0.0, -0.50 ],
113 [ 0.50, 0.0, -0.50 ],
114 [ 0.50, 0.0, -1.00 ],
115 [ 1.00, 0.0, -1.00 ] ]
116 state3 = [ [ -1.00, 0.0, -1.00 ],
117 [ -1.00, 0.0, -0.50 ],
118 [ -0.50, 0.0, -0.50 ],
119 [ -0.50, 0.0, -1.00 ],
120 [ -1.00, 0.0, -1.00 ] ]
121 state4 = [ [ -1.00, 0.0, 1.00 ],
122 [ -1.00, 0.0, 0.50 ],
123 [ -0.50, 0.0, 0.50 ],
124 [ -0.50, 0.0, 1.00 ],
125 [ -1.00, 0.0, 1.00 ] ]
126 arrow1 = [ [ 0.00, 0.0, 1.00 ],
128 [ -0.10, 0.0, 0.20 ],
129 [ 0.00, 0.0, 1.00 ] ]
130 arrow2 = [ [ 1.00, 0.0, 0.00 ],
132 [ 0.20, 0.0, -0.10 ],
133 [ 1.00, 0.0, 0.00 ] ]
134 arrow3 = [ [ 0.00, 0.0, -1.00 ],
135 [ 0.10, 0.0, -0.20 ],
136 [ -0.10, 0.0, -0.20 ],
137 [ 0.00, 0.0, -1.00 ] ]
138 arrow4 = [ [ -1.00, 0.0, 0.00 ],
139 [ -0.20, 0.0, 0.10 ],
140 [ -0.20, 0.0, -0.10 ],
141 [ -1.00, 0.0, 0.00 ] ]
142 perimeter=[ [ 1.10, 0.0, 1.10 ],
143 [ 1.10, 0.0, -1.10 ],
144 [ -1.10, 0.0, -1.10 ],
145 [ -1.10, 0.0, 1.10 ],
146 [ 1.10, 0.0, 1.10 ] ]
164 class swissArmyLocatorManip(OpenMayaMPx.MPxManipContainer):
166 OpenMayaMPx.MPxManipContainer.__init__(self)
181 def createChildren(self):
183 self.fFreePointTriadManip = self.addFreePointTriadManip(
"freePointTriadManip",
"point")
187 self.fDirectionManip = self.addDirectionManip(
"directionManip",
"direction")
191 self.fToggleManip = self.addToggleManip(
"toggleManip",
"toggle")
195 self.fStateManip = self.addStateManip(
"stateManip",
"state")
199 self.fDiscManip = self.addDiscManip(
"discManip",
"angle")
203 self.fCircleSweepManip = self.addCircleSweepManip(
"circleSweepManip",
"angle")
207 circleSweepManipFn.setRadius(2.0)
208 circleSweepManipFn.setDrawAsArc(
True)
211 self.fDistanceManip = self.addDistanceManip(
"distanceManip",
"distance")
217 self.fRotateManip = self.addRotateManip(
"RotateManip",
"rotation")
221 self.fScaleManip = self.addScaleManip(
"scaleManip",
"scale")
225 def connectToDependNode(self, node):
228 dagNodeFn.getPath(self.fNodePath)
229 parentNode = dagNodeFn.parent(0)
234 nodeFn.setObject(node)
239 translationPlug = parentNodeFn.findPlug(
"t")
240 freePointTriadManipFn.connectToPointPlug(translationPlug)
246 directionManipFn.setObject(self.fDirectionManip)
248 directionPlug = nodeFn.findPlug(
"arrow2Direction")
249 directionManipFn.connectToDirectionPlug(directionPlug)
250 startPointIndex = directionManipFn.startPointIndex()
251 self.addPlugToManipConversion(startPointIndex)
257 distanceManipFn.setObject(self.fDistanceManip)
259 sizePlug = nodeFn.findPlug(
"size")
260 distanceManipFn.connectToDistancePlug(sizePlug)
261 startPointIndex = distanceManipFn.startPointIndex()
262 self.addPlugToManipConversion(startPointIndex)
269 arrow1AnglePlug = nodeFn.findPlug(
"arrow1Angle")
270 circleSweepManipFn.connectToAnglePlug(arrow1AnglePlug)
271 centerIndex = circleSweepManipFn.centerIndex()
272 self.addPlugToManipConversion(centerIndex)
279 arrow3AnglePlug = nodeFn.findPlug(
"arrow3Angle")
280 discManipFn.connectToAnglePlug(arrow3AnglePlug)
281 centerIndex = discManipFn.centerIndex()
282 self.addPlugToManipConversion(centerIndex)
289 statePlug = nodeFn.findPlug(
"state")
290 stateManipFn.connectToStatePlug(statePlug)
291 positionIndex = stateManipFn.positionIndex()
292 self.addPlugToManipConversion(positionIndex)
299 togglePlug = nodeFn.findPlug(
"toggle")
300 toggleManipFn.connectToTogglePlug(togglePlug)
301 startPointIndex = toggleManipFn.startPointIndex()
302 self.addPlugToManipConversion(startPointIndex)
315 rotatePlug = transformNode.findPlug(
"rotate")
316 rotateManipFn.connectToRotationPlug(rotatePlug)
317 rotateManipFn.displayWithNode(node)
324 scalePlug = transformNode.findPlug(
"scale")
325 scaleManipFn.connectToScalePlug(scalePlug)
326 scaleManipFn.displayWithNode(node)
330 self.finishAddingManips()
331 OpenMayaMPx.MPxManipContainer.connectToDependNode(self, node)
334 def draw(self, view, path, style, status):
335 OpenMayaMPx.MPxManipContainer.draw(self, view, path, style, status)
338 view.drawText(
"Swiss Army Manipulator", textPos, OpenMayaUI.M3dView.kLeft)
342 def plugToManipConversion(self, theIndex):
344 numDataObj = numData.create(OpenMaya.MFnNumericData.k3Float)
345 vec = self.nodeTranslation()
346 numData.setData3Float(vec.x, vec.y, vec.z)
351 def nodeTranslation(self):
357 return transformFn.getTranslation(OpenMaya.MSpace.kWorld)
363 class swissArmyLocator(OpenMayaMPx.MPxLocatorNode):
380 OpenMayaMPx.MPxLocatorNode.__init__(self)
383 def compute(self, plug, data):
384 return OpenMaya.kUnknownParameter
387 def draw(self, view, path, style, status):
390 thisNode = self.thisMObject()
393 sizeVal = plug.asMDistance()
395 arrow1AnglePlug =
OpenMaya.MPlug(thisNode, swissArmyLocator.aArrow1Angle)
396 arrow1Angle = arrow1AnglePlug.asMAngle()
397 angle1 = -arrow1Angle.asRadians() - 3.1415927/2.0
399 arrow3AnglePlug =
OpenMaya.MPlug(thisNode, swissArmyLocator.aArrow3Angle)
400 arrow3Angle = arrow3AnglePlug.asMAngle()
401 angle3 = arrow3Angle.asRadians()
404 state = statePlug.asInt()
407 toggle = togglePlug.asBool()
409 directionXPlug =
OpenMaya.MPlug(thisNode, swissArmyLocator.aArrow2DirectionX)
410 directionYPlug =
OpenMaya.MPlug(thisNode, swissArmyLocator.aArrow2DirectionY)
411 directionZPlug =
OpenMaya.MPlug(thisNode, swissArmyLocator.aArrow2DirectionZ)
412 dirX = directionXPlug.asDouble()
413 dirY = directionYPlug.asDouble()
414 dirZ = directionZPlug.asDouble()
416 angle2 = math.atan2(dirZ, dirX)
419 multiplier = sizeVal.asCentimeters()
423 if ((style == OpenMayaUI.M3dView.kFlatShaded)
or
424 (style == OpenMayaUI.M3dView.kGouraudShaded)):
426 glFT.glPushAttrib(OpenMayaRender.MGL_CURRENT_BIT)
428 if (status == OpenMayaUI.M3dView.kActive):
429 view.setDrawColor(13, OpenMayaUI.M3dView.kActiveColors)
431 view.setDrawColor(13, OpenMayaUI.M3dView.kDormantColors)
434 if (status == OpenMayaUI.M3dView.kActive):
435 view.setDrawColor(15, OpenMayaUI.M3dView.kActiveColors)
437 view.setDrawColor(15, OpenMayaUI.M3dView.kDormantColors)
438 glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
439 last = kCentreCount - 1
440 for i
in range(last):
441 glFT.glVertex3f(centre[i][0] * multiplier,
442 centre[i][1] * multiplier,
443 centre[i][2] * multiplier)
447 if (status == OpenMayaUI.M3dView.kActive):
448 view.setDrawColor(19, OpenMayaUI.M3dView.kActiveColors)
450 view.setDrawColor(19, OpenMayaUI.M3dView.kDormantColors)
451 glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
452 last = kState1Count - 1
453 for i
in range(last):
454 glFT.glVertex3f(state1[i][0] * multiplier,
455 state1[i][1] * multiplier,
456 state1[i][2] * multiplier)
460 if (status == OpenMayaUI.M3dView.kActive):
461 view.setDrawColor(21, OpenMayaUI.M3dView.kActiveColors)
463 view.setDrawColor(21, OpenMayaUI.M3dView.kDormantColors)
464 glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
465 last = kState2Count - 1
466 for i
in range(last):
467 glFT.glVertex3f(state2[i][0] * multiplier,
468 state2[i][1] * multiplier,
469 state2[i][2] * multiplier)
473 if (status == OpenMayaUI.M3dView.kActive):
474 view.setDrawColor(18, OpenMayaUI.M3dView.kActiveColors)
476 view.setDrawColor(18, OpenMayaUI.M3dView.kDormantColors)
477 glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
478 last = kState3Count - 1
479 for i
in range(last):
480 glFT.glVertex3f(state3[i][0] * multiplier,
481 state3[i][1] * multiplier,
482 state3[i][2] * multiplier)
486 if (status == OpenMayaUI.M3dView.kActive):
487 view.setDrawColor(17, OpenMayaUI.M3dView.kActiveColors)
489 view.setDrawColor(17, OpenMayaUI.M3dView.kDormantColors)
490 glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
491 last = kState4Count - 1
492 for i
in range(last):
493 glFT.glVertex3f(state4[i][0] * multiplier,
494 state4[i][1] * multiplier,
495 state4[i][2] * multiplier)
498 if (status == OpenMayaUI.M3dView.kActive):
499 view.setDrawColor(12, OpenMayaUI.M3dView.kActiveColors)
501 view.setDrawColor(12, OpenMayaUI.M3dView.kDormantColors)
502 glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
503 last = kArrow1Count - 1
504 for i
in range(last):
505 glFT.glVertex3f((-arrow1[i][0] * multiplier * math.cos(angle1) - arrow1[i][2] * multiplier * math.sin(angle1)),
506 (arrow1[i][1] * multiplier + delta1),
507 (arrow1[i][2] * multiplier * math.cos(angle1) - arrow1[i][0] * multiplier * math.sin(angle1)))
510 if (status == OpenMayaUI.M3dView.kActive):
511 view.setDrawColor(16, OpenMayaUI.M3dView.kActiveColors)
513 view.setDrawColor(16, OpenMayaUI.M3dView.kDormantColors)
514 glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
515 last = kArrow2Count - 1
516 for i
in range(last):
517 glFT.glVertex3f((-arrow2[i][0] * multiplier * math.cos(angle2) - arrow2[i][2] * multiplier * math.sin(angle2)),
518 (arrow2[i][1] * multiplier + delta2),
519 (arrow2[i][2] * multiplier * math.cos(angle2) - arrow2[i][0] * multiplier * math.sin(angle2)))
522 if (status == OpenMayaUI.M3dView.kActive):
523 view.setDrawColor(13, OpenMayaUI.M3dView.kActiveColors)
525 view.setDrawColor(13, OpenMayaUI.M3dView.kDormantColors)
526 glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
527 last = kArrow3Count - 1
528 for i
in range(last):
529 glFT.glVertex3f((-arrow3[i][0] * multiplier * math.cos(angle3) - arrow3[i][2] * multiplier * math.sin(angle3)),
530 (arrow3[i][1] * multiplier + delta3),
531 (arrow3[i][2] * multiplier * math.cos(angle3) - arrow3[i][0] * multiplier * math.sin(angle3)))
534 if (status == OpenMayaUI.M3dView.kActive):
535 view.setDrawColor(5, OpenMayaUI.M3dView.kActiveColors)
537 view.setDrawColor(5, OpenMayaUI.M3dView.kDormantColors)
538 glFT.glBegin(OpenMayaRender.MGL_TRIANGLE_FAN)
539 last = kArrow4Count - 1
540 for i
in range(last):
541 glFT.glVertex3f((arrow4[i][0] * multiplier),
542 (arrow4[i][1] * multiplier + delta4),
543 (arrow4[i][2] * multiplier))
549 glFT.glBegin(OpenMayaRender.MGL_LINES)
552 last = kCentreCount - 1
553 for i
in range(last):
554 glFT.glVertex3f(centre[i][0] * multiplier,
555 centre[i][1] * multiplier,
556 centre[i][2] * multiplier)
557 glFT.glVertex3f(centre[i+1][0] * multiplier,
558 centre[i+1][1] * multiplier,
559 centre[i+1][2] * multiplier)
562 last = kState1Count - 1
563 for i
in range(last):
564 glFT.glVertex3f(state1[i][0] * multiplier,
565 state1[i][1] * multiplier,
566 state1[i][2] * multiplier)
567 glFT.glVertex3f(state1[i+1][0] * multiplier,
568 state1[i+1][1] * multiplier,
569 state1[i+1][2] * multiplier)
572 last = kState2Count - 1
573 for i
in range(last):
574 glFT.glVertex3f(state2[i][0] * multiplier,
575 state2[i][1] * multiplier,
576 state2[i][2] * multiplier)
577 glFT.glVertex3f(state2[i+1][0] * multiplier,
578 state2[i+1][1] * multiplier,
579 state2[i+1][2] * multiplier)
582 last = kState3Count - 1
583 for i
in range(last):
584 glFT.glVertex3f(state3[i][0] * multiplier,
585 state3[i][1] * multiplier,
586 state3[i][2] * multiplier)
587 glFT.glVertex3f(state3[i+1][0] * multiplier,
588 state3[i+1][1] * multiplier,
589 state3[i+1][2] * multiplier)
592 last = kState4Count - 1
593 for i
in range(last):
594 glFT.glVertex3f(state4[i][0] * multiplier,
595 state4[i][1] * multiplier,
596 state4[i][2] * multiplier)
597 glFT.glVertex3f(state4[i+1][0] * multiplier,
598 state4[i+1][1] * multiplier,
599 state4[i+1][2] * multiplier)
601 last = kArrow1Count - 1
602 for i
in range(last):
603 glFT.glVertex3f((-arrow1[i][0] * multiplier * math.cos(angle1) - arrow1[i][2] * multiplier * math.sin(angle1)),
604 (arrow1[i][1] * multiplier + delta1),
605 (arrow1[i][2] * multiplier * math.cos(angle1) - arrow1[i][0] * multiplier * math.sin(angle1)))
606 glFT.glVertex3f((-arrow1[i+1][0] * multiplier * math.cos(angle1) - arrow1[i+1][2] * multiplier * math.sin(angle1)),
607 (arrow1[i+1][1] * multiplier + delta1),
608 (arrow1[i+1][2] * multiplier * math.cos(angle1) - arrow1[i+1][0] * multiplier * math.sin(angle1)))
610 last = kArrow2Count - 1
611 for i
in range(last):
612 glFT.glVertex3f((-arrow2[i][0] * multiplier * math.cos(angle2) - arrow2[i][2] * multiplier * math.sin(angle2)),
613 (arrow2[i][1] * multiplier + delta2),
614 (arrow2[i][2] * multiplier * math.cos(angle2) - arrow2[i][0] * multiplier * math.sin(angle2)))
615 glFT.glVertex3f((-arrow2[i+1][0] * multiplier * math.cos(angle2) - arrow2[i+1][2] * multiplier * math.sin(angle2)),
616 (arrow2[i+1][1] * multiplier + delta2),
617 (arrow2[i+1][2] * multiplier * math.cos(angle2) - arrow2[i+1][0] * multiplier * math.sin(angle2)))
619 last = kArrow3Count - 1
620 for i
in range(last):
621 glFT.glVertex3f((-arrow3[i][0] * multiplier * math.cos(angle3) - arrow3[i][2] * multiplier * math.sin(angle3)),
622 (arrow3[i][1] * multiplier + delta3),
623 (arrow3[i][2] * multiplier * math.cos(angle3) - arrow3[i][0] * multiplier * math.sin(angle3)))
624 glFT.glVertex3f((-arrow3[i+1][0] * multiplier * math.cos(angle3) - arrow3[i+1][2] * multiplier * math.sin(angle3)),
625 (arrow3[i+1][1] * multiplier + delta3),
626 (arrow3[i+1][2] * multiplier * math.cos(angle3) - arrow3[i+1][0] * multiplier * math.sin(angle3)))
628 last = kArrow4Count - 1
629 for i
in range(last):
630 glFT.glVertex3f((arrow4[i][0] * multiplier),
631 (arrow4[i][1] * multiplier + delta4),
632 (arrow4[i][2] * multiplier))
633 glFT.glVertex3f((arrow4[i+1][0] * multiplier),
634 (arrow4[i+1][1] * multiplier + delta4),
635 (arrow4[i+1][2] * multiplier))
637 last = kPerimeterCount - 1
638 for i
in range(last):
639 glFT.glVertex3f(perimeter[i][0] * multiplier,
640 perimeter[i][1] * multiplier,
641 perimeter[i][2] * multiplier)
642 glFT.glVertex3f(perimeter[i+1][0] * multiplier,
643 perimeter[i+1][1] * multiplier,
644 perimeter[i+1][2] * multiplier)
655 def boundingBox(self):
656 thisNode = self.thisMObject()
658 sizeVal = plug.asMDistance()
660 multiplier = sizeVal.asCentimeters()
665 corner1 = corner1 * multiplier
666 corner2 = corner2 * multiplier
676 def locatorCreator():
677 return OpenMayaMPx.asMPxPtr(swissArmyLocator())
685 swissArmyLocator.aSize = unitFn.create(
"size",
"sz", OpenMaya.MFnUnitAttribute.kDistance, 10.0)
686 unitFn.setStorable(
True)
687 unitFn.setWritable(
True)
690 swissArmyLocator.aPointX = numericFn.create(
"pointX",
"ptx", OpenMaya.MFnNumericData.kDouble, 0.0)
691 swissArmyLocator.aPointY = numericFn.create(
"pointY",
"pty", OpenMaya.MFnNumericData.kDouble, 0.0)
692 swissArmyLocator.aPointZ = numericFn.create(
"pointZ",
"ptz", OpenMaya.MFnNumericData.kDouble, 0.0)
693 swissArmyLocator.aPoint = numericFn.create(
"point",
"pt", swissArmyLocator.aPointX, swissArmyLocator.aPointY, swissArmyLocator.aPointZ)
696 swissArmyLocator.aArrow1Angle = unitFn.create(
"arrow1Angle",
"a1a", OpenMaya.MFnUnitAttribute.kAngle, 0.0)
699 swissArmyLocator.aArrow2DirectionX = numericFn.create(
"arrow2DirectionX",
"a2x", OpenMaya.MFnNumericData.kDouble, 1.0)
700 swissArmyLocator.aArrow2DirectionY = numericFn.create(
"arrow2DirectionY",
"a2y", OpenMaya.MFnNumericData.kDouble, 0.0)
701 swissArmyLocator.aArrow2DirectionZ = numericFn.create(
"arrow2DirectionZ",
"a2z", OpenMaya.MFnNumericData.kDouble, 0.0)
702 swissArmyLocator.aArrow2Direction = numericFn.create(
"arrow2Direction",
"dir", swissArmyLocator.aArrow2DirectionX, swissArmyLocator.aArrow2DirectionY, swissArmyLocator.aArrow2DirectionZ)
705 swissArmyLocator.aArrow3Angle = unitFn.create(
"arrow3Angle",
"a3a", OpenMaya.MFnUnitAttribute.kAngle, 0.0)
707 swissArmyLocator.aArrow4Distance = unitFn.create(
"arrow2Distance",
"dis", OpenMaya.MFnUnitAttribute.kDistance, 0.0)
710 swissArmyLocator.aState = numericFn.create(
"state",
"s", OpenMaya.MFnNumericData.kLong, 0)
713 swissArmyLocator.aToggle = numericFn.create(
"toggle",
"t", OpenMaya.MFnNumericData.kBoolean,
False)
715 swissArmyLocator.addAttribute(swissArmyLocator.aPoint)
716 swissArmyLocator.addAttribute(swissArmyLocator.aArrow1Angle)
717 swissArmyLocator.addAttribute(swissArmyLocator.aArrow2Direction)
718 swissArmyLocator.addAttribute(swissArmyLocator.aArrow3Angle)
719 swissArmyLocator.addAttribute(swissArmyLocator.aArrow4Distance)
720 swissArmyLocator.addAttribute(swissArmyLocator.aState)
721 swissArmyLocator.addAttribute(swissArmyLocator.aToggle)
722 swissArmyLocator.addAttribute(swissArmyLocator.aSize)
724 OpenMayaMPx.MPxManipContainer.addToManipConnectTable(kSwissArmyLocatorId)
727 def locatorManipCreator():
728 return OpenMayaMPx.asMPxPtr(swissArmyLocatorManip())
731 def locatorManipInit():
732 OpenMayaMPx.MPxManipContainer.initialize()
736 def initializePlugin(mobject):
737 mplugin = OpenMayaMPx.MFnPlugin(mobject,
"Autodesk",
"1.0",
"Any")
740 mplugin.registerNode(kSwissArmyLocatorName,
744 OpenMayaMPx.MPxNode.kLocatorNode)
746 print(
"Failed to register context command: %s" % kSwissArmyLocatorName)
750 mplugin.registerNode(kSwissArmyLocatorManipName,
751 kSwissArmyLocatorManipId,
754 OpenMayaMPx.MPxNode.kManipContainer)
756 print(
"Failed to register node: %s" % kSwissArmyLocatorManipName)
761 def uninitializePlugin(mobject):
762 mplugin = OpenMayaMPx.MFnPlugin(mobject)
765 mplugin.deregisterNode(kSwissArmyLocatorId)
767 print(
"Failed to deregister context command: %s" % kSwissArmyLocatorName)
771 mplugin.deregisterNode(kSwissArmyLocatorManipId)
773 print(
"Failed to deregister node: %s" % kSwissArmyLocatorManipName)