9 import maya.api.OpenMayaRender 
as omr
 
   10 import maya.api.OpenMaya 
as om
 
   36     The presence of this function tells Maya that the plugin produces, and 
   37     expects to be passed, objects created using the Maya Python API 2.0. 
   42 class MyCustomBufferGenerator(omr.MPxVertexBufferGenerator):
 
   44         omr.MPxVertexBufferGenerator.__init__(self)
 
   46     def getSourceIndexing(self, object, sourceIndexing):
 
   48         mesh = om.MFnMesh(object)
 
   51         numPolys = mesh.numPolygons
 
   55         vertToFaceVertIDs = sourceIndexing.indices()
 
   59         for i 
in range(0, numPolys):
 
   62             faceColorID = faceNum % 3
 
   64             vertexCount = mesh.polygonVertexCount(i)
 
   65             for x 
in range(0, vertexCount):
 
   67                 vertToFaceVertIDs.append(faceColorID)
 
   72         sourceIndexing.setComponentType(omr.MComponentDataIndexing.kFaceVertex)
 
   76     def getSourceStreams(self, object, sourceStreams):
 
   80     def createVertexStream(self, object, vertexBuffer, targetIndexing, sharedIndexing, sourceStreams):
 
   83         descriptor = vertexBuffer.descriptor()
 
   86         if descriptor.dataType != omr.MGeometry.kFloat:
 
   90         if descriptor.dimension != 2:
 
   94         if descriptor.semantic != omr.MGeometry.kTexture:
 
   99         mesh = om.MFnMesh(object)
 
  101         indices = targetIndexing.indices()
 
  103         vertexCount = len(indices)
 
  108         buffer = vertexBuffer.acquire(vertexCount, 
True)    
 
  110         inc = sizeof(c_float)
 
  113         for i 
in range(0, vertexCount):
 
  118             c_float.from_address(address).value = 1.0
 
  121             c_float.from_address(address).value = indices[i] 
 
  125         vertexBuffer.commit(buffer)
 
  128 class MyCustomBufferGenerator2(omr.MPxVertexBufferGenerator):
 
  130         omr.MPxVertexBufferGenerator.__init__(self)
 
  132     def getSourceIndexing(self, object, sourceIndexing):
 
  134         mesh = om.MFnMesh(object)
 
  136         (vertexCount, vertexList) = mesh.getVertices()
 
  137         vertCount = len(vertexList)
 
  139         vertices = sourceIndexing.indices()
 
  140         for i 
in range(0, vertCount):
 
  141             vertices.append( vertexList[i] )
 
  145     def getSourceStreams(self, object, sourceStreams):
 
  146         sourceStreams.append( 
"Position" )
 
  147         sourceStreams.append( 
"Normal" )
 
  150     def createVertexStream(self, object, vertexBuffer, targetIndexing, sharedIndexing, sourceStreams):
 
  153         descriptor = vertexBuffer.descriptor()
 
  156         dataType = descriptor.dataType
 
  157         if dataType != omr.MGeometry.kFloat 
and dataType != omr.MGeometry.kInt32:
 
  161         dimension = descriptor.dimension
 
  162         if dimension != 4 
and dimension != 3:
 
  166         if descriptor.semantic != omr.MGeometry.kTexture:
 
  171         mesh = om.MFnMesh(object)
 
  173         indices = targetIndexing.indices()
 
  175         vertexCount = len(indices)
 
  179         positionStream = sourceStreams.getBuffer( 
"Position" )
 
  180         if positionStream == 
None or positionStream.descriptor().dataType != omr.MGeometry.kFloat:
 
  182         positionDimension = positionStream.descriptor().dimension
 
  183         if positionDimension != 3 
and positionDimension != 4:
 
  186         normalStream = sourceStreams.getBuffer( 
"Normal" )
 
  187         if normalStream == 
None or normalStream.descriptor().dataType != omr.MGeometry.kFloat:
 
  189         normalDimension = normalStream.descriptor().dimension
 
  190         if normalDimension != 3 
and normalDimension != 4:
 
  193         positionBuffer = positionStream.map()
 
  194         if positionBuffer != 0:
 
  195             normalBuffer = normalStream.map()
 
  196             if normalBuffer != 0:
 
  197                 compositeBuffer = vertexBuffer.acquire(vertexCount, 
True) 
 
  198                 if compositeBuffer != 0:
 
  200                     compaddress = compositeBuffer
 
  201                     posaddress = positionBuffer
 
  202                     normaddress = normalBuffer
 
  204                     floatinc = sizeof(c_float)
 
  205                     intinc = sizeof(c_int)
 
  207                     if dataType == omr.MGeometry.kFloat:
 
  209                         for i 
in range(0, vertexCount):
 
  210                             xcompaddr = compaddress
 
  211                             ycompaddr = compaddress+floatinc
 
  212                             zcompaddr = compaddress+2*floatinc
 
  213                             wcompaddr = compaddress+3*floatinc
 
  216                             yposaddr = posaddress+floatinc
 
  217                             zposaddr = posaddress+2*floatinc
 
  219                             xnormaddr = normaddress
 
  221                             znormaddr = normaddress+2*floatinc
 
  223                             c_float.from_address(xcompaddr).value = c_float.from_address(yposaddr).value  
 
  224                             c_float.from_address(ycompaddr).value = c_float.from_address(zposaddr).value  
 
  225                             c_float.from_address(zcompaddr).value = c_float.from_address(xnormaddr).value 
 
  227                                 c_float.from_address(wcompaddr).value = c_float.from_address(znormaddr).value 
 
  229                             compaddress += dimension*floatinc
 
  230                             posaddress += positionDimension*floatinc
 
  231                             normaddress += normalDimension*floatinc
 
  233                     elif dataType == omr.MGeometry.kInt32:
 
  235                         for i 
in range(0, vertexCount):
 
  236                             xcompaddr = compaddress
 
  237                             ycompaddr = compaddress+intinc
 
  238                             zcompaddr = compaddress+2*intinc
 
  239                             wcompaddr = compaddress+3*intinc
 
  242                             yposaddr = posaddress+floatinc
 
  243                             zposaddr = posaddress+2*floatinc
 
  245                             xnormaddr = normaddress
 
  247                             znormaddr = normaddress+2*floatinc
 
  249                             c_int.from_address(xcompaddr).value = c_float.from_address(yposaddr).value * 255  
 
  250                             c_int.from_address(ycompaddr).value = c_float.from_address(zposaddr).value * 255  
 
  251                             c_int.from_address(zcompaddr).value = c_float.from_address(xnormaddr).value * 255 
 
  253                                 c_int.from_address(wcompaddr).value = c_float.from_address(znormaddr).value * 255 
 
  255                             compaddress += dimension*intinc
 
  256                             posaddress += positionDimension*floatinc
 
  257                             normaddress += normalDimension*floatinc
 
  259                     vertexBuffer.commit(compositeBuffer)
 
  263             positionStream.unmap()
 
  267 def createMyCustomBufferGenerator():
 
  268     return MyCustomBufferGenerator()
 
  270 def createMyCustomBufferGenerator2():
 
  271     return MyCustomBufferGenerator2()
 
  274 def getCustomSemantics():
 
  275     if omr.MRenderer.drawAPI() == omr.MRenderer.kDirectX11:
 
  278         return (
"myCustomStream", 
"myCustomStreamB")
 
  279     if omr.MRenderer.drawAPI() == omr.MRenderer.kOpenGLCoreProfile:
 
  283         return (
"myCustomStream", 
"myCustomStreamB")
 
  284     if omr.MRenderer.drawAPI() == omr.MRenderer.kOpenGL:
 
  287         return (
"ATTR8", 
"ATTR7")
 
  294 def initializePlugin(obj):
 
  295     (customSemantic, customSemantic2) = getCustomSemantics()
 
  297     omr.MDrawRegistry.registerVertexBufferGenerator(customSemantic, createMyCustomBufferGenerator)
 
  298     omr.MDrawRegistry.registerVertexBufferGenerator(customSemantic2, createMyCustomBufferGenerator2)
 
  300 def uninitializePlugin(obj):
 
  301     (customSemantic, customSemantic2) = getCustomSemantics()
 
  303     omr.MDrawRegistry.deregisterVertexBufferGenerator(customSemantic)
 
  304     omr.MDrawRegistry.deregisterVertexBufferGenerator(customSemantic2)