| Code: |
| # Simple script used for importing camera tracking data generated by BouJou4
# BouJou4 will not export to Blender, though it will export to a text file. # This script will read that file. # Mon 15 Oct 2007 - Simon Beeching (enhzflep) #import Blender; #from Blender import Camera, Object, Scene, NMesh #from Blender import Mathutils #from Blender.Mathutils import * import bpy; from bpy import *; print ('-------------------------------------------------------') print ('------------- BouJou4 import script v0.0 --------------') print ('-------------------------------------------------------') # declaration of function used to load the file def myLoader(fileName): file = open(fileName, 'r'); i = 1; readVerts = False; readCam = False; numFrames = 0; for curLine in file.readlines(): # check for correct file header. # if it's not found, print an error and exit if (i==1) and ('boujou export' not in curLine): print ('ERROR: incorrect file header.') print ('line #: '+str(i)) print (' - expecting "# boujou export: text"'); print (' - found "'+curLine+'"'); file.close(); break; ################################################################################ # # Camera loading code # ################################################################################ # if we find the string that signifies the end of the file, # change our flag to false - we can't read nothing!! if (readCam == True): coOrds = curLine.split() if (len(coOrds) == 0): readCam = False; print('('+str(i)+'): Found end of camera block'); allObj = Object.Get() for curObj in allObj: if curObj.name[:10]=='boujou_tmp': cur.unlink(curObj); # CAMERA FRAME DATA FOUND!! else: # set the camera rotation/translation matrix rX = [float(coOrds[0]), float(coOrds[1]), float(coOrds[2]), 0.0]; rY = [float(coOrds[3]), float(coOrds[4]), float(coOrds[5]), 0.0]; rZ = [float(coOrds[6]), float(coOrds[7]), float(coOrds[8]), 0.0]; tR = [float(coOrds[9]), float(coOrds[11])*-1.0, float(coOrds[10]), 1.0]; tmpObj.setMatrix(Mathutils.Matrix(rX, rY, rZ, tR)); # set the lens tmpCam.lens = float(coOrds[12]); # add point(keyframe) for camera position IPOs locx.addBezier((numFrames+1, tmpObj.LocX)) locy.addBezier((numFrames+1, tmpObj.LocY)) locz.addBezier((numFrames+1, tmpObj.LocZ)) # add point(keyframe) for the camera rotation IPOs rotx.addBezier((numFrames+1, (tmpObj.RotX*18/3.141593)-9)) roty.addBezier((numFrames+1, -1.0*tmpObj.RotZ*18/3.141593)) rotz.addBezier((numFrames+1, 1.0*tmpObj.RotY*18/3.141593)) # add point on IPO curve for lens (focal distance) lenscurve.addBezier((numFrames+1, tmpCam.lens)) # increment the index used for setting IPOs to point to next frame numFrames += 1; # CAMERA BLOCK FOUND!! # if '#R(0,0)' is found, then we have the start of the vertex data if ('#R(0,0)' in curLine): print( '(' +str(i) +'): Camera data found in export file'); readCam = True; # set flag to indicate we're in a camera block cRend = Camera.New('persp'); cRend.lens = 35.0 cRend.setDrawSize(1.0); oRend = Object.New('Camera'); oRend.name = 'BouJou_Cam' oRend.link(cRend); cur = Scene.GetCurrent(); cur.link(oRend); cur.setCurrentCamera(oRend); ipo = Blender.Ipo.New('Object', 'render_cam_objipo'); oRend.setIpo(ipo); # create the ipo curves that we'll be needing locx = ipo.addCurve('LocX') locx.setInterpolation('Linear') locy = ipo.addCurve('LocY') locy.setInterpolation('Linear') locz = ipo.addCurve('LocZ') locz.setInterpolation('Linear') rotx = ipo.addCurve('RotX') rotx.setInterpolation('Linear') roty = ipo.addCurve('RotY') roty.setInterpolation('Linear') rotz = ipo.addCurve('RotZ') rotz.setInterpolation('Linear') camipo = Blender.Ipo.New('Camera','render_cam_camipo') cRend.setIpo(camipo) lenscurve = camipo.addCurve('Lens') lenscurve.setInterpolation('Linear') # create the temporary camera that is used to convert our camera matrix # into rotation and translation figures. This dummy camera also holds the # lens info. This temp camera is needed to create the IPO curves tmpCam = Camera.New('persp'); tmpCam.lens = 35.0; tmpObj = Object.New('Camera'); tmpObj.name = 'boujou_tmp' tmpObj.link(tmpCam); cur.link(tmpObj); ################################################################################ # # Vertex loading code # ################################################################################ # if we find the string that signifies the end of the file, # change our flag to false - there's nothing left to read if ('#End of boujou export file' in curLine): print( '(' +str(i) +'): Found end of boujou export file'); readVerts = False; mesh.update() cur.update() # if we're currently allowed to read vertex positions, then # split the line up into individual 'words' - putting the first 3 # into a 3 element array used to hold 1 point if (readVerts==True): coOrds = curLine.split(); if len(coOrds): x = float(coOrds[0]); y = float(coOrds[1]); z = float(coOrds[2]); v = NMesh.Vert(x, -z, y) mesh.verts.append(v); # if '#x y z' is found, then we have the start of the vertex data # (the x, y and x are all separated with tab characters (\t)) if ('#x\ty\tz' in curLine): readVerts = True; ob = Object.New('Mesh', 'BouJouData') ob.setLocation(0.0, 0.0, 0.0) mesh = ob.getData(); cur = Scene.getCurrent() cur.link(ob) print( '(' +str(i) +'): Vertex data found in export file'); ########################################################################## # increment the file's line counter i += 1; ########### End of loop performed on each lie of the file ################ print ('Total of '+str(numFrames)+' frames tracked.'); file.close(); ##################### End of myLoader(filename) ############################## # Script starts execution here # call function myLoader with the file chosen in a file open dialog # use the string "Import .txt" on the button Blender.Window.FileSelector(myLoader, "Import .txt"); |
| Code: |
| Blender.Window.FileSelector(myLoader, "Import .txt"); |
| Code: |
|
import bpy,os from io_utils import ImportHelper def main(context,self): directory, file = os.path.split(self.filepath) print("Directory:"+directory) print("File: "+file) class OBJECT_PT_hello(bpy.types.Panel): bl_label = "Hello World Panel" bl_space_type = "PROPERTIES" bl_region_type = "WINDOW" bl_context = "object" def draw(self, context): layout = self.layout obj = context.object row = layout.row() row.label(text="Import File", icon='WORLD_DATA') row = layout.row() row.operator(SimpleOperator.bl_idname) class SimpleOperator(bpy.types.Operator, ImportHelper): '''Tooltip''' bl_idname = "object.simple_operator" bl_label = "Simple Object Operator" @classmethod def poll(cls, context): return context.active_object != None def draw(self,context): layout = self.layout obj = context.object row = layout.row() row.label(text="Import File in FileBrowser", icon='WORLD_DATA') def execute(self, context): main(context,self) return {'FINISHED'} def invoke(self,context,event): context.window_manager.fileselect_add(self) return {'RUNNING_MODAL'} def BoujouImport(self, context): self.layout.operator(SimpleOperator.bl_idname, text="Bojou Import", icon='PLUGIN') def register(): bpy.utils.register_class(SimpleOperator) bpy.utils.register_class(OBJECT_PT_hello) bpy.types.INFO_MT_file_import.append(BoujouImport) def unregister(): bpy.utils.unregister_class(SimpleOperator) bpy.utils.unregister_class(OBJECT_PT_hello) bpy.types.INFO_MT_file_import.remove(BoujouImport) if __name__ == "__main__": register() # test call # bpy.ops.object.simple_operator() |
| Quote: |
| Do you have any idea? I don't undestand... maybe it's my fault! |
| Code: |
|
def myLoader(fileName): file = open(fileName, 'r'); ... ... |
| Code: |
| def MyLoader(context,self):
directory, file = os.path.split(self.filepath) print("Directory:"+directory) print("File: "+file) f = open(self.filepath,'r') ... ... |
| Code: |
|
main(context,self) |
| Code: |
|
MyLoader(context,self) |