#!BPY #""" #Name: 'Point Cache 2 to Mesh Shape Keys' #Blender: 245 #Group: 'Import' #Tooltip: 'baked vertex animation to active mesh object.' #""" __author__ = "Matt Ebb, Bill L.Nieuwendorp" __bpydoc__ = """\ This script imports 3DS Max's Point Cache 2 format. It's a modified version of Bill L. Nieuwendorp's MDD importer, included in the Blender source distribution. """ # pc2 importer # # Warning if the vertex order or vertex count differs from the # origonal model the mdd was Baked out from their will be Strange # behavior # # matt@mke3.net # #vertex animation to ShapeKeys with ipo and gives the frame a value of 1.0 #A modifier to read mdd files would be Ideal but thats for another day :) # # ***** BEGIN GPL LICENSE BLOCK ***** # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software Foundation, # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # ***** END GPL LICENCE BLOCK ***** try: from struct import unpack except: unpack = None import Blender from Blender import Mesh, Object, Scene import BPyMessages def pc2_import(filepath, ob, PREF_IPONAME, PREF_OFFSET, PREF_JUMP): print '\n\nimporting point cache 2 "%s"' % filepath Blender.Window.DrawProgressBar (0.0, "Importing pc2 ...") Blender.Window.EditMode(0) Blender.Window.WaitCursor(1) file = open(filepath, 'rb') headerFormat='<12ciiffi' header = unpack(headerFormat, file.read(32)) numPoints = header[13] startFrame = int(header[14]) sampleRate = header[15] numSamples = header[16] print '\tnumPoints:%d startFrame:%d sampleRate:%d numSamples:%d' % (numPoints, startFrame, sampleRate, numSamples) scn = Scene.GetCurrent() ctx = scn.getRenderingContext() Blender.Set("curframe", startFrame+PREF_OFFSET) me = ob.getData(mesh=1) def UpdateMesh(me,fr): for v in me.verts: # 12 is the size of 3 floats x,y,z= unpack('<3f', file.read(12)) v.co[:] = x,y,z me.update() Blender.Window.DrawProgressBar (0.4, "4 Importing pc2 ...") curfr = ctx.currentFrame() print'\tloading pc2 data...' for i in xrange(numSamples): Blender.Set("curframe", i+startFrame+PREF_OFFSET) if len(me.verts) > 1 and (curfr >= startFrame+PREF_OFFSET) and (curfr <= startFrame+PREF_OFFSET+numSamples): UpdateMesh(me, i) ob.insertShapeKey() Blender.Window.DrawProgressBar (0.5, "5 Importing pc2 ...") key= me.key # Add the key of its not there if not key: me.insertKey(1, 'relative') key= me.key key.ipo = Blender.Ipo.New('Key', PREF_IPONAME) ipo = key.ipo # block = key.getBlocks() # not used. all_keys = ipo.curveConsts for i in xrange(PREF_JUMP+1, len(all_keys), PREF_JUMP): curve = ipo.getCurve(i) if curve == None: curve = ipo.addCurve(all_keys[i]) curve.append((startFrame+PREF_OFFSET+i-1,1)) curve.append((startFrame+PREF_OFFSET+i- PREF_JUMP -1,0)) curve.append((startFrame+PREF_OFFSET+i+ PREF_JUMP-1,0)) curve.setInterpolation('Linear') curve.recalc() print 'done' Blender.Window.WaitCursor(0) Blender.Window.DrawProgressBar (1.0, '') def pc2_import_ui(filepath): if BPyMessages.Error_NoFile(filepath): return scn= Scene.GetCurrent() ob_act= scn.objects.active if ob_act == None or ob_act.type != 'Mesh': BPyMessages.Error_NoMeshActive() return PREF_IPONAME = Blender.Draw.Create(filepath.split('/')[-1].split('\\')[-1].split('.')[0]) PREF_OFFSET = Blender.Draw.Create(0) PREF_JUMP = Blender.Draw.Create(1) block = [\ ("Ipo Name: ", PREF_IPONAME, 0, 30, "Ipo name for the new shape key"),\ ("Frame Offset: ", PREF_OFFSET, -3000, 3000, "Offset the Point cache's start frame in the "),\ ("Key Skip: ", PREF_JUMP, 1, 100, "Key Reduction, Skip every Nth Frame")\ ] if not Blender.Draw.PupBlock("Import Point Cache 2", block): return orig_frame = Blender.Get('curframe') pc2_import(filepath, ob_act, PREF_IPONAME.val, PREF_OFFSET.val, PREF_JUMP.val) Blender.Set('curframe', orig_frame) if __name__ == '__main__': if not unpack: Draw.PupMenu('Error%t|This script requires a full python install') Blender.Window.FileSelector(pc2_import_ui, 'Import Point Cache 2', '*.pc2')