Advertisement
Guest User

Exporting .pc2 files straight out of maya... optimized

a guest
May 16th, 2014
600
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # originally from http://www.losart3d.com/?p=559
  2.  
  3. import maya.cmds as cmds
  4. import struct, os, os.path
  5. import maya.OpenMaya as om
  6.  
  7. def frange(x, y, jump,):
  8.     ar =[]
  9.     while x < y:
  10.         ar.append(x)
  11.         x += jump
  12.     return ar
  13.  
  14.  
  15. def getDagPathFromShape(shape):
  16.     # add the shape to an empty selection list
  17.     sel = om.MSelectionList()
  18.     sel.add(shape)
  19.  
  20.     # get the dagPath
  21.     dagPathToMesh = om.MDagPath()
  22.     sel.getDagPath( 0, dagPathToMesh )
  23.  
  24.     # make sure the path is to the shape node (not the transform)
  25.     dagPathToMesh.extendToShape()
  26.    
  27.     return dagPathToMesh
  28.  
  29.  
  30. def getPointList( dagPath ):
  31.     shapeDagPath = getDagPathFromShape(dagPath)
  32.  
  33.     # create empty point array
  34.     inMeshMPointArray = om.MPointArray()
  35.  
  36.     # create function set and get points in world space
  37.     currentInMeshMFnMesh = om.MFnMesh(shapeDagPath)
  38.     currentInMeshMFnMesh.getPoints(inMeshMPointArray, om.MSpace.kWorld)
  39.  
  40.     # put each point to a list
  41.     pointList = []
  42.  
  43.     for i in range( inMeshMPointArray.length() ) :
  44.         pointList.append( [inMeshMPointArray[i][0], inMeshMPointArray[i][1], inMeshMPointArray[i][2]] )
  45.  
  46.     return pointList
  47.  
  48.  
  49. def writePC2File(filename, shape, world=True, zUp=False, startF=0, endF=100, samplesPerFrame=1):
  50.     sampleRate = 1.0/samplesPerFrame
  51.    
  52.     dagPath = getDagPathFromShape(shape)
  53.     pointList = getPointList(dagPath)
  54.     numPoints = len(pointList)
  55.  
  56.     totalFrames = endF - startF
  57.     timeRange = frange(startF, startF+totalFrames+sampleRate, sampleRate)
  58.     numSamples = len(timeRange)
  59.  
  60.     headerFormat='<12ciiffi'
  61.     headerStr = struct.pack(headerFormat, 'P','O','I','N','T','C','A','C','H','E','2','\0', 1, numPoints, startF, sampleRate, numSamples)
  62.  
  63.     file = open(filename, "wb")
  64.     file.write(headerStr)
  65.  
  66.     cmds.refresh(su=True)
  67.     sampleFrames = []
  68.  
  69.     print "startF=%s endF=%s sampleRate=%s shape=%s" % (startF, startF+numSamples, sampleRate,shape)
  70.  
  71.     for i in timeRange:
  72.         cmds.currentTime(i)
  73.         pointList = getPointList(dagPath)
  74.         for vPos in pointList:
  75.             vFormat = "fff"
  76.             thisVertex = struct.pack(vFormat, vPos[0], vPos[1], vPos[2])
  77.             file.write(thisVertex)
  78.  
  79.     cmds.refresh(su=False)
  80.     file.flush()
  81.     file.close()
  82.     print filename," was sucessfully written out"
  83.     return True
  84.  
  85. '''
  86. #example  
  87. out = r'C:\testMaya\test.pc2'  
  88. shp = cmds.listRelatives(shapes=True)  
  89. writePC2File(out,shp[0],startF=1000,endF=1200,samplesPerFrame=2)
  90. '''
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement