cwisbg

convert vertex color to vertex alpha_v2

Apr 30th, 2012
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. import maya.cmds as mc
  2. import maya.OpenMaya as OpenMaya
  3. import time
  4. start = time.clock()
  5. def get_mobject( node):
  6.     selectionList = OpenMaya.MSelectionList()
  7.     selectionList.add(node)
  8.     oNode = OpenMaya.MObject()
  9.     selectionList.getDependNode(0, oNode)
  10.     return oNode
  11. sl= mc.ls(sl=1)#selected()
  12. slShape = mc.listRelatives(sl,s=True,ni=True)[0]
  13. # <-- I grab the MObject of this shape in order to play with the API
  14. cubeShapeApi_Obj = get_mobject(slShape)
  15. convertedAlphaColor = OpenMaya.MColor()
  16. vertexColorList = OpenMaya.MColorArray()
  17. # <-- access and play with mesh data with MFnMesh here I choose to initialize it with the previous MObject, you can also do it with MDagPath...
  18. meshFn = OpenMaya.MFnMesh(cubeShapeApi_Obj)
  19. # <-- grab vertex color in one pass  
  20. meshFn.getVertexColors(vertexColorList)
  21. lenVertexList = vertexColorList.length()
  22. sumAlpha = 0.0
  23. # -- Dirty way to do  a vertexID list
  24. # -- in python you can do it with loop but hey why not use the API?  
  25. fnComponent = OpenMaya.MFnSingleIndexedComponent()
  26. fullComponent = fnComponent.create( OpenMaya.MFn.kMeshVertComponent )
  27. # <--  fnComponent.setCompleteData create a full list of vertex ID like [0,1,2,N]
  28. fnComponent.setCompleteData( lenVertexList );
  29. vertexIndexList = OpenMaya.MIntArray()
  30. # <-- store these ID into vertexIndexList  
  31. fnComponent.getElements(vertexIndexList)
  32. for k in range(lenVertexList):
  33.     sumAlpha = (vertexColorList[k].r+vertexColorList[k].g+vertexColorList[k].b)/3.0
  34.     if sumAlpha < 0.0:
  35.         sumAlpha = 0.0
  36.     convertedAlphaColor.a =  sumAlpha
  37.     vertexColorList.set(convertedAlphaColor,k)
  38. meshFn.setVertexColors(vertexColorList,vertexIndexList,None)
  39. elapsed = (time.clock() - start)
  40. print "done, took:", elapsed
Advertisement
Add Comment
Please, Sign In to add comment