13 from OpenGL.GL
import *
16 import maya.api.OpenMaya
as om
17 import maya.api.OpenMayaUI
as omui
18 import maya.api.OpenMayaRender
as omr
22 The presence of this function tells Maya that the plugin produces, and
23 expects to be passed, objects created using the Maya Python API 2.0.
28 def matrixAsArray(matrix):
31 array.append(matrix[i])
34 def drawBoundingBox(box, color):
35 min = om.MFloatPoint( box.min )
36 max = om.MFloatPoint( box.max )
38 glPushAttrib(GL_ALL_ATTRIB_BITS)
40 glDisable( GL_LIGHTING )
41 glColor3f( color.r, color.g, color.b)
42 glBegin( GL_LINE_STRIP )
43 glVertex3f( min.x, min.y, min.z )
44 glVertex3f( max.x, min.y, min.z )
45 glVertex3f( max.x, max.y, min.z )
46 glVertex3f( min.x, max.y, min.z )
47 glVertex3f( min.x, min.y, min.z )
48 glVertex3f( min.x, min.y, max.z )
49 glVertex3f( min.x, max.y, max.z )
50 glVertex3f( min.x, max.y, min.z )
51 glVertex3f( max.x, max.y, min.z )
52 glVertex3f( max.x, max.y, max.z )
53 glVertex3f( max.x, min.y, max.z )
54 glVertex3f( max.x, min.y, min.z )
55 glVertex3f( max.x, min.y, max.z )
56 glVertex3f( min.x, min.y, max.z )
57 glVertex3f( min.x, max.y, max.z )
58 glVertex3f( max.x, max.y, max.z )
63 class hwColorPerVertexShader(omui.MPxHwShaderNode):
64 id = om.MTypeId(0x00105450)
71 aNormalsPerVertex =
None
72 aColorsPerVertex =
None
77 aDrawBoundingBox =
None
80 omui.MPxHwShaderNode.__init__(self)
83 self.mColorGain = [1.0, 0.0, 0.0]
84 self.mColorBias = [0.0, 0.0, 0.0]
85 self.mTranspGain = 1.0
86 self.mTranspBias = 0.0
88 self.mNormalsPerVertex = 0
89 self.mColorsPerVertex = 0
90 self.mColorSetName =
""
91 self.mTexRotateX = 0.0
92 self.mTexRotateY = 0.0
93 self.mTexRotateZ = 0.0
95 self.mDrawBoundingBox =
False
97 self.mAttributesChanged =
False
99 self.mSwatchShader =
None
102 if self.mSwatchShader
is not None:
103 shaderMgr = omr.MRenderer.getShaderManager()
104 if shaderMgr
is not None:
105 shaderMgr.releaseShader(self.mSwatchShader)
106 self.mSwatchShader =
None
114 return hwColorPerVertexShader()
123 nAttr = om.MFnNumericAttribute()
124 tAttr = om.MFnTypedAttribute()
129 hwColorPerVertexShader.aColorGain = nAttr.createColor(
"colorGain",
"cg")
130 nAttr.storable =
True
132 nAttr.default = (1.0, 1.0, 1.0)
134 nAttr.internal =
True
135 nAttr.affectsAppearance =
True
137 hwColorPerVertexShader.aTranspGain = nAttr.create(
"transparencyGain",
"tg", om.MFnNumericData.kFloat, 1.0)
138 nAttr.storable =
True
141 nAttr.setSoftMin(0.0)
142 nAttr.setSoftMax(2.0)
144 nAttr.internal =
True
145 nAttr.affectsAppearance =
True
147 hwColorPerVertexShader.aColorBias = nAttr.createColor(
"colorBias",
"cb")
148 nAttr.storable =
True
150 nAttr.default = (0.0, 0.0, 0.0)
152 nAttr.internal =
True
153 nAttr.affectsAppearance =
True
155 hwColorPerVertexShader.aTranspBias = nAttr.create(
"transparencyBias",
"tb", om.MFnNumericData.kFloat, 0.0)
156 nAttr.storable =
True
159 nAttr.setSoftMin(-1.0)
160 nAttr.setSoftMax(1.0)
162 nAttr.internal =
True
163 nAttr.affectsAppearance =
True
165 hwColorPerVertexShader.aNormalsPerVertex = nAttr.create(
"normalsPerVertex",
"nv", om.MFnNumericData.kInt, 0)
166 nAttr.storable =
True
167 nAttr.keyable =
False
172 nAttr.internal =
True
173 nAttr.affectsAppearance =
True
175 hwColorPerVertexShader.aColorsPerVertex = nAttr.create(
"colorsPerVertex",
"cv", om.MFnNumericData.kInt, 0)
176 nAttr.storable =
True
177 nAttr.keyable =
False
182 nAttr.internal =
True
183 nAttr.affectsAppearance =
True
185 hwColorPerVertexShader.aColorSetName = tAttr.create(
"colorSetName",
"cs", om.MFnData.kString, om.MObject.kNullObj)
186 tAttr.storable =
True
187 tAttr.keyable =
False
189 tAttr.internal =
True
190 tAttr.affectsAppearance =
True
192 hwColorPerVertexShader.aTexRotateX = nAttr.create(
"texRotateX",
"tx", om.MFnNumericData.kFloat, 0.0)
193 nAttr.storable =
True
197 nAttr.internal =
True
198 nAttr.affectsAppearance =
True
200 hwColorPerVertexShader.aTexRotateY = nAttr.create(
"texRotateY",
"ty", om.MFnNumericData.kFloat, 0.0)
201 nAttr.storable =
True
205 nAttr.internal =
True
206 nAttr.affectsAppearance =
True
208 hwColorPerVertexShader.aTexRotateZ = nAttr.create(
"texRotateZ",
"tz", om.MFnNumericData.kFloat, 0.0)
209 nAttr.storable =
True
213 nAttr.internal =
True
214 nAttr.affectsAppearance =
True
216 hwColorPerVertexShader.aDrawBoundingBox = nAttr.create(
"drawBoundingBox",
"dbb", om.MFnNumericData.kBoolean,
False)
217 nAttr.storable =
True
219 nAttr.default =
False
221 nAttr.internal =
True
222 nAttr.affectsAppearance =
True
231 om.MPxNode.addAttribute(hwColorPerVertexShader.aColorGain)
232 om.MPxNode.addAttribute(hwColorPerVertexShader.aTranspGain)
233 om.MPxNode.addAttribute(hwColorPerVertexShader.aColorBias)
234 om.MPxNode.addAttribute(hwColorPerVertexShader.aTranspBias)
235 om.MPxNode.addAttribute(hwColorPerVertexShader.aNormalsPerVertex)
236 om.MPxNode.addAttribute(hwColorPerVertexShader.aColorsPerVertex)
237 om.MPxNode.addAttribute(hwColorPerVertexShader.aColorSetName)
238 om.MPxNode.addAttribute(hwColorPerVertexShader.aTexRotateX)
239 om.MPxNode.addAttribute(hwColorPerVertexShader.aTexRotateY)
240 om.MPxNode.addAttribute(hwColorPerVertexShader.aTexRotateZ)
241 om.MPxNode.addAttribute(hwColorPerVertexShader.aDrawBoundingBox)
243 om.MPxNode.attributeAffects (hwColorPerVertexShader.aColorGain, omui.MPxHwShaderNode.outColor)
244 om.MPxNode.attributeAffects (hwColorPerVertexShader.aTranspGain, omui.MPxHwShaderNode.outColor)
245 om.MPxNode.attributeAffects (hwColorPerVertexShader.aColorBias, omui.MPxHwShaderNode.outColor)
246 om.MPxNode.attributeAffects (hwColorPerVertexShader.aTranspBias, omui.MPxHwShaderNode.outColor)
247 om.MPxNode.attributeAffects (hwColorPerVertexShader.aNormalsPerVertex, omui.MPxHwShaderNode.outColor)
248 om.MPxNode.attributeAffects (hwColorPerVertexShader.aColorsPerVertex, omui.MPxHwShaderNode.outColor)
249 om.MPxNode.attributeAffects (hwColorPerVertexShader.aColorSetName, omui.MPxHwShaderNode.outColor)
250 om.MPxNode.attributeAffects (hwColorPerVertexShader.aTexRotateX, omui.MPxHwShaderNode.outColor)
251 om.MPxNode.attributeAffects (hwColorPerVertexShader.aTexRotateY, omui.MPxHwShaderNode.outColor)
252 om.MPxNode.attributeAffects (hwColorPerVertexShader.aTexRotateZ, omui.MPxHwShaderNode.outColor)
253 om.MPxNode.attributeAffects (hwColorPerVertexShader.aDrawBoundingBox, omui.MPxHwShaderNode.outColor)
260 def compute(self, plug, data):
263 if (plug != omui.MPxHwShaderNode.outColor)
and (plug.parent() != omui.MPxHwShaderNode.outColor):
266 inputData = data.inputValue(hwColorPerVertexShader.aColorGain)
267 color = inputData.asFloatVector()
269 outColorHandle = data.outputValue(hwColorPerVertexShader.outColor)
270 outColorHandle.setMFloatVector(color)
274 def postConstructor(self):
275 self.setMPSafe(
False)
278 def getInternalValue(self, plug, handle):
279 handledAttribute =
False
281 if (plug == hwColorPerVertexShader.aColorGain):
282 handledAttribute =
True
283 handle.set3Float( self.mColorGain[0], self.mColorGain[1], self.mColorGain[2] )
285 elif (plug == hwColorPerVertexShader.aColorBias):
286 handledAttribute =
True
287 handle.set3Float( self.mColorBias[0], self.mColorBias[1], self.mColorBias[2] )
289 elif (plug == hwColorPerVertexShader.aTranspGain):
290 handledAttribute =
True
291 handle.setFloat( self.mTranspGain )
293 elif (plug == hwColorPerVertexShader.aTranspBias):
294 handledAttribute =
True
295 handle.setFloat( self.mTranspBias )
297 elif (plug == hwColorPerVertexShader.aNormalsPerVertex):
298 handledAttribute =
True
299 handle.setInt( self.mNormalsPerVertex )
301 elif (plug == hwColorPerVertexShader.aColorsPerVertex):
302 handledAttribute =
True
303 handle.setInt( self.mColorsPerVertex )
305 elif (plug == hwColorPerVertexShader.aColorSetName):
306 handledAttribute =
True
307 handle.setString( self.mColorSetName )
309 elif (plug == hwColorPerVertexShader.aTexRotateX):
310 handledAttribute =
True
311 handle.setFloat( self.mTexRotateX )
313 elif (plug == hwColorPerVertexShader.aTexRotateY):
314 handledAttribute =
True
315 handle.setFloat( self.mTexRotateY )
317 elif (plug == hwColorPerVertexShader.aTexRotateZ):
318 handledAttribute =
True
319 handle.setFloat( self.mTexRotateZ )
321 elif (plug == hwColorPerVertexShader.aDrawBoundingBox):
322 handledAttribute =
True
323 handle.setBool( self.mDrawBoundingBox )
325 return handledAttribute
327 def setInternalValue(self, plug, handle):
328 handledAttribute =
False
330 if (plug == hwColorPerVertexShader.aNormalsPerVertex):
331 handledAttribute =
True
332 self.mNormalsPerVertex = handle.asInt()
334 elif (plug == hwColorPerVertexShader.aColorsPerVertex):
335 handledAttribute =
True
336 self.mColorsPerVertex = handle.asInt()
338 elif (plug == hwColorPerVertexShader.aColorSetName):
339 handledAttribute =
True
340 self.mColorSetName = handle.asString()
342 elif (plug == hwColorPerVertexShader.aTexRotateX):
343 handledAttribute =
True
344 self.mTexRotateX = handle.asFloat()
346 elif (plug == hwColorPerVertexShader.aTexRotateY):
347 handledAttribute =
True
348 self.mTexRotateY = handle.asFloat()
350 elif (plug == hwColorPerVertexShader.aTexRotateZ):
351 handledAttribute =
True
352 self.mTexRotateZ = handle.asFloat()
354 elif (plug == hwColorPerVertexShader.aColorGain):
355 handledAttribute =
True
356 val = handle.asFloat3()
357 if val != self.mColorGain:
358 self.mColorGain = val
359 self.mAttributesChanged =
True
361 elif (plug == hwColorPerVertexShader.aColorBias):
362 handledAttribute =
True
363 val = handle.asFloat3()
364 if val != self.mColorBias:
365 self.mColorBias = val
366 self.mAttributesChanged =
True
368 elif (plug == hwColorPerVertexShader.aTranspGain):
369 handledAttribute =
True
370 val = handle.asFloat()
371 if val != self.mTranspGain:
372 self.mTranspGain = val
373 self.mAttributesChanged =
True
375 elif (plug == hwColorPerVertexShader.aTranspBias):
376 handledAttribute =
True
377 val = handle.asFloat()
378 if val != self.mTranspBias:
379 self.mTranspBias = val
380 self.mAttributesChanged =
True
382 elif (plug == hwColorPerVertexShader.aDrawBoundingBox):
383 handledAttribute =
True
384 val = handle.asBool()
385 if val != self.mDrawBoundingBox:
386 self.mDrawBoundingBox = val
387 self.mAttributesChanged =
True
389 return handledAttribute
391 def bind(self, request, view):
394 def unbind(self, request, view):
397 def geometry(self, request, view, prim, writable, indexCount, indexArray, vertexCount, vertexIDs, vertexArray, normalCount, normalArrays, colorCount, colorArrays, texCoordCount, texCoordArrays):
399 if self.mDrawBoundingBox:
400 points = ((ctypes.c_float * 3)*vertexCount).from_buffer(vertexArray)
403 box = om.MBoundingBox()
404 for i
in range(vertexCount):
406 box.expand( om.MPoint(point[0], point[1], point[2]) )
409 wireColor = om.MColor( (0.1, 0.15, 0.35) )
410 drawBoundingBox( box, wireColor )
414 if colorCount > 0
and colorArrays[ colorCount - 1]
is not None:
415 indexArray = (ctypes.c_uint).from_buffer(indexArray)
416 vertexArray = (ctypes.c_float * 3).from_buffer(vertexArray)
417 colorArray = (ctypes.c_float * 4).from_buffer(colorArrays[colorCount - 1])
419 glPushAttrib(GL_ALL_ATTRIB_BITS)
420 glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
421 glDisable(GL_LIGHTING)
423 glEnableClientState(GL_COLOR_ARRAY)
424 glColorPointer( 4, GL_FLOAT, 0, colorArray )
426 glEnableClientState(GL_VERTEX_ARRAY)
427 glVertexPointer ( 3, GL_FLOAT, 0, vertexArray )
428 glDrawElements ( prim, indexCount, GL_UNSIGNED_INT, indexArray )
430 glDisableClientState(GL_COLOR_ARRAY)
439 if normalCount > self.mNormalsPerVertex:
440 normalCount = self.mNormalsPerVertex
443 indexArray = (ctypes.c_uint).from_buffer(indexArray)
444 vertexArray = (ctypes.c_float * 3).from_buffer(vertexArray)
445 normalArray = (ctypes.c_float * 3).from_buffer(normalArrays[normalCount - 1])
447 glPushAttrib(GL_ALL_ATTRIB_BITS)
448 glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
449 glDisable(GL_LIGHTING)
451 glEnableClientState(GL_COLOR_ARRAY)
452 glColorPointer(3, GL_FLOAT, 0, normalArray )
454 glEnableClientState(GL_VERTEX_ARRAY)
455 glVertexPointer ( 3, GL_FLOAT, 0, vertexArray )
456 glDrawElements ( prim, indexCount, GL_UNSIGNED_INT, indexArray )
458 glDisableClientState(GL_COLOR_ARRAY)
464 self.draw( prim, writable, indexCount, indexArray, vertexCount, vertexArray, colorCount, colorArrays )
467 def glBind(self, shapePath):
470 def glUnbind(self, shapePath):
473 def glGeometry(self, path, prim, writable, indexCount, indexArray, vertexCount, vertexIDs, vertexArray, normalCount, normalArrays, colorCount, colorArrays, texCoordCount, texCoordArrays):
477 if normalCount > self.mNormalsPerVertex:
478 normalCount = self.mNormalsPerVertex
481 indexArray = (ctypes.c_uint).from_buffer(indexArray)
482 vertexArray = (ctypes.c_float * 3).from_buffer(vertexArray)
483 normalArray = (ctypes.c_float * 3).from_buffer(normalArrays[normalCount - 1])
485 glPushAttrib(GL_ALL_ATTRIB_BITS)
486 glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
487 glDisable(GL_LIGHTING)
490 glEnableClientState(GL_COLOR_ARRAY)
491 glColorPointer(3, GL_FLOAT, 0, normalArray )
493 glEnableClientState(GL_VERTEX_ARRAY)
494 glVertexPointer ( 3, GL_FLOAT, 0, vertexArray )
495 glDrawElements ( prim, indexCount, GL_UNSIGNED_INT, indexArray )
497 glDisableClientState(GL_COLOR_ARRAY)
503 self.draw( prim, writable, indexCount, indexArray, vertexCount, vertexArray, colorCount, colorArrays )
507 def renderSwatchImage(self, outImage):
508 return omr.MRenderUtilities.renderMaterialViewerGeometry(
"meshSphere",
511 lightRig=omr.MRenderUtilities.kSwatchLight )
513 def draw(self, prim, writable, indexCount, indexArray, vertexCount, vertexArray, colorCount, colorArrays):
516 self.mAttributesChanged =
False
520 if (vertexCount == 0)
or not ((prim == GL_TRIANGLES)
or (prim == GL_TRIANGLE_STRIP)):
521 raise ValueError(
"kFailure")
523 indexArray = (ctypes.c_uint).from_buffer(indexArray)
524 vertexArray = (ctypes.c_float * 3).from_buffer(vertexArray)
526 glPushAttrib( GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT )
527 glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
528 glDisable(GL_LIGHTING)
530 glEnableClientState(GL_VERTEX_ARRAY)
531 glVertexPointer ( 3, GL_FLOAT, 0, vertexArray )
537 if colorCount > 0
and colorArrays[0]
is not None:
538 colorArray = (ctypes.c_float * 4).from_buffer(colorArrays[0])
539 glEnableClientState(GL_COLOR_ARRAY)
540 glColorPointer(4, GL_FLOAT, 0, colorArray)
542 glColor4f(1.0, 0.5, 1.0, 1.0)
543 glDrawElements( prim, indexCount, GL_UNSIGNED_INT, indexArray )
548 glBlendFunc(GL_ONE, GL_ONE)
549 glEnableClientState(GL_COLOR_ARRAY)
551 for i
in range(colorCount):
552 if colorArrays[i]
is not None:
555 colorArray = (ctypes.c_float * 4).from_buffer(colorArrays[i])
557 if self.mColorGain != [1.0, 1.0, 1.0]
or self.mColorBias != [0.0, 0.0, 0.0]
or self.mTranspGain != 1.0
or self.mTranspBias != 0.0:
566 biasT = 1 - self.mTranspGain - self.mTranspBias
570 if not (writable & omui.MPxHwShaderNode.kWriteColorArrays):
572 origColors = ((ctypes.c_float * 4)*vertexCount).from_buffer(colorArrays[i])
573 for ii
in range(vertexCount):
574 origColor = origColors[ii]
575 colorArray.append( (origColor[0] * self.mColorGain[0]) + self.mColorBias[0] )
576 colorArray.append( (origColor[1] * self.mColorGain[1]) + self.mColorBias[1] )
577 colorArray.append( (origColor[2] * self.mColorGain[2]) + self.mColorBias[2] )
578 colorArray.append( (origColor[3] * self.mTranspGain) + biasT )
580 origColors = ((ctypes.c_float * 4)*vertexCount).from_buffer(colorArrays[i])
581 for ii
in range(vertexCount):
582 origColor = origColors[ii]
583 origColor[0] = (origColor[0] * self.mColorGain[0]) + self.mColorBias[0]
584 origColor[1] = (origColor[1] * self.mColorGain[1]) + self.mColorBias[1]
585 origColor[2] = (origColor[2] * self.mColorGain[2]) + self.mColorBias[2]
586 origColor[3] = (origColor[3] * self.mTranspGain) + biasT
589 glEnableClientState(GL_COLOR_ARRAY)
590 glColorPointer(4, GL_FLOAT, 0, colorArray)
594 glDisableClientState(GL_COLOR_ARRAY)
595 glColor4f(1.0, 1.0, 1.0, 1.0)
597 glDrawElements ( prim, indexCount, GL_UNSIGNED_INT, indexArray )
605 def colorsPerVertex(self):
608 if self.mNormalsPerVertex:
613 path = self.currentPath()
614 if path.hasFn( om.MFn.kMesh ):
615 fnMesh = om.MFnMesh( path.node() )
616 numColorSets = fnMesh.numColorSets
618 numColors = numColorSets
625 def hasTransparency(self):
629 def normalsPerVertex(self):
630 numNormals = self.mNormalsPerVertex
632 path = self.currentPath()
633 if path.hasFn( om.MFn.kMesh ):
634 fnMesh = om.MFnMesh( path.node() )
638 if fnMesh.numUVSets == 0:
640 dispWarn =
"Asking for more uvsets then available for shape: "
641 dispWarn += path.fullPathName()
642 om.MGlobal.displayWarning( dispWarn )
644 if self.mNormalsPerVertex:
652 def texCoordsPerVertex(self):
655 def colorSetName(self):
656 return self.mColorSetName
658 def wantDrawBoundingBox(self):
659 return self.mDrawBoundingBox
662 class hwCPVShaderOverride(omr.MPxShaderOverride):
670 def __init__(self, obj):
671 omr.MPxShaderOverride.__init__(self, obj)
674 self.fShaderNode =
None
675 self.fBlendState =
None
679 return hwCPVShaderOverride(obj)
682 def initialize(self, shader):
684 if not shader.isNull():
685 self.fShaderNode = omui.MPxHwShaderNode.getHwShaderNode(shader)
689 self.addGeometryRequirement( omr.MVertexBufferDescriptor(reqName, omr.MGeometry.kPosition, omr.MGeometry.kFloat, 3) )
692 if self.fShaderNode
is not None:
693 reqName = self.fShaderNode.colorSetName()
694 self.addGeometryRequirement( omr.MVertexBufferDescriptor(reqName, omr.MGeometry.kColor, omr.MGeometry.kFloat, 4) )
696 return "Autodesk Maya hwColorPerVertexShader"
703 def draw(self, context, renderItemList):
708 glMatrixMode(GL_MODELVIEW)
710 transform = context.getMatrix(omr.MFrameContext.kWorldViewMtx)
711 glLoadMatrixd( matrixAsArray(transform) )
714 glMatrixMode(GL_PROJECTION)
716 projection = context.getMatrix(omr.MFrameContext.kProjectionMtx)
717 glLoadMatrixd( matrixAsArray(projection) )
722 stateMgr = context.getStateManager()
726 curBlendState = stateMgr.getBlendState()
727 if curBlendState
is None:
730 if self.fBlendState
is None:
735 blendDesc = omr.MBlendStateDesc()
736 for i
in range(omr.MBlendState.kMaxTargets):
737 blendDesc.targetBlends[i].blendEnable =
True
738 blendDesc.targetBlends[i].sourceBlend = omr.MBlendState.kSourceAlpha
739 blendDesc.targetBlends[i].destinationBlend = omr.MBlendState.kInvSourceAlpha
740 blendDesc.targetBlends[i].blendOperation = omr.MBlendState.kAdd
741 blendDesc.targetBlends[i].alphaSourceBlend = omr.MBlendState.kOne
742 blendDesc.targetBlends[i].alphaDestinationBlend = omr.MBlendState.kInvSourceAlpha
743 blendDesc.targetBlends[i].alphaBlendOperation = omr.MBlendState.kAdd
745 blendDesc.blendFactor = (1.0, 1.0, 1.0, 1.0)
747 self.fBlendState = stateMgr.acquireBlendState(blendDesc)
748 if self.fBlendState
is None:
752 stateMgr.setBlendState(self.fBlendState)
755 if self.fShaderNode.wantDrawBoundingBox():
756 for renderItem
in renderItemList:
757 if renderItem
is None:
762 box = renderItem.boundingBox(om.MSpace.kObject)
765 wireColor = om.MColor( (0.1, 0.15, 0.35) )
766 drawBoundingBox( box, wireColor )
771 useCustomDraw =
False
775 self.customDraw(context, renderItemList)
780 self.drawGeometry(context)
787 glMatrixMode(GL_MODELVIEW)
795 stateMgr.setBlendState(curBlendState)
800 def customDraw(self, context, renderItemList):
801 glPushClientAttrib ( GL_CLIENT_ALL_ATTRIB_BITS )
803 for renderItem
in renderItemList:
804 if renderItem
is None:
807 geometry = renderItem.geometry()
808 if geometry
is not None:
809 hwCPVShaderOverride.customDrawGeometry(context, geometry, renderItem.primitive())
818 def customDrawGeometry(context, geometry, indexPrimType):
821 bufferCount = geometry.vertexBufferCount()
824 for i
in range(bufferCount):
825 buffer = geometry.vertexBuffer(i)
830 desc = buffer.descriptor()
831 dataHandle = buffer.resourceHandle()
836 dataBufferId = (ctypes.c_uint).from_address(dataHandle)
837 fieldOffset = ctypes.c_void_p(desc.offset)
838 fieldStride = desc.stride
841 glBindBuffer(GL_ARRAY_BUFFER, dataBufferId)
842 currentError = glGetError()
843 if currentError != GL_NO_ERROR:
848 if desc.semantic == omr.MGeometry.kPosition:
849 glEnableClientState(GL_VERTEX_ARRAY)
850 glVertexPointer(3, GL_FLOAT, fieldStride*4, fieldOffset)
851 currentError = glGetError()
852 if currentError != GL_NO_ERROR:
854 elif desc.semantic == omr.MGeometry.kColor:
855 glEnableClientState(GL_COLOR_ARRAY)
856 glColorPointer(4, GL_FLOAT, fieldStride*4, fieldOffset)
857 currentError = glGetError()
858 if currentError != GL_NO_ERROR:
864 if boundData
and geometry.indexBufferCount() > 0:
867 buffer = geometry.indexBuffer(0)
868 indexHandle = buffer.resourceHandle()
872 indexBufferId = (ctypes.c_uint).from_address(indexHandle)
873 indexBufferCount = ctypes.c_int(buffer.size())
876 if indexBufferId
and indexBufferId > 0:
877 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferId)
878 currentError = glGetError()
879 if currentError == GL_NO_ERROR:
880 indexPrimTypeGL = GL_TRIANGLES
881 if indexPrimType == omr.MGeometry.kPoints:
882 indexPrimTypeGL = GL_POINTS
883 elif indexPrimType == omr.MGeometry.kLines:
884 indexPrimTypeGL = GL_LINES
885 elif indexPrimType == omr.MGeometry.kLineStrip:
886 indexPrimTypeGL = GL_LINE_STRIP
887 elif indexPrimType == omr.MGeometry.kTriangles:
888 indexPrimTypeGL = GL_TRIANGLES
889 elif indexPrimType == omr.MGeometry.kTriangleStrip:
890 indexPrimTypeGL = GL_TRIANGLE_STRIP
896 indexType = GL_UNSIGNED_SHORT
897 if buffer.dataType() == omr.MGeometry.kUnsignedInt32:
898 indexType = GL_UNSIGNED_INT
899 glDrawElements(indexPrimTypeGL, indexBufferCount, indexType, ctypes.c_void_p(0))
902 def rebuildAlways(self):
908 def supportedDrawAPIs(self):
909 return omr.MRenderer.kOpenGL
911 def isTransparent(self):
920 sHWCPVShaderRegistrantId =
"HWCPVShaderRegistrantId"
922 def initializePlugin(obj):
923 plugin = om.MFnPlugin(obj,
"Autodesk",
"4.5",
"Any")
925 swatchName = omui.MHWShaderSwatchGenerator.initialize()
926 userClassify =
"shader/surface/utility/:drawdb/shader/surface/hwColorPerVertexShader:swatch/" + swatchName
927 plugin.registerNode(
"hwColorPerVertexShader", hwColorPerVertexShader.id, hwColorPerVertexShader.creator, hwColorPerVertexShader.initialize, om.MPxNode.kHwShaderNode, userClassify)
929 sys.stderr.write(
"Failed to register node\n")
933 global sHWCPVShaderRegistrantId
934 omr.MDrawRegistry.registerShaderOverrideCreator(
"drawdb/shader/surface/hwColorPerVertexShader", sHWCPVShaderRegistrantId, hwCPVShaderOverride.creator)
936 sys.stderr.write(
"Failed to register override\n")
944 def uninitializePlugin(obj):
945 plugin = om.MFnPlugin(obj)
947 plugin.deregisterNode(hwColorPerVertexShader.id)
949 sys.stderr.write(
"Failed to deregister node\n")
953 global sHWCPVShaderRegistrantId
954 omr.MDrawRegistry.deregisterShaderOverrideCreator(
"drawdb/shader/surface/hwColorPerVertexShader", sHWCPVShaderRegistrantId)
956 sys.stderr.write(
"Failed to deregister override\n")