Python API 2.0 Reference
python/api2/py2UiDrawManager.py
1 #-
2 # ===========================================================================
3 # Copyright 2018 Autodesk, Inc. All rights reserved.
4 #
5 # Use of this software is subject to the terms of the Autodesk license
6 # agreement provided at the time of installation or download, or which
7 # otherwise accompanies this software in either electronic or hard copy form.
8 # ===========================================================================
9 #+
10 
11 from builtins import range
12 import sys
13 import maya.api.OpenMaya as om
14 import maya.api.OpenMayaUI as omui
15 import maya.api.OpenMayaRender as omr
16 
17 def maya_useNewAPI():
18  """
19  The presence of this function tells Maya that the plugin produces, and
20  expects to be passed, objects created using the Maya Python API 2.0.
21  """
22  pass
23 
24 kText = 0
25 kLine = 1
26 kPoint = 2
27 kRect = 3
28 kQuad = 4
29 kSphere = 5
30 kCircle = 6
31 kArc = 7
32 kSphere2 = 8
33 kCircle2 = 9
34 kArc2 = 10
35 kCone = 11
36 kCylinder = 12
37 kCapsule = 13
38 
39 class uiDrawManager(omui.MPxLocatorNode):
40  id = om.MTypeId(0x0008006C)
41  drawDbClassification = "drawdb/geometry/uiDrawManager_py"
42  drawRegistrantId = "uiDrawManagerPlugin_py"
43 
44  ## ui type attribute
45  aUIType = None
46  aSelectability = None
47 
48  ## color
49  aPrimitiveColor = None
50  aPrimitiveTransparency = None
51 
52  ## line width and line style attributes
53  aLineWidth = None
54  aLineStyle = None
55 
56  ## fill attribute
57  aIsFilled = None
58 
59  ## radius attribute
60  aRadius = None
61 
62  ## height attribute
63  aHeight = None
64 
65  ## 2D attributes
66  aDraw2D = None
67  aPosition = None
68 
69  ## Text attributes
70  aTextAlignment = None
71  eTextIncline = None
72  aTextWeight = None
73  aTextStretch = None
74  aTextLine = None
75  aTextBoxSize = None
76  aText = None
77  aTextBoxColor = None
78  aTextBoxTransparency = None
79  aTextFontSize = None
80  aFontFaceName = None
81 
82  ## point attributes
83  aPointSize = None
84 
85  ## line attributes
86  aLineStartPoint = None
87  aLineEndPoint = None
88 
89  ## rect attributes
90  aRectUp = None
91  aRectNormal = None
92  aRectScale = None
93 
94  ## quad attributes
95  aQuadVertex = [None, None, None, None]
96 
97  ## circle attributes
98  aCircleNormal = None
99 
100  ## arc attributes
101  aArcStart = None
102  aArcEnd = None
103  aArcNormal = None
104 
105  fFontList = None
106 
107  ## subdivision attributes
108  aSubdivisionsAxis = 10
109  aSubdivisionsHeight = 4
110  aNumSubdivisions = 10
111 
112  ## cone attributes
113  aConeDirection = None
114 
115  # cylinder attirbutes
116  aCylinderUp = None
117 
118  # capsule attributes
119  aCapsuleUp = None
120 
121  def __init__(self):
122  omui.MPxLocatorNode.__init__(self)
123 
124  @staticmethod
125  def creator():
126  return uiDrawManager()
127 
128  @staticmethod
129  def initialize():
130  nAttr = om.MFnNumericAttribute()
131  eAttr = om.MFnEnumAttribute()
132  typedAttr = om.MFnTypedAttribute()
133 
134  ## Add ui type attribute
135  uiDrawManager.aUIType = eAttr.create("uiType", "ut", kText)
136  eAttr.addField("text", kText)
137  eAttr.addField("line", kLine)
138  eAttr.addField("point", kPoint)
139  eAttr.addField("rect", kRect)
140  eAttr.addField("quad", kQuad)
141  eAttr.addField("sphere", kSphere)
142  eAttr.addField("circle", kCircle)
143  eAttr.addField("arc", kArc)
144  eAttr.addField("sphere2", kSphere2)
145  eAttr.addField("circle2", kCircle2)
146  eAttr.addField("arc2", kArc2)
147  eAttr.addField("cone", kCone)
148  eAttr.addField("cylinder", kCylinder)
149  eAttr.addField("capsule", kCapsule)
150 
151  om.MPxNode.addAttribute(uiDrawManager.aUIType)
152 
153  ## Add selectability
154  uiDrawManager.aSelectability = eAttr.create("selectability", "st", omr.MUIDrawManager.kAutomatic)
155  eAttr.addField("Unselectable", omr.MUIDrawManager.kNonSelectable)
156  eAttr.addField("Selectable", omr.MUIDrawManager.kSelectable)
157  eAttr.addField("Automatic", omr.MUIDrawManager.kAutomatic)
158  om.MPxNode.addAttribute(uiDrawManager.aSelectability)
159 
160  ## Add color attribute
161  uiDrawManager.aPrimitiveColor = nAttr.create("primitiveColor", "pc", om.MFnNumericData.k3Float)
162  nAttr.default = (1.0, 0.0, 0.0)
163  nAttr.usedAsColor = True
164  om.MPxNode.addAttribute(uiDrawManager.aPrimitiveColor)
165 
166  ## Add transparency attribute
167  uiDrawManager.aPrimitiveTransparency = nAttr.create("primitiveTransparency", "pt", om.MFnNumericData.kFloat, 0.0)
168  nAttr.setSoftMin(0.0)
169  nAttr.setSoftMax(1.0)
170  om.MPxNode.addAttribute(uiDrawManager.aPrimitiveTransparency)
171 
172  ## add line width and line style attributes
173  uiDrawManager.aLineWidth = nAttr.create("lineWidth", "lw", om.MFnNumericData.kFloat, 2.0)
174  om.MPxNode.addAttribute(uiDrawManager.aLineWidth)
175 
176  uiDrawManager.aLineStyle = eAttr.create("lineStyle", "ls", omr.MUIDrawManager.kSolid)
177  eAttr.addField("solid", omr.MUIDrawManager.kSolid)
178  eAttr.addField("shortdotted", omr.MUIDrawManager.kShortDotted)
179  eAttr.addField("shortdashed", omr.MUIDrawManager.kShortDashed)
180  eAttr.addField("dashed", omr.MUIDrawManager.kDashed)
181  eAttr.addField("dotted", omr.MUIDrawManager.kDotted)
182  om.MPxNode.addAttribute(uiDrawManager.aLineStyle)
183 
184  ## Add filled attribute
185  uiDrawManager.aIsFilled = nAttr.create("isFilled", "if", om.MFnNumericData.kBoolean, 0)
186  om.MPxNode.addAttribute(uiDrawManager.aIsFilled)
187 
188  ## Add radius attribute
189  uiDrawManager.aRadius = nAttr.create("radius", "ra", om.MFnNumericData.kDouble, 1.0)
190  om.MPxNode.addAttribute(uiDrawManager.aRadius)
191 
192  ## Add height attribute
193  uiDrawManager.aHeight = nAttr.create("height", "he", om.MFnNumericData.kDouble, 1.0)
194  om.MPxNode.addAttribute(uiDrawManager.aHeight)
195 
196  ## add 2D attributes
197  uiDrawManager.aDraw2D = nAttr.create("draw2D", "d2", om.MFnNumericData.kBoolean, 0)
198  om.MPxNode.addAttribute(uiDrawManager.aDraw2D)
199 
200  uiDrawManager.aPosition = nAttr.create("position", "pos", om.MFnNumericData.k3Double)
201  nAttr.default = (0.0, 0.0, 0.001)
202  om.MPxNode.addAttribute(uiDrawManager.aPosition)
203 
204  ## Add text attributes.
205  stringFn = om.MFnStringData()
206  defaultText = stringFn.create("uiDrawManager-Text")
207  uiDrawManager.aText = typedAttr.create("text", "t", om.MFnData.kString, defaultText)
208  om.MPxNode.addAttribute(uiDrawManager.aText)
209 
210  uiDrawManager.aTextFontSize = nAttr.create("textFontSize", "tfs", om.MFnNumericData.kInt, omr.MUIDrawManager.kDefaultFontSize)
211  nAttr.setMin(-1)
212  nAttr.setMax(99)
213  om.MPxNode.addAttribute(uiDrawManager.aTextFontSize)
214 
215  uiDrawManager.fFontList = omr.MUIDrawManager.getFontList()
216  if len(uiDrawManager.fFontList) == 0:
217  sys.stderr.write("No font available!\n")
218 
219  uiDrawManager.aFontFaceName = eAttr.create("fontFaceName", "ffn", 0)
220  for i in range(len(uiDrawManager.fFontList)):
221  faceName = uiDrawManager.fFontList[i]
222  eAttr.addField(faceName, i)
223  om.MPxNode.addAttribute(uiDrawManager.aFontFaceName)
224 
225  uiDrawManager.aTextAlignment = eAttr.create("textAlignment", "ta", omr.MUIDrawManager.kLeft)
226  eAttr.addField("left", omr.MUIDrawManager.kLeft)
227  eAttr.addField("center", omr.MUIDrawManager.kCenter)
228  eAttr.addField("right", omr.MUIDrawManager.kRight)
229  om.MPxNode.addAttribute(uiDrawManager.aTextAlignment)
230 
231  uiDrawManager.eTextIncline = eAttr.create("textIncline", "tic", omr.MUIDrawManager.kInclineNormal)
232  eAttr.addField("normal", omr.MUIDrawManager.kInclineNormal)
233  eAttr.addField("italic", omr.MUIDrawManager.kInclineItalic)
234  om.MPxNode.addAttribute(uiDrawManager.eTextIncline)
235 
236  uiDrawManager.aTextWeight = eAttr.create("textWeight", "tw", omr.MUIDrawManager.kWeightBold)
237  eAttr.addField("light", omr.MUIDrawManager.kWeightLight)
238  eAttr.addField("bold", omr.MUIDrawManager.kWeightBold)
239  om.MPxNode.addAttribute(uiDrawManager.aTextWeight)
240 
241  uiDrawManager.aTextStretch = nAttr.create("textStretch", "ts", om.MFnNumericData.kInt, omr.MUIDrawManager.kStretchUnstretched)
242  nAttr.setMin(50)
243  nAttr.setMax(200)
244  om.MPxNode.addAttribute(uiDrawManager.aTextStretch)
245 
246  uiDrawManager.aTextLine = eAttr.create("textLine", "tl", 0)
247  eAttr.addField("none", 0)
248  eAttr.addField("overline", omr.MUIDrawManager.kLineOverline)
249  eAttr.addField("underline", omr.MUIDrawManager.kLineUnderline)
250  eAttr.addField("strikeout", omr.MUIDrawManager.kLineStrikeoutLine)
251  om.MPxNode.addAttribute(uiDrawManager.aTextLine)
252 
253  uiDrawManager.aTextBoxSize = nAttr.create("textBoxSize", "tbs", om.MFnNumericData.k2Int)
254  nAttr.default = (0, 0)
255  om.MPxNode.addAttribute(uiDrawManager.aTextBoxSize)
256 
257  uiDrawManager.aTextBoxColor = nAttr.create("textBoxColor", "tbc", om.MFnNumericData.k3Float)
258  nAttr.default = (0.0, 1.0, 1.0)
259  nAttr.usedAsColor = True
260  om.MPxNode.addAttribute(uiDrawManager.aTextBoxColor)
261 
262  uiDrawManager.aTextBoxTransparency = nAttr.create("textBoxTransparency", "tbt", om.MFnNumericData.kFloat, 0.0)
263  nAttr.setSoftMin(0.0)
264  nAttr.setSoftMax(1.0)
265  om.MPxNode.addAttribute(uiDrawManager.aTextBoxTransparency)
266 
267  ## add point attributes
268  uiDrawManager.aPointSize = nAttr.create("pointSize", "ps", om.MFnNumericData.kFloat, 2.0)
269  om.MPxNode.addAttribute(uiDrawManager.aPointSize)
270 
271  ## add line attributes
272  uiDrawManager.aLineStartPoint = nAttr.create("lineStartPoint", "lsp", om.MFnNumericData.k3Double)
273  nAttr.default = (0.0, 0.0, 0.0)
274  om.MPxNode.addAttribute(uiDrawManager.aLineStartPoint)
275 
276  uiDrawManager.aLineEndPoint = nAttr.create("lineEndPoint", "lep", om.MFnNumericData.k3Double)
277  nAttr.default = (1.0, 1.0, 1.0)
278  om.MPxNode.addAttribute(uiDrawManager.aLineEndPoint)
279 
280  ## add rect attributes
281  uiDrawManager.aRectUp = nAttr.create("rectUp", "ru", om.MFnNumericData.k3Double)
282  nAttr.default = (0.0, 1.0, 0.0)
283  om.MPxNode.addAttribute(uiDrawManager.aRectUp)
284 
285  uiDrawManager.aRectNormal = nAttr.create("rectNormal", "rn", om.MFnNumericData.k3Double)
286  nAttr.default = (0.0, 0.0, 1.0)
287  om.MPxNode.addAttribute(uiDrawManager.aRectNormal)
288 
289  uiDrawManager.aRectScale = nAttr.create("rectScale", "rs", om.MFnNumericData.k2Double)
290  nAttr.default = (1.0, 1.0)
291  om.MPxNode.addAttribute(uiDrawManager.aRectScale)
292 
293  ## add quad attributes
294  defaultPosition = [ (0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (1.0, 1.0, 0.0), (0.0, 1.0, 0.0) ]
295  for i in range(4):
296  fullName = "quadVertex" + str(i)
297  shortName = "qv" + str(i)
298  uiDrawManager.aQuadVertex[i] = nAttr.create(fullName, shortName, om.MFnNumericData.k3Double)
299  nAttr.default = defaultPosition[i]
300  om.MPxNode.addAttribute(uiDrawManager.aQuadVertex[i])
301 
302  ## add circle attributes
303  uiDrawManager.aCircleNormal = nAttr.create("circleNormal", "cn", om.MFnNumericData.k3Double)
304  nAttr.default = (0.0, 0.0, 1.0)
305  om.MPxNode.addAttribute(uiDrawManager.aCircleNormal)
306 
307  ## add arc attributes
308  uiDrawManager.aArcStart = nAttr.create("arcStartVector", "asv", om.MFnNumericData.k3Double)
309  nAttr.default = (1.0, 0.0, 0.0)
310  om.MPxNode.addAttribute(uiDrawManager.aArcStart)
311 
312  uiDrawManager.aArcEnd = nAttr.create("arcEndVector", "aev", om.MFnNumericData.k3Double)
313  nAttr.default = (0.0, 1.0, 0.0)
314  om.MPxNode.addAttribute(uiDrawManager.aArcEnd)
315 
316  uiDrawManager.aArcNormal = nAttr.create("arcNormal", "an", om.MFnNumericData.k3Double)
317  nAttr.default = (0.0, 0.0, 1.0)
318  om.MPxNode.addAttribute(uiDrawManager.aArcNormal)
319 
320  ## add subdivisionAxis, subdivisionsHeight, numSubdivisions attributes
321  uiDrawManager.aSubdivisionsAxis = nAttr.create("subdivisionAxis", "sda", om.MFnNumericData.kInt, 10)
322  om.MPxNode.addAttribute(uiDrawManager.aSubdivisionsAxis)
323 
324  uiDrawManager.aSubdivisionsHeight = nAttr.create("subdivisionsHeight", "sdh", om.MFnNumericData.kInt, 4)
325  om.MPxNode.addAttribute(uiDrawManager.aSubdivisionsHeight)
326 
327  uiDrawManager.aNumSubdivisions = nAttr.create("numSubdivisions", "sdn", om.MFnNumericData.kInt, 10)
328  om.MPxNode.addAttribute(uiDrawManager.aNumSubdivisions)
329 
330  ## add cone attributes
331  uiDrawManager.aConeDirection = nAttr.create("coneDirection", "cd", om.MFnNumericData.k3Double)
332  nAttr.default = (0.0, 1.0, 0.0)
333  om.MPxNode.addAttribute(uiDrawManager.aConeDirection)
334 
335  # add cylinder attributes
336  uiDrawManager.aCylinderUp = nAttr.create("cylinderUp", "cu", om.MFnNumericData.k3Double)
337  nAttr.default = (0.0, 1.0, 0.0)
338  om.MPxNode.addAttribute(uiDrawManager.aCylinderUp)
339 
340  #add capsule attributes
341  uiDrawManager.aCapsuleUp = nAttr.create("capsuleUp", "cau", om.MFnNumericData.k3Double)
342  nAttr.default = (0.0, 1.0, 0.0)
343  om.MPxNode.addAttribute(uiDrawManager.aCapsuleUp)
344 
345 
346 
347 
348 ##---------------------------------------------------------------------------
349 ##---------------------------------------------------------------------------
350 ## Viewport 2.0 override implementation
351 ##---------------------------------------------------------------------------
352 ##---------------------------------------------------------------------------
353 class uiDrawManagerData(om.MUserData):
354  def __init__(self):
355  om.MUserData.__init__(self, False)
356  self.fUIType = kText
357  self.fSelectability = omr.MUIDrawManager.kAutomatic
358  self.fColor = om.MColor((1., 0., 0., 1.))
359  self.fLineWidth = 2
360  self.fLineStyle = omr.MUIDrawManager.kSolid
361  self.fIsFilled = False
362  self.fRadius = 1.
363  self.fHeight = 1.
364  self.fDraw2D = False
365  self.fPosition = om.MPoint(0.0, 0.0, 0.001)
366  # text
367  self.fText = "uiDrawManager-Text"
368  self.fTextFontSize = omr.MUIDrawManager.kDefaultFontSize
369  self.fFontFaceIndex = 0
370  self.fTextAlignment = omr.MUIDrawManager.kLeft
371  self.fTextIncline = omr.MUIDrawManager.kInclineNormal
372  self.fTextWeight = omr.MUIDrawManager.kWeightBold
373  self.fTextStretch = omr.MUIDrawManager.kStretchUnstretched
374  self.fTextLine = 0
375  self.fTextBoxSize = [0, 0]
376  self.fTextBoxColor = om.MColor((0., 1., 1., 1.))
377  # point
378  self.fPointSize = 2.
379  # line
380  self.fLineStartPoint = om.MPoint(0.0, 0.0, 0.0)
381  self.fLineEndPoint = om.MPoint(1.0, 1.0, 1.0)
382  # rect
383  self.fRectUp = om.MVector(0.0, 1.0, 0.0)
384  self.fRectNormal = om.MVector(0.0, 0.0, 1.0)
385  self.fRectScale = [1., 1.]
386  # quad
387  self.fQuadVertex = [ om.MPoint(0.0, 0.0, 0.0), om.MPoint(1.0, 0.0, 0.0), om.MPoint(1.0, 1.0, 0.0), om.MPoint(0.0, 1.0, 0.0) ]
388  # circle
389  self.fCircleNormal = om.MVector(0.0, 0.0, 1.0)
390  # arc
391  self.fArcStart = om.MVector(1.0, 0.0, 0.0)
392  self.fArcEnd = om.MVector(0.0, 1.0, 0.0)
393  self.fArcNormal = om.MVector(0.0, 0.0, 1.0)
394 
395  # subdivisions
396  self.fSubdivisionsAxis = 10
397  self.fSubdivisionsHeight = 4
398  self.fNumSubdivisions = 10
399 
400  # cone
401  self.fConeDirection = om.MVector(0.0, 0.0, 1.0)
402  # cylinder
403  self.fCylinderUp = om.MVector(0.0, 1.0, 0.0)
404  # capsule
405  self.fCapsuleUp = om.MVector(0.0, 0.0, 1.0)
406 
407 
408 ################################################################################################
409 class uiDrawManagerDrawOverride(omr.MPxDrawOverride):
410  def __init__(self, obj):
411  omr.MPxDrawOverride.__init__(self, obj, None)
412 
413  @staticmethod
414  def creator(obj):
415  return uiDrawManagerDrawOverride(obj)
416 
417  def supportedDrawAPIs(self):
418  return (omr.MRenderer.kOpenGL | omr.MRenderer.kDirectX11 | omr.MRenderer.kOpenGLCoreProfile)
419 
420  def isBounded(self, objPath, cameraPath):
421  return False
422 
423  def boundingBox(self, objPath, cameraPath):
424  return om.MBoundingBox()
425 
426  def prepareForDraw(self, objPath, cameraPath, frameContext, oldData):
427  data = oldData
428  if not isinstance(data, uiDrawManagerData):
429  data = uiDrawManagerData()
430 
431  uiDrawManagerNode = objPath.node()
432  if uiDrawManagerNode.isNull():
433  return
434 
435  ## retrieve uiType
436  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aUIType)
437  data.fUIType = plug.asShort()
438 
439  ## common attributes
440  ## retrieve selectability
441  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aSelectability)
442  data.fSelectability = plug.asShort()
443 
444  ## retrieve color
445  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aPrimitiveColor)
446  o = plug.asMObject()
447  nData = om.MFnNumericData(o)
448  data.fColor = om.MColor(nData.getData())
449 
450  ## retrieve transparency
451  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aPrimitiveTransparency)
452  data.fColor.a = 1.0 - plug.asFloat()
453 
454  ## retrieve line width
455  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aLineWidth)
456  data.fLineWidth = plug.asFloat()
457 
458  ## retrieve line style
459  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aLineStyle)
460  data.fLineStyle = plug.asShort()
461 
462  ## retrieve filled flag
463  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aIsFilled)
464  data.fIsFilled = plug.asBool()
465 
466  ## retrieve radius
467  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aRadius)
468  data.fRadius = plug.asDouble()
469 
470  ## retrieve height
471  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aHeight)
472  data.fHeight = plug.asDouble()
473 
474  ## retrieve 2D flag
475  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aDraw2D)
476  data.fDraw2D = plug.asBool()
477 
478  ## retrieve screen position
479  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aPosition)
480  o = plug.asMObject()
481  nData = om.MFnNumericData(o)
482  data.fPosition = om.MPoint(nData.getData())
483  data.fPosition.w = 1.0
484 
485  ## retrieve subdivisions
486  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aSubdivisionsAxis)
487  data.fSubdivisionAxis = plug.asInt()
488 
489  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aSubdivisionsHeight)
490  data.fSubdivisionHeight = plug.asInt()
491 
492  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aNumSubdivisions)
493  data.fNumSubdivisions = plug.asInt()
494 
495  if data.fUIType == kText:
496  ## retrieve text
497  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aText)
498  data.fText = plug.asString()
499 
500  ## retrieve text font size
501  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aTextFontSize)
502  data.fTextFontSize = max(plug.asInt(), 0)
503 
504  ## retrieve font face index
505  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aFontFaceName)
506  data.fFontFaceIndex = plug.asInt()
507 
508  ## retrieve text alignment
509  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aTextAlignment)
510  data.fTextAlignment = plug.asShort()
511 
512  ## retrieve text incline
513  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.eTextIncline)
514  data.fTextIncline = plug.asInt()
515 
516  ## retrieve text weight
517  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aTextWeight)
518  data.fTextWeight = plug.asInt()
519 
520  ## retrieve text stretch
521  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aTextStretch)
522  data.fTextStretch = plug.asInt()
523 
524  ## retrieve text line
525  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aTextLine)
526  data.fTextLine = plug.asInt()
527 
528  ## retrieve text box size
529  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aTextBoxSize)
530  o = plug.asMObject()
531  nData = om.MFnNumericData(o)
532  data.fTextBoxSize = nData.getData()
533 
534  ## retrieve text box color
535  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aTextBoxColor)
536  o = plug.asMObject()
537  nData = om.MFnNumericData(o)
538  data.fTextBoxColor = om.MColor(nData.getData())
539 
540  ## retrieve text box transparency
541  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aTextBoxTransparency)
542  data.fTextBoxColor.a = 1.0 - plug.asFloat()
543 
544  elif data.fUIType == kPoint:
545  ## retrieve point size
546  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aPointSize)
547  data.fPointSize = plug.asFloat()
548 
549  elif data.fUIType == kLine:
550  ## retrieve line start point
551  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aLineStartPoint)
552  o = plug.asMObject()
553  nData = om.MFnNumericData(o)
554  data.fLineStartPoint = om.MPoint(nData.getData())
555  data.fLineStartPoint.w = 1.0
556 
557  ## retrieve line end point
558  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aLineEndPoint)
559  o = plug.asMObject()
560  nData = om.MFnNumericData(o)
561  data.fLineEndPoint = om.MPoint(nData.getData())
562  data.fLineEndPoint.w = 1.0
563 
564  elif data.fUIType == kRect:
565  ## retrieve rect up vector
566  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aRectUp)
567  o = plug.asMObject()
568  nData = om.MFnNumericData(o)
569  data.fRectUp = om.MVector(nData.getData())
570 
571  ## retrieve rect normal vector
572  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aRectNormal)
573  o = plug.asMObject()
574  nData = om.MFnNumericData(o)
575  data.fRectNormal = om.MVector(nData.getData())
576 
577  ## retrieve rect scale
578  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aRectScale)
579  o = plug.asMObject()
580  nData = om.MFnNumericData(o)
581  data.fRectScale = nData.getData()
582 
583  elif data.fUIType == kQuad:
584  ## retrieve quad vertices
585  for i in range(4):
586  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aQuadVertex[i])
587  o = plug.asMObject()
588  nData = om.MFnNumericData(o)
589  data.fQuadVertex[i] = om.MPoint(nData.getData())
590 
591  elif data.fUIType == kCircle:
592  ## retrieve circle normal
593  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aCircleNormal)
594  o = plug.asMObject()
595  nData = om.MFnNumericData(o)
596  data.fCircleNormal = om.MVector(nData.getData())
597 
598  elif data.fUIType == kArc:
599  ## retrieve arc start vector
600  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aArcStart)
601  o = plug.asMObject()
602  nData = om.MFnNumericData(o)
603  data.fArcStart = om.MVector(nData.getData())
604 
605  ## retrieve arc end vector
606  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aArcEnd)
607  o = plug.asMObject()
608  nData = om.MFnNumericData(o)
609  data.fArcEnd = om.MVector(nData.getData())
610 
611  ## retrieve arc normal
612  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aArcNormal)
613  o = plug.asMObject()
614  nData = om.MFnNumericData(o)
615  data.fArcNormal = om.MVector(nData.getData())
616 
617  elif data.fUIType == kSphere:
618  pass
619 
620  elif data.fUIType == kCone:
621  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aConeDirection)
622  o = plug.asMObject()
623  nData = om.MFnNumericData(o)
624  data.fConeDirection = om.MVector(nData.getData())
625  elif data.fUIType == kCylinder:
626  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aCylinderUp)
627  o = plug.asMObject()
628  nData = om.MFnNumericData(o)
629  data.fCylinderUp = om.MVector(nData.getData())
630  elif data.fUIType == kCapsule:
631  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aCapsuleUp)
632  o = plug.asMObject()
633  nData = om.MFnNumericData(o)
634  data.fCapsuleUp = om.MVector(nData.getData())
635  elif data.fUIType == kSphere2:
636  pass
637  elif data.fUIType == kCircle2:
638  pass
639  elif data.fUIType == kArc2:
640  ## retrieve arc start vector
641  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aArcStart)
642  o = plug.asMObject()
643  nData = om.MFnNumericData(o)
644  data.fArcStart = om.MVector(nData.getData())
645 
646  ## retrieve arc end vector
647  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aArcEnd)
648  o = plug.asMObject()
649  nData = om.MFnNumericData(o)
650  data.fArcEnd = om.MVector(nData.getData())
651 
652  ## retrieve arc normal
653  plug = om.MPlug(uiDrawManagerNode, uiDrawManager.aArcNormal)
654  o = plug.asMObject()
655  nData = om.MFnNumericData(o)
656  data.fArcNormal = om.MVector(nData.getData())
657  else:
658  sys.stderr.write("unhandled ui type.\n")
659 
660  return data
661 
662  def hasUIDrawables(self):
663  return True
664 
665  def addUIDrawables(self, objPath, drawManager, frameContext, data):
666  if not isinstance(data, uiDrawManagerData):
667  return
668 
669  if data.fUIType == kText:
670  ## Draw a text "uiDrawManager"
671  ## All drawing operations must take place between calls to beginDrawable()
672  ## and endDrawable().
673  drawManager.beginDrawable(data.fSelectability)
674 
675  drawManager.setColor(data.fColor)
676  drawManager.setFontSize(data.fTextFontSize)
677  drawManager.setFontIncline(data.fTextIncline)
678  drawManager.setFontWeight(data.fTextWeight)
679  drawManager.setFontStretch(data.fTextStretch)
680  drawManager.setFontLine(data.fTextLine)
681 
682  faceName = uiDrawManager.fFontList[data.fFontFaceIndex]
683  drawManager.setFontName(faceName)
684 
685  boxSize = data.fTextBoxSize
686  if boxSize[0] + boxSize[1] == 0:
687  boxSize = None
688 
689  if data.fDraw2D:
690  ## uiDrawManagerData::fPosition gives a screen space position
691  ## where 2D UI item is located.
692  drawManager.text2d(data.fPosition, data.fText, data.fTextAlignment, boxSize, data.fTextBoxColor, False)
693 
694  else:
695  ## for 3D items, place it at the origin of the world space.
696  drawManager.text(data.fPosition, data.fText, data.fTextAlignment, boxSize, data.fTextBoxColor, False)
697 
698  drawManager.endDrawable()
699 
700  elif data.fUIType == kLine:
701  drawManager.beginDrawable(data.fSelectability)
702 
703  drawManager.setColor(data.fColor)
704  drawManager.setLineWidth(data.fLineWidth)
705  drawManager.setLineStyle(data.fLineStyle)
706 
707  if data.fDraw2D:
708  drawManager.line2d(data.fLineStartPoint, data.fLineEndPoint)
709 
710  else:
711  drawManager.line(data.fLineStartPoint, data.fLineEndPoint)
712 
713  drawManager.endDrawable()
714 
715  elif data.fUIType == kPoint:
716  drawManager.beginDrawable(data.fSelectability)
717 
718  drawManager.setColor(data.fColor)
719  drawManager.setPointSize(data.fPointSize)
720 
721  if data.fDraw2D:
722  drawManager.point2d(data.fPosition)
723 
724  else:
725  drawManager.point(data.fPosition)
726 
727  drawManager.endDrawable()
728 
729  elif data.fUIType == kRect:
730  drawManager.beginDrawable(data.fSelectability)
731 
732  drawManager.setColor(data.fColor)
733  drawManager.setLineWidth(data.fLineWidth)
734  drawManager.setLineStyle(data.fLineStyle)
735 
736  if data.fDraw2D:
737  ## For 2d rectangle, an up vector in screen space is used to determine its X
738  ## and Y directions. In addition, "fRectScale" (in pixels)
739  ## specify the half-lengths of the 2d rectangle.
740  drawManager.rect2d(data.fPosition, data.fRectUp, data.fRectScale[0], data.fRectScale[1], data.fIsFilled)
741 
742  else:
743  ## For 3d rectangle, the up vector should not be parallel with the normal vector.
744  drawManager.rect(data.fPosition, data.fRectUp, data.fRectNormal, data.fRectScale[0], data.fRectScale[1], data.fIsFilled)
745 
746  drawManager.endDrawable()
747 
748  elif data.fUIType == kQuad:
749  drawManager.beginDrawable(data.fSelectability)
750 
751  drawManager.setColor(data.fColor)
752  drawManager.setLineWidth(data.fLineWidth)
753  drawManager.setLineStyle(data.fLineStyle)
754 
755  ## prepare primitive type
756  mode = omr.MUIDrawManager.kClosedLine
757  if data.fIsFilled:
758  mode = omr.MUIDrawManager.kTriStrip
759 
760  ## prepare index
761  index = None
762  if data.fIsFilled:
763  index = om.MUintArray()
764  index.append(0)
765  index.append(1)
766  index.append(3)
767  index.append(2)
768 
769  ## prepare position list
770  position = om.MPointArray()
771  for i in range(4):
772  position.append(data.fQuadVertex[i])
773 
774  ## draw mesh
775  if data.fDraw2D:
776  drawManager.mesh2d(mode, position, None, index)
777 
778  else:
779  drawManager.mesh(mode, position, None, None, index)
780 
781  drawManager.endDrawable()
782 
783  elif data.fUIType == kSphere:
784  drawManager.beginDrawable(data.fSelectability)
785 
786  drawManager.setColor(data.fColor)
787  drawManager.setLineWidth(data.fLineWidth)
788  drawManager.setLineStyle(data.fLineStyle)
789 
790  drawManager.sphere(data.fPosition, data.fRadius, data.fIsFilled)
791 
792  drawManager.endDrawable()
793 
794  elif data.fUIType == kCircle:
795  drawManager.beginDrawable(data.fSelectability)
796 
797  drawManager.setColor(data.fColor)
798  drawManager.setLineWidth(data.fLineWidth)
799  drawManager.setLineStyle(data.fLineStyle)
800 
801  if data.fDraw2D:
802  ## The radius in specified as pixel unit for 2d items.
803  drawManager.circle2d(data.fPosition, data.fRadius, data.fIsFilled)
804 
805  else:
806  drawManager.circle(data.fPosition, data.fCircleNormal, data.fRadius, data.fIsFilled)
807 
808  drawManager.endDrawable()
809 
810  elif data.fUIType == kArc:
811  drawManager.beginDrawable(data.fSelectability)
812 
813  drawManager.setColor(data.fColor)
814  drawManager.setLineWidth(data.fLineWidth)
815  drawManager.setLineStyle(data.fLineStyle)
816 
817  if data.fDraw2D:
818  ## If 2d, the range of the arc is defined by the start and end vectors
819  ## specified in screen space.
820  drawManager.arc2d(data.fPosition, data.fArcStart, data.fArcEnd, data.fRadius, data.fIsFilled)
821 
822  else:
823  ## For 3d arc, the projections of the start and end vectors onto the arc plane(
824  ## determined by the normal vector) determine the range of the arc.
825  drawManager.arc(data.fPosition, data.fArcStart, data.fArcEnd, data.fArcNormal, data.fRadius, data.fIsFilled)
826 
827  drawManager.endDrawable()
828  elif data.fUIType == kSphere2:
829  drawManager.beginDrawable(data.fSelectability)
830 
831  drawManager.setColor(data.fColor)
832  drawManager.setLineWidth(data.fLineWidth)
833  drawManager.setLineStyle(data.fLineStyle)
834 
835  drawManager.sphere(data.fPosition, data.fRadius, data.fSubdivisionAxis, data.fSubdivisionHeight, data.fIsFilled)
836 
837  drawManager.endDrawable()
838  elif data.fUIType == kCircle2:
839  drawManager.beginDrawable(data.fSelectability)
840 
841  drawManager.setColor(data.fColor)
842  drawManager.setLineWidth(data.fLineWidth)
843  drawManager.setLineStyle(data.fLineStyle)
844 
845  if data.fDraw2D:
846  ## The radius in specified as pixel unit for 2d items.
847  drawManager.circle2d(data.fPosition, data.fRadius, data.fNumSubdivisions, data.fIsFilled)
848 
849  else:
850  drawManager.circle(data.fPosition, data.fCircleNormal, data.fRadius, data.fNumSubdivisions, data.fIsFilled)
851 
852  drawManager.endDrawable()
853  elif data.fUIType == kArc2:
854  drawManager.beginDrawable(data.fSelectability)
855 
856  drawManager.setColor(data.fColor)
857  drawManager.setLineWidth(data.fLineWidth)
858  drawManager.setLineStyle(data.fLineStyle)
859 
860  if data.fDraw2D:
861  ## If 2d, the range of the arc is defined by the start and end vectors
862  ## specified in screen space.
863  drawManager.arc2d(data.fPosition, data.fArcStart, data.fArcEnd, data.fRadius, data.fNumSubdivisions, data.fIsFilled)
864 
865  else:
866  ## For 3d arc, the projections of the start and end vectors onto the arc plane(
867  ## determined by the normal vector) determine the range of the arc.
868  drawManager.arc(data.fPosition, data.fArcStart, data.fArcEnd, data.fArcNormal, data.fRadius, data.fNumSubdivisions, data.fIsFilled)
869 
870  drawManager.endDrawable()
871  elif data.fUIType == kCone:
872  drawManager.beginDrawable(data.fSelectability)
873 
874  drawManager.setColor(data.fColor)
875  drawManager.setLineWidth(data.fLineWidth)
876  drawManager.setLineStyle(data.fLineStyle)
877 
878  drawManager.cone(data.fPosition, data.fConeDirection, data.fRadius, data.fNumSubdivisions, data.fIsFilled)
879 
880  drawManager.endDrawable()
881  elif data.fUIType == kCylinder:
882  drawManager.beginDrawable(data.fSelectability)
883 
884  drawManager.setColor(data.fColor)
885  drawManager.setLineWidth(data.fLineWidth)
886  drawManager.setLineStyle(data.fLineStyle)
887 
888  drawManager.cylinder(data.fPosition, data.fCylinderUp, data.fRadius, data.fHeight, data.fSubdivisionAxis, data.fIsFilled)
889 
890  drawManager.endDrawable()
891  elif data.fUIType == kCapsule:
892  drawManager.beginDrawable(data.fSelectability)
893 
894  drawManager.setColor(data.fColor)
895  drawManager.setLineWidth(data.fLineWidth)
896  drawManager.setLineStyle(data.fLineStyle)
897 
898  drawManager.capsule(data.fPosition, om.MVector(0.0, 1.0, 0.0), data.fRadius, data.fHeight, 36, 20, data.fIsFilled)
899 
900  drawManager.endDrawable()
901  else:
902  sys.stderr.write("unhandled ui type.\n")
903 
904 
905 def initializePlugin(obj):
906  plugin = om.MFnPlugin(obj, "Autodesk", "3.0", "Any")
907  try:
908  plugin.registerNode("uiDrawManager_py", uiDrawManager.id, uiDrawManager.creator, uiDrawManager.initialize, om.MPxNode.kLocatorNode, uiDrawManager.drawDbClassification)
909  except:
910  sys.stderr.write("Failed to register node\n")
911  raise
912 
913  try:
914  omr.MDrawRegistry.registerDrawOverrideCreator(uiDrawManager.drawDbClassification, uiDrawManager.drawRegistrantId, uiDrawManagerDrawOverride.creator)
915  except:
916  sys.stderr.write("Failed to register override\n")
917  raise
918 
919 def uninitializePlugin(obj):
920  plugin = om.MFnPlugin(obj)
921  try:
922  plugin.deregisterNode(uiDrawManager.id)
923  except:
924  sys.stderr.write("Failed to deregister node\n")
925  raise
926 
927  try:
928  omr.MDrawRegistry.deregisterGeometryOverrideCreator(uiDrawManager.drawDbClassification, uiDrawManager.drawRegistrantId)
929  except:
930  sys.stderr.write("Failed to deregister override\n")
931  raise
932