8 from builtins
import range
10 import maya.api.OpenMayaRender
as omr
11 import maya.api.OpenMaya
as om
37 The presence of this function tells Maya that the plugin produces, and
38 expects to be passed, objects created using the Maya Python API 2.0.
43 class MyCustomBufferGenerator(omr.MPxVertexBufferGenerator):
45 omr.MPxVertexBufferGenerator.__init__(self)
47 def getSourceIndexing(self, object, sourceIndexing):
49 mesh = om.MFnMesh(object)
52 numPolys = mesh.numPolygons
56 vertToFaceVertIDs = sourceIndexing.indices()
60 for i
in range(0, numPolys):
63 faceColorID = faceNum % 3
65 vertexCount = mesh.polygonVertexCount(i)
66 for x
in range(0, vertexCount):
68 vertToFaceVertIDs.append(faceColorID)
73 sourceIndexing.setComponentType(omr.MComponentDataIndexing.kFaceVertex)
77 def getSourceStreams(self, object, sourceStreams):
81 def createVertexStream(self, object, vertexBuffer, targetIndexing, sharedIndexing, sourceStreams):
84 descriptor = vertexBuffer.descriptor()
87 if descriptor.dataType != omr.MGeometry.kFloat:
91 if descriptor.dimension != 2:
95 if descriptor.semantic != omr.MGeometry.kTexture:
100 mesh = om.MFnMesh(object)
102 indices = targetIndexing.indices()
104 vertexCount = len(indices)
109 buffer = vertexBuffer.acquire(vertexCount,
True)
111 inc = sizeof(c_float)
114 for i
in range(0, vertexCount):
119 c_float.from_address(address).value = 1.0
122 c_float.from_address(address).value = indices[i]
126 vertexBuffer.commit(buffer)
129 class MyCustomBufferGenerator2(omr.MPxVertexBufferGenerator):
131 omr.MPxVertexBufferGenerator.__init__(self)
133 def getSourceIndexing(self, object, sourceIndexing):
135 mesh = om.MFnMesh(object)
137 (vertexCount, vertexList) = mesh.getVertices()
138 vertCount = len(vertexList)
140 vertices = sourceIndexing.indices()
141 for i
in range(0, vertCount):
142 vertices.append( vertexList[i] )
146 def getSourceStreams(self, object, sourceStreams):
147 sourceStreams.append(
"Position" )
148 sourceStreams.append(
"Normal" )
151 def createVertexStream(self, object, vertexBuffer, targetIndexing, sharedIndexing, sourceStreams):
154 descriptor = vertexBuffer.descriptor()
157 dataType = descriptor.dataType
158 if dataType != omr.MGeometry.kFloat
and dataType != omr.MGeometry.kInt32:
162 dimension = descriptor.dimension
163 if dimension != 4
and dimension != 3:
167 if descriptor.semantic != omr.MGeometry.kTexture:
172 mesh = om.MFnMesh(object)
174 indices = targetIndexing.indices()
176 vertexCount = len(indices)
180 positionStream = sourceStreams.getBuffer(
"Position" )
181 if positionStream ==
None or positionStream.descriptor().dataType != omr.MGeometry.kFloat:
183 positionDimension = positionStream.descriptor().dimension
184 if positionDimension != 3
and positionDimension != 4:
187 normalStream = sourceStreams.getBuffer(
"Normal" )
188 if normalStream ==
None or normalStream.descriptor().dataType != omr.MGeometry.kFloat:
190 normalDimension = normalStream.descriptor().dimension
191 if normalDimension != 3
and normalDimension != 4:
194 positionBuffer = positionStream.map()
195 if positionBuffer != 0:
196 normalBuffer = normalStream.map()
197 if normalBuffer != 0:
198 compositeBuffer = vertexBuffer.acquire(vertexCount,
True)
199 if compositeBuffer != 0:
201 compaddress = compositeBuffer
202 posaddress = positionBuffer
203 normaddress = normalBuffer
205 floatinc = sizeof(c_float)
206 intinc = sizeof(c_int)
208 if dataType == omr.MGeometry.kFloat:
210 for i
in range(0, vertexCount):
211 xcompaddr = compaddress
212 ycompaddr = compaddress+floatinc
213 zcompaddr = compaddress+2*floatinc
214 wcompaddr = compaddress+3*floatinc
217 yposaddr = posaddress+floatinc
218 zposaddr = posaddress+2*floatinc
220 xnormaddr = normaddress
222 znormaddr = normaddress+2*floatinc
224 c_float.from_address(xcompaddr).value = c_float.from_address(yposaddr).value
225 c_float.from_address(ycompaddr).value = c_float.from_address(zposaddr).value
226 c_float.from_address(zcompaddr).value = c_float.from_address(xnormaddr).value
228 c_float.from_address(wcompaddr).value = c_float.from_address(znormaddr).value
230 compaddress += dimension*floatinc
231 posaddress += positionDimension*floatinc
232 normaddress += normalDimension*floatinc
234 elif dataType == omr.MGeometry.kInt32:
236 for i
in range(0, vertexCount):
237 xcompaddr = compaddress
238 ycompaddr = compaddress+intinc
239 zcompaddr = compaddress+2*intinc
240 wcompaddr = compaddress+3*intinc
243 yposaddr = posaddress+floatinc
244 zposaddr = posaddress+2*floatinc
246 xnormaddr = normaddress
248 znormaddr = normaddress+2*floatinc
250 c_int.from_address(xcompaddr).value = c_float.from_address(yposaddr).value * 255
251 c_int.from_address(ycompaddr).value = c_float.from_address(zposaddr).value * 255
252 c_int.from_address(zcompaddr).value = c_float.from_address(xnormaddr).value * 255
254 c_int.from_address(wcompaddr).value = c_float.from_address(znormaddr).value * 255
256 compaddress += dimension*intinc
257 posaddress += positionDimension*floatinc
258 normaddress += normalDimension*floatinc
260 vertexBuffer.commit(compositeBuffer)
264 positionStream.unmap()
268 def createMyCustomBufferGenerator():
269 return MyCustomBufferGenerator()
271 def createMyCustomBufferGenerator2():
272 return MyCustomBufferGenerator2()
275 def getCustomSemantics():
276 if omr.MRenderer.drawAPI() == omr.MRenderer.kDirectX11:
279 return (
"myCustomStream",
"myCustomStreamB")
280 if omr.MRenderer.drawAPI() == omr.MRenderer.kOpenGLCoreProfile:
284 return (
"myCustomStream",
"myCustomStreamB")
285 if omr.MRenderer.drawAPI() == omr.MRenderer.kOpenGL:
288 return (
"ATTR8",
"ATTR7")
295 def initializePlugin(obj):
296 (customSemantic, customSemantic2) = getCustomSemantics()
298 omr.MDrawRegistry.registerVertexBufferGenerator(customSemantic, createMyCustomBufferGenerator)
299 omr.MDrawRegistry.registerVertexBufferGenerator(customSemantic2, createMyCustomBufferGenerator2)
301 def uninitializePlugin(obj):
302 (customSemantic, customSemantic2) = getCustomSemantics()
304 omr.MDrawRegistry.deregisterVertexBufferGenerator(customSemantic)
305 omr.MDrawRegistry.deregisterVertexBufferGenerator(customSemantic2)