fork download
  1. #!BPY
  2.  
  3. """
  4. Name: 'Binary Obj (.bobj)...'
  5. Blender: 237
  6. Group: 'Import'
  7. Tooltip: 'Load a Binary OBJ File'
  8. """
  9.  
  10. __author__ = "Nils Thuerey, Campbell Barton"
  11. __url__ = ["blender", "elysiun"]
  12. __version__ = "0.9"
  13.  
  14. __bpydoc__ = """\
  15. This script imports BOBJ files to Blender.
  16.  
  17. Usage:
  18.  
  19. Run this script from "File->Import" menu and then load the desired BOBJ file.
  20. """
  21.  
  22. # $Id: bobj_import.py,v 1.3 2005/07/15 11:37:06 sinithue Exp $
  23. #
  24. # --------------------------------------------------------------------------
  25. # BOBJ Import v0.9 by Nils Thuerey, Campbell Barton (AKA Ideasman)
  26. # --------------------------------------------------------------------------
  27. # ***** BEGIN GPL LICENSE BLOCK *****
  28. #
  29. # This program is free software; you can redistribute it and/or
  30. # modify it under the terms of the GNU General Public License
  31. # as published by the Free Software Foundation; either version 2
  32. # of the License, or (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, write to the Free Software Foundation,
  41. # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  42. #
  43. # ***** END GPL LICENCE BLOCK *****
  44. # --------------------------------------------------------------------------
  45.  
  46. globDIR = ''
  47.  
  48. #==============================================#
  49. # Return directory, where is file #
  50. #==============================================#
  51. def pathName(path,name):
  52. length=len(path)
  53. for CH in range(1, length):
  54. if path[length-CH:] == name:
  55. path = path[:length-CH]
  56. break
  57. return path
  58.  
  59. #==============================================#
  60. # Strips the slashes from the back of a string #
  61. #==============================================#
  62. def stripPath(path):
  63. for CH in range(len(path), 0, -1):
  64. if path[CH-1] == "/" or path[CH-1] == "\\":
  65. path = path[CH:]
  66. break
  67. return path
  68.  
  69. #====================================================#
  70. # Strips the prefix off the name before writing #
  71. #====================================================#
  72. def stripName(name): # name is a string
  73. prefixDelimiter = '.'
  74. return name[ : name.find(prefixDelimiter) ]
  75.  
  76.  
  77. from Blender import *
  78. import struct
  79. import gzip
  80. import re
  81.  
  82.  
  83.  
  84. #======================================================================#
  85. # Returns unique name of object (preserve overwriting existing meshes) #
  86. #======================================================================#
  87. def getUniqueName(name):
  88. uniqueInt = 0
  89. while 1:
  90. try:
  91. ob = Object.Get(name)
  92. # Okay, this is working, so lets make a new name
  93. name += '.' + str(uniqueInt)
  94. uniqueInt +=1
  95. except:
  96. if NMesh.GetRaw(name) == None:
  97. return name
  98. else:
  99. name += '.' + str(uniqueInt)
  100. uniqueInt +=1
  101.  
  102.  
  103. #==================================================================================#
  104. # This loads data from .bobj file #
  105. #==================================================================================#
  106. def loadFile(filename):
  107. stripFilename = stripName( stripPath(filename) )
  108. meshname = "mesh_" + stripFilename
  109. meshname = getUniqueName(meshname)
  110. globDIR = pathName(filename, stripPath(filename))
  111. time1 = sys.time()
  112. loadBobj(filename, meshname)
  113. print "bobj ",filename," from ",globDIR,"as",meshname," import time: ", sys.time() - time1
  114.  
  115. #==================================================================================#
  116. # Create object with given name from file
  117. # warning - this is exported to elbeemworld.py!
  118. #==================================================================================#
  119. def loadBobj(filename,objname):
  120.  
  121. # Get the file name with no path or .obj
  122. postfixDelimiter = '.'
  123. postfix = filename[ filename.find(postfixDelimiter)+1 : ]
  124. #postfix = stripPostfix(filename)
  125.  
  126. if re.match( r'.*\.bobj$', filename):
  127. bfile = open(filename, 'rb')
  128. elif re.match( r'.*\.bobj\.gz$', filename):
  129. bfile = gzip.GzipFile( filename, "r" )
  130. else:
  131. print ("Error - invalid postfix '%s', filename '%s', aborting..." % (postfix,filename))
  132. return;
  133.  
  134. # start creating mesh
  135. mesh = NMesh.GetRaw(objname) # CHECK ??
  136. if mesh == None:
  137. mesh = NMesh.GetRaw() # CHECK ??
  138. else:
  139. mesh.verts = []
  140. mesh.faces = []
  141.  
  142.  
  143. vertNumBytes = bfile.read(4)
  144. vertNum = struct.unpack("<l", vertNumBytes)[0]
  145. print "VN:",vertNum
  146. for i in range(0,vertNum):
  147. vxb = bfile.read(4)
  148. vyb = bfile.read(4)
  149. vzb = bfile.read(4)
  150. vx = struct.unpack("f",vxb)[0]
  151. vy = struct.unpack("f",vyb)[0]
  152. vz = struct.unpack("f",vzb)[0]
  153. vert = NMesh.Vert(vx,vy,vz)
  154. vert.no[0] = 1
  155. vert.no[1] = 0
  156. vert.no[2] = 0
  157. mesh.verts.append( vert )
  158. #if i>(vertNum-10): print "V",i,":",vx,vy,vz
  159.  
  160. normNumBytes = bfile.read(4)
  161. normNum = struct.unpack("<l", normNumBytes)[0]
  162. print "NN:",normNum
  163. for i in range(0,normNum):
  164. nxb = bfile.read(4)
  165. nyb = bfile.read(4)
  166. nzb = bfile.read(4)
  167. nx = struct.unpack("f",nxb)[0]
  168. ny = struct.unpack("f",nyb)[0]
  169. nz = struct.unpack("f",nzb)[0]
  170. mesh.verts[i].no[0] = nx
  171. mesh.verts[i].no[1] = ny
  172. mesh.verts[i].no[2] = nz
  173. #if i>(normNum-10): print "V",i,":",nx,ny,nz
  174.  
  175. faceNumBytes = bfile.read(4)
  176. faceNum = struct.unpack("<l", faceNumBytes)[0]
  177. print "FN:",faceNum
  178. for i in range(0,faceNum):
  179. fxb = bfile.read(4)
  180. fyb = bfile.read(4)
  181. fzb = bfile.read(4)
  182. fx = struct.unpack("<l",fxb)[0]
  183. fy = struct.unpack("<l",fyb)[0]
  184. fz = struct.unpack("<l",fzb)[0]
  185. meshface = NMesh.Face()
  186. meshface.append(mesh.verts[fx])
  187. meshface.append(mesh.verts[fy])
  188. meshface.append(mesh.verts[fz])
  189. meshface.smooth = 1
  190.  
  191. mesh.faces.append( meshface )
  192. #if i>(faceNum-10): print "V",i,":",fx,fy,fz
  193.  
  194. obj = NMesh.PutRaw( mesh, objname, 0)
  195. if obj != None:
  196. obj.setName( objname )
  197.  
  198. # TODO FIXME , reuse vertices from surface mesh!
  199. bfile.close()
  200. return;
  201.  
  202. # blender pbrt test call
  203. #./blender_rtag2.36/blender -w -p 0 0 250 250 pbrtt1.blend -P exportPbrt.py ; pbrt expt.pbrt ; exrview expt.exr
  204.  
  205.  
  206. Window.FileSelector(loadFile, 'Import BOBJ')
  207.  
  208.  
Runtime error #stdin #stdout #stderr 0.01s 7116KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 77, in <module>
ImportError: No module named Blender