Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # originally from http://www.losart3d.com/?p=559
- import maya.cmds as cmds
- import struct, os, os.path
- import maya.OpenMaya as om
- def frange(x, y, jump,):
- ar =[]
- while x < y:
- ar.append(x)
- x += jump
- return ar
- def getDagPathFromShape(shape):
- # add the shape to an empty selection list
- sel = om.MSelectionList()
- sel.add(shape)
- # get the dagPath
- dagPathToMesh = om.MDagPath()
- sel.getDagPath( 0, dagPathToMesh )
- # make sure the path is to the shape node (not the transform)
- dagPathToMesh.extendToShape()
- return dagPathToMesh
- def getPointList( dagPath ):
- shapeDagPath = getDagPathFromShape(dagPath)
- # create empty point array
- inMeshMPointArray = om.MPointArray()
- # create function set and get points in world space
- currentInMeshMFnMesh = om.MFnMesh(shapeDagPath)
- currentInMeshMFnMesh.getPoints(inMeshMPointArray, om.MSpace.kWorld)
- # put each point to a list
- pointList = []
- for i in range( inMeshMPointArray.length() ) :
- pointList.append( [inMeshMPointArray[i][0], inMeshMPointArray[i][1], inMeshMPointArray[i][2]] )
- return pointList
- def writePC2File(filename, shape, world=True, zUp=False, startF=0, endF=100, samplesPerFrame=1):
- sampleRate = 1.0/samplesPerFrame
- dagPath = getDagPathFromShape(shape)
- pointList = getPointList(dagPath)
- numPoints = len(pointList)
- totalFrames = endF - startF
- timeRange = frange(startF, startF+totalFrames+sampleRate, sampleRate)
- numSamples = len(timeRange)
- headerFormat='<12ciiffi'
- headerStr = struct.pack(headerFormat, 'P','O','I','N','T','C','A','C','H','E','2','\0', 1, numPoints, startF, sampleRate, numSamples)
- file = open(filename, "wb")
- file.write(headerStr)
- cmds.refresh(su=True)
- sampleFrames = []
- print "startF=%s endF=%s sampleRate=%s shape=%s" % (startF, startF+numSamples, sampleRate,shape)
- for i in timeRange:
- cmds.currentTime(i)
- pointList = getPointList(dagPath)
- for vPos in pointList:
- vFormat = "fff"
- thisVertex = struct.pack(vFormat, vPos[0], vPos[1], vPos[2])
- file.write(thisVertex)
- cmds.refresh(su=False)
- file.flush()
- file.close()
- print filename," was sucessfully written out"
- return True
- '''
- #example
- out = r'C:\testMaya\test.pc2'
- shp = cmds.listRelatives(shapes=True)
- writePC2File(out,shp[0],startF=1000,endF=1200,samplesPerFrame=2)
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement