scripted/pyUiDrawManager.py

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