12 import maya.api.OpenMaya
as om
13 import maya.api.OpenMayaUI
as omui
14 import maya.api.OpenMayaRender
as omr
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.
24 class geometryReplicator(om.MPxSurfaceShape):
25 id = om.MTypeId(0x00080029)
26 drawDbClassification =
"drawdb/geometry/geometryReplicator"
27 drawRegistrantId =
"geometryReplicatorPlugin"
34 om.MPxSurfaceShape.__init__(self)
38 return geometryReplicator()
42 nAttr = om.MFnNumericAttribute()
44 geometryReplicator.aShowCPV = nAttr.create(
"showCPV",
"sc", om.MFnNumericData.kBoolean, 0)
45 om.MPxNode.addAttribute(geometryReplicator.aShowCPV)
47 geometryReplicator.aBaseMesh = nAttr.create(
"isBaseMesh",
"bm", om.MFnNumericData.kBoolean, 0)
48 om.MPxNode.addAttribute(geometryReplicator.aBaseMesh)
50 def postConstructor(self):
51 self.isRenderable =
True
56 def boundingBox(self):
57 corner1 = om.MPoint( -0.5, 0.0, -0.5 )
58 corner2 = om.MPoint( 0.5, 0.0, 0.5 )
60 return om.MBoundingBox( corner1, corner2 )
72 class geometryReplicatorShapeUI(omui.MPxSurfaceShapeUI):
74 omui.MPxSurfaceShapeUI.__init__(self)
78 return geometryReplicatorShapeUI()
89 class geometryReplicatorGeometryOverride(omr.MPxGeometryOverride):
90 def __init__(self, obj):
91 omr.MPxGeometryOverride.__init__(self, obj)
92 self.fThisNode = om.MObject(obj)
93 self.fType = om.MFn.kInvalid
94 self.fPath = om.MDagPath()
98 return geometryReplicatorGeometryOverride(obj)
100 def supportedDrawAPIs(self):
102 return (omr.MRenderer.kOpenGL | omr.MRenderer.kDirectX11)
104 def isCPVShown(self):
106 plug = om.MPlug(self.fThisNode, geometryReplicator.aShowCPV)
111 def isBaseMesh(self):
113 plug = om.MPlug(self.fThisNode, geometryReplicator.aBaseMesh)
119 if not self.fPath.isValid():
120 fnThisNode = om.MFnDependencyNode(self.fThisNode)
122 messageAttr = fnThisNode.attribute(
"message")
123 messagePlug = om.MPlug(self.fThisNode, messageAttr)
125 connections = messagePlug.connectedTo(
False,
True)
126 for plug
in connections:
128 if node.hasFn(om.MFn.kMesh)
or node.hasFn(om.MFn.kNurbsSurface)
or node.hasFn(om.MFn.kNurbsCurve)
or node.hasFn(om.MFn.kBezierCurve):
129 path = om.MDagPath.getAPathTo(node)
131 self.fType = path.apiType()
134 def updateRenderItems(self, path, list):
135 if not self.fPath.isValid():
137 shaderManager = omr.MRenderer.getShaderManager()
138 if shaderManager
is None:
141 if self.fType == om.MFn.kNurbsCurve
or self.fType == om.MFn.kBezierCurve:
144 index = list.indexOf(
"geometryReplicatorCurve")
146 curveItem = omr.MRenderItem.create(
"geometryReplicatorCurve", omr.MRenderItem.NonMaterialSceneItem, omr.MGeometry.kLines)
147 curveItem.setDrawMode(omr.MGeometry.kAll)
148 list.append(curveItem)
150 shader = shaderManager.getStockShader(omr.MShaderManager.k3dSolidShader)
151 if shader
is not None:
152 theColor = [1.0, 0.0, 0.0, 1.0]
153 shader.setParameter(
"solidColor", theColor)
155 curveItem.setShader(shader)
156 shaderManager.releaseShader(shader)
159 curveItem = list[index]
161 if curveItem
is not None:
162 curveItem.enable(
True)
164 elif self.fType == om.MFn.kMesh:
167 index = list.indexOf(
"geometryReplicatorWireframe")
169 wireframeItem = omr.MRenderItem.create(
"geometryReplicatorWireframe", omr.MRenderItem.DecorationItem, omr.MGeometry.kLines)
170 wireframeItem.setDrawMode(omr.MGeometry.kWireframe)
171 wireframeItem.setDepthPriority(omr.MRenderItem.sActiveWireDepthPriority)
172 list.append(wireframeItem)
174 shader = shaderManager.getStockShader(omr.MShaderManager.k3dSolidShader)
175 if shader
is not None:
176 theColor = [1.0, 0.0, 0.0, 1.0]
177 shader.setParameter(
"solidColor", theColor)
179 wireframeItem.setShader(shader)
180 shaderManager.releaseShader(shader)
183 wireframeItem = list[index]
185 if wireframeItem
is not None:
186 wireframeItem.enable(
True)
189 showCPV = self.isCPVShown()
190 index = list.indexOf(
"StandardShadedItem", omr.MGeometry.kTriangles, omr.MGeometry.kShaded)
192 shadedItem = list[index]
193 if shadedItem
is not None:
194 shadedItem.enable(
not showCPV)
196 index = list.indexOf(
"StandardShadedItem", omr.MGeometry.kTriangles, omr.MGeometry.kTextured)
198 shadedItem = list[index]
199 if shadedItem
is not None:
200 shadedItem.enable(
not showCPV)
203 index = list.indexOf(
"geometryReplicatorCPV")
205 cpvItem = list[index]
206 if cpvItem
is not None:
207 cpvItem.enable(showCPV)
212 cpvItem = omr.MRenderItem.create(
"geometryReplicatorCPV", omr.MRenderItem.MaterialSceneItem, omr.MGeometry.kTriangles)
213 cpvItem.setDrawMode(omr.MGeometry.kShaded | omr.MGeometry.kTextured)
216 shader = shaderManager.getStockShader(omr.MShaderManager.k3dCPVSolidShader)
217 if shader
is not None:
218 cpvItem.setShader(shader)
220 shaderManager.releaseShader(shader)
222 def populateGeometry(self, requirements, renderItems, data):
223 if not self.fPath.isValid():
230 options = omr.MGeometryExtractor.kPolyGeom_Normal
231 if self.isBaseMesh():
232 options = options | omr.MGeometryExtractor.kPolyGeom_BaseMesh
234 extractor = omr.MGeometryExtractor(requirements, self.fPath, options)
235 if extractor
is None:
239 descList = requirements.vertexRequirements()
240 for desc
in descList:
242 if desc.semantic == omr.MGeometry.kPosition
or desc.semantic == omr.MGeometry.kPosition
or desc.semantic == omr.MGeometry.kNormal
or desc.semantic == omr.MGeometry.kTexture
or desc.semantic == omr.MGeometry.kTangent
or desc.semantic == omr.MGeometry.kBitangent
or desc.semantic == omr.MGeometry.kColor:
243 vertexBuffer = data.createVertexBuffer(desc)
244 if vertexBuffer
is not None:
249 vertexCount = extractor.vertexCount()
250 vertices = vertexBuffer.acquire(vertexCount,
True)
251 if vertices
is not None:
252 extractor.populateVertexBuffer(vertices, vertexCount, desc)
253 vertexBuffer.commit(vertices)
256 for item
in renderItems:
257 indexBuffer = data.createIndexBuffer(omr.MGeometry.kUnsignedInt32)
258 if indexBuffer
is None:
265 if item.primitive() == omr.MGeometry.kTriangles:
266 triangleDesc = omr.MIndexBufferDescriptor(omr.MIndexBufferDescriptor.kTriangle,
"", omr.MGeometry.kTriangles, 3)
267 numTriangles = extractor.primitiveCount(triangleDesc)
269 indices = indexBuffer.acquire(3 * numTriangles,
True)
270 extractor.populateIndexBuffer(indices, numTriangles, triangleDesc)
271 indexBuffer.commit(indices)
273 elif item.primitive() == omr.MGeometry.kLines:
274 edgeDesc = omr.MIndexBufferDescriptor(omr.MIndexBufferDescriptor.kEdgeLine,
"", omr.MGeometry.kLines, 2)
275 numEdges = extractor.primitiveCount(edgeDesc)
277 indices = indexBuffer.acquire(2 * numEdges,
True)
278 extractor.populateIndexBuffer(indices, numEdges, edgeDesc)
279 indexBuffer.commit(indices)
281 item.associateWithIndexBuffer(indexBuffer)
294 def initializePlugin(obj):
295 plugin = om.MFnPlugin(obj,
"Autodesk",
"3.0",
"Any")
297 plugin.registerShape(
"geometryReplicator", geometryReplicator.id, geometryReplicator.creator, geometryReplicator.initialize, geometryReplicatorShapeUI.creator, geometryReplicator.drawDbClassification)
299 sys.stderr.write(
"Failed to register shape\n")
303 omr.MDrawRegistry.registerGeometryOverrideCreator(geometryReplicator.drawDbClassification, geometryReplicator.drawRegistrantId, geometryReplicatorGeometryOverride.creator)
305 sys.stderr.write(
"Failed to register override\n")
308 def uninitializePlugin(obj):
309 plugin = om.MFnPlugin(obj)
311 omr.MDrawRegistry.deregisterGeometryOverrideCreator(geometryReplicator.drawDbClassification, geometryReplicator.drawRegistrantId)
313 sys.stderr.write(
"Failed to deregister override\n")
317 plugin.deregisterNode(geometryReplicator.id)
319 sys.stderr.write(
"Failed to deregister shape\n")