Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import maya.cmds as mc
- import maya.OpenMaya as OpenMaya
- import time
- start = time.clock()
- def get_mobject( node):
- selectionList = OpenMaya.MSelectionList()
- selectionList.add(node)
- oNode = OpenMaya.MObject()
- selectionList.getDependNode(0, oNode)
- return oNode
- sl= mc.ls(sl=1)#selected()
- slShape = mc.listRelatives(sl,s=True,ni=True)[0]
- # <-- I grab the MObject of this shape in order to play with the API
- cubeShapeApi_Obj = get_mobject(slShape)
- convertedAlphaColor = OpenMaya.MColor()
- vertexColorList = OpenMaya.MColorArray()
- # <-- 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...
- meshFn = OpenMaya.MFnMesh(cubeShapeApi_Obj)
- # <-- grab vertex color in one pass
- meshFn.getVertexColors(vertexColorList)
- lenVertexList = vertexColorList.length()
- sumAlpha = 0.0
- # -- Dirty way to do a vertexID list
- # -- in python you can do it with loop but hey why not use the API?
- fnComponent = OpenMaya.MFnSingleIndexedComponent()
- fullComponent = fnComponent.create( OpenMaya.MFn.kMeshVertComponent )
- # <-- fnComponent.setCompleteData create a full list of vertex ID like [0,1,2,N]
- fnComponent.setCompleteData( lenVertexList );
- vertexIndexList = OpenMaya.MIntArray()
- # <-- store these ID into vertexIndexList
- fnComponent.getElements(vertexIndexList)
- for k in range(lenVertexList):
- sumAlpha = (vertexColorList[k].r+vertexColorList[k].g+vertexColorList[k].b)/3.0
- if sumAlpha < 0.0:
- sumAlpha = 0.0
- convertedAlphaColor.a = sumAlpha
- vertexColorList.set(convertedAlphaColor,k)
- meshFn.setVertexColors(vertexColorList,vertexIndexList,None)
- elapsed = (time.clock() - start)
- print "done, took:", elapsed
Advertisement
Add Comment
Please, Sign In to add comment