Advertisement
Guest User

Boujou To Blender 2.49

a guest
Oct 11th, 2012
2,269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.46 KB | None | 0 0
  1. #!BPY
  2.  
  3. """
  4. Name: 'BOUJOU 5(.txt) ...'
  5. Blender: 249
  6. Group: 'Import'
  7. Tooltip: 'Import scene from Boujou 5 text export (.txt)'
  8. """
  9.  
  10.  
  11. __author__ = "Blup1980"
  12. __url__ = ("Project homepage, http://sourceforge.net/projects/boujouimport/")
  13. __version__ = "0.0"
  14. __email__ = ""
  15. __bpydoc__ = """\
  16.  
  17.  
  18. Description: Imports a Boujou 5 txt file into a Blender scene.
  19.  
  20.  
  21. Usage: Run the script from the menu or inside Blender.
  22. """
  23.  
  24.  
  25. # **************************************************************************
  26. #    Blender Boujou import script
  27. #    Copyright (C) 2010 Blup1980
  28. #
  29. #    This program is free software: you can redistribute it and/or modify
  30. #    it under the terms of the GNU General Public License as published by
  31. #    the Free Software Foundation, either version 3 of the License, or
  32. #    (at your option) any later version.
  33. #
  34. #    This program is distributed in the hope that it will be useful,
  35. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  37. #    GNU General Public License for more details.
  38. #
  39. #    You should have received a copy of the GNU General Public License
  40. #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  41. #
  42. # **************************** Revision History ****************************
  43. #
  44. #    Revision 0.0  2010 mai 16 Blup1980
  45. #    Based on enhzflep work on boujou4 import script.
  46. #    Axis are now the same in boujou and blender
  47. #    Script now in blender import menu
  48. #    Still have issues with the Focal length
  49. #    
  50. # **************************************************************************
  51.  
  52.  
  53.  
  54.  
  55. import Blender;
  56. import math;
  57. from Blender import Camera, Object, Scene, NMesh
  58. from Blender import Mathutils
  59. from Blender.Mathutils import *
  60.  
  61.  
  62. print '-------------------------------------------------------'
  63. print '------------- Boujou5 import script v0.0 --------------'
  64. print '-------------------------------------------------------'
  65.  
  66.  
  67.  
  68.  
  69. # declaration of function used to load the file
  70. def myLoader(fileName):
  71.  
  72.  
  73.     file = open(fileName, 'r');
  74.     i = 1;
  75.     readVerts = False;
  76.     readCam = False;
  77.     numFrames = 0;
  78.  
  79.  
  80.     for curLine in file.readlines():
  81.         # check for correct file header.
  82.         # if it's not found, print an error and exit
  83.         if (i==1) and ('boujou export' not in curLine):
  84.             print 'ERROR: incorrect file header.'
  85.             print 'line #: '+str(i)
  86.             print ' - expecting "# boujou export: text"';
  87.             print ' - found "'+curLine+'"';
  88.             file.close();
  89.             break;
  90.    
  91.    
  92.     ################################################################################
  93.     #
  94.     #  Camera loading code
  95.     #
  96.     ################################################################################
  97.    
  98.         # if we find the string that signifies the end of the file,
  99.         # change our flag to false - we can't read nothing!!
  100.         if (readCam == True):
  101.             coOrds = curLine.split()
  102.             if  (len(coOrds) == 0):
  103.                 readCam = False;
  104.                 print('('+str(i)+'): Found end of camera block');
  105.                 allObj = Object.Get()
  106.                 for curObj in allObj:
  107.  
  108.  
  109.                     if curObj.name[:10]=='boujou_tmp':
  110.                         cur.unlink(curObj);
  111.  
  112.  
  113.    
  114.         # CAMERA FRAME DATA FOUND!!
  115.             else:
  116.                 # set the camera rotation/translation matrix
  117.                 rX = [float(coOrds[0]), float(coOrds[1]), float(coOrds[2]), 0.0];
  118.                 rY = [float(coOrds[3]), float(coOrds[4]), float(coOrds[5]), 0.0];
  119.                 rZ = [float(coOrds[6]), float(coOrds[7]), float(coOrds[8]), 0.0];
  120.                 tR = [float(coOrds[9]), float(coOrds[10]), float(coOrds[11]), 1.0];
  121.                 tmpObj.setMatrix(Mathutils.Matrix(rX, rY, rZ, tR));
  122.                
  123.                 # compute the FOV in rad
  124.                 fovRad = 2.0*math.atan(float(filmSize[2])/(2.0*float(coOrds[12])));
  125.                 # This is working and gives the same fov than doing a 3ds export and reading
  126.                 # directly the fov on that file.
  127.                
  128.                 # This is a dirty workaround ! The theoretical factor here is 16. But I need to put more. Why ?
  129.                 # set the lens
  130.                 tmpCam.lens = 16.48 / math.tan( fovRad / 2.0);
  131.                
  132.                 # add point(keyframe) for camera position IPOs
  133.                 locx.addBezier((numFrames+1, tmpObj.LocX))
  134.                 locy.addBezier((numFrames+1, tmpObj.LocY))
  135.                 locz.addBezier((numFrames+1, tmpObj.LocZ))
  136.    
  137.                 # add point(keyframe) for the camera rotation IPOs
  138.                 rotx.addBezier((numFrames+1, (tmpObj.RotX*18/3.141593)-18))
  139.                 roty.addBezier((numFrames+1, tmpObj.RotY*18/3.141593))
  140.                 rotz.addBezier((numFrames+1, tmpObj.RotZ*18/3.141593))
  141.  
  142.  
  143.                 # add point on IPO curve for lens (focal distance)
  144.                 lenscurve.addBezier((numFrames+1, tmpCam.lens))
  145.                                        
  146.                 # increment the index used for setting IPOs to point to next frame
  147.                 numFrames += 1;
  148.    
  149.         # if '#Filmback Size' is found, then we have the size of the film to compute the FOV
  150.         if ('#Filmback Size ' in curLine):
  151.             filmSize = curLine.split()        
  152.        
  153.         # if '#R(0,0)' is found, then we have the start of the vertex data
  154.         if ('#R(0,0)' in curLine):
  155.             print( '(' +str(i) +'): Camera data found in export file');
  156.             readCam = True;            # set flag to indicate we're in a camera block
  157.             cRend = Camera.New('persp');
  158.             cRend.lens = 35.0
  159.             cRend.setDrawSize(1.0);
  160.             oRend = Object.New('Camera');
  161.             oRend.name = 'BouJou_Cam'
  162.             oRend.link(cRend);
  163.             cur = Scene.GetCurrent();
  164.             cur.link(oRend);
  165.             cur.setCurrentCamera(oRend);
  166.             ipo = Blender.Ipo.New('Object', 'render_cam_objipo');
  167.             oRend.setIpo(ipo);
  168.            
  169.             # create the ipo curves that we'll be needing
  170.             locx = ipo.addCurve('LocX')
  171.             locx.setInterpolation('Linear')
  172.             locy = ipo.addCurve('LocY')
  173.             locy.setInterpolation('Linear')
  174.             locz = ipo.addCurve('LocZ')
  175.             locz.setInterpolation('Linear')
  176.             rotx = ipo.addCurve('RotX')
  177.             rotx.setInterpolation('Linear')
  178.             roty = ipo.addCurve('RotY')
  179.             roty.setInterpolation('Linear')
  180.             rotz = ipo.addCurve('RotZ')
  181.             rotz.setInterpolation('Linear')
  182.             camipo = Blender.Ipo.New('Camera','render_cam_camipo')
  183.             cRend.setIpo(camipo)
  184.             lenscurve = camipo.addCurve('Lens')
  185.             lenscurve.setInterpolation('Linear')
  186.    
  187.             # create the temporary camera that is used to convert our camera matrix
  188.             # into rotation and translation figures. This dummy camera also holds the
  189.             # lens info. This temp camera is needed to create the IPO curves
  190.             tmpCam = Camera.New('persp');
  191.             tmpCam.lens = 35.0;
  192.             tmpObj = Object.New('Camera');
  193.             tmpObj.name = 'boujou_tmp'
  194.             tmpObj.link(tmpCam);
  195.             cur.link(tmpObj);
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.         ################################################################################
  203.         #
  204.         #  Vertex loading code
  205.         #
  206.         ################################################################################
  207.         # if we find the string that signifies the end of the file,
  208.         # change our flag to false - there's nothing left to read
  209.         if ('#End of boujou export file' in curLine):
  210.             print( '(' +str(i) +'): Found end of boujou export file');
  211.             readVerts = False;
  212.             mesh.update()
  213.             cur.update()
  214.  
  215.  
  216.         # if we're currently allowed to read vertex positions, then
  217.         # split the line up into individual 'words' - putting the first 3
  218.         # into a 3 element array used to hold 1 point
  219.         if (readVerts==True):
  220.             coOrds = curLine.split();
  221.             if len(coOrds):
  222.                 x = float(coOrds[0]);
  223.                 y = float(coOrds[1]);
  224.                 z = float(coOrds[2]);
  225.                 v = NMesh.Vert(x, y, z)
  226.                 mesh.verts.append(v);
  227.    
  228.         # if '#x y z' is found, then we have the start of the vertex data
  229.         # (the x, y and x are all separated with tab characters (\t))
  230.         if ('#x\ty\tz' in curLine):
  231.             readVerts = True;
  232.  
  233.  
  234.             ob = Object.New('Mesh', 'BouJouData')
  235.             ob.setLocation(0.0, 0.0, 0.0)
  236.             mesh = ob.getData();
  237.             cur = Scene.getCurrent()
  238.             cur.link(ob)
  239.  
  240.  
  241.             print( '(' +str(i) +'): Vertex data found in export file');
  242.         ##########################################################################
  243.  
  244.  
  245.         # increment the file's line counter
  246.         i += 1;
  247.         ########### End of loop performed on each lie of the file ################
  248.        
  249.     print 'Total of '+str(numFrames)+' frames tracked.';
  250.     file.close();
  251.     ##################### End of myLoader(filename) ##############################
  252.    
  253.    
  254. # Script starts execution here
  255. # call function myLoader with the file chosen in a file open dialog
  256. # use the string "Import  .txt" on the button
  257. Blender.Window.FileSelector(myLoader, "Import .txt")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement