fork download
  1. ###for blender 2.79
  2.  
  3. #!BPY
  4. """
  5. Name: 'Binary Obj (.bobj)...'
  6. Blender: 237
  7. Group: 'Import'
  8. Tooltip: 'Load a Binary OBJ File'
  9. """
  10.  
  11. __author__ = "Nils Thuerey, Campbell Barton"
  12. __url__ = ["blender", "elysiun"]
  13. __version__ = "0.9"
  14.  
  15. __bpydoc__ = """\
  16. This script imports BOBJ files to Blender.
  17.  
  18. Usage:
  19.  
  20. Run this script from "File->Import" menu and then load the desired BOBJ file.
  21. """
  22.  
  23. # $Id: bobj_import.py,v 1.3 2005/07/15 11:37:06 sinithue Exp $
  24. #
  25. # --------------------------------------------------------------------------
  26. # BOBJ Import v0.9 by Nils Thuerey, Campbell Barton (AKA Ideasman)
  27. # --------------------------------------------------------------------------
  28. # ***** BEGIN GPL LICENSE BLOCK *****
  29. #
  30. # This program is free software; you can redistribute it and/or
  31. # modify it under the terms of the GNU General Public License
  32. # as published by the Free Software Foundation; either version 2
  33. # of the License, or (at your option) any later version.
  34. #
  35. # This program is distributed in the hope that it will be useful,
  36. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. # GNU General Public License for more details.
  39. #
  40. # You should have received a copy of the GNU General Public License
  41. # along with this program; if not, write to the Free Software Foundation,
  42. # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  43. #
  44. # ***** END GPL LICENCE BLOCK *****
  45. # --------------------------------------------------------------------------
  46.  
  47. globDIR = ''
  48.  
  49. #==============================================#
  50. # Return directory, where is file #
  51. #==============================================#
  52. def pathName(path,name):
  53. length=len(path)
  54. for CH in range(1, length):
  55. if path[length-CH:] == name:
  56. path = path[:length-CH]
  57. break
  58. return path
  59.  
  60. #==============================================#
  61. # Strips the slashes from the back of a string #
  62. #==============================================#
  63. def stripPath(path):
  64. for CH in range(len(path), 0, -1):
  65. if path[CH-1] == "/" or path[CH-1] == "\\":
  66. path = path[CH:]
  67. break
  68. return path
  69.  
  70. #====================================================#
  71. # Strips the prefix off the name before writing #
  72. #====================================================#
  73. def stripName(name): # name is a string
  74. prefixDelimiter = '.'
  75. return name[ : name.find(prefixDelimiter) ]
  76.  
  77.  
  78. #from Blender import *
  79. import bpy,struct
  80. import gzip
  81. import re
  82. import sys
  83. import time
  84. import bmesh
  85.  
  86. #======================================================================#
  87. # Returns unique name of object (preserve overwriting existing meshes) #
  88. #======================================================================#
  89. def getUniqueName(name):
  90. uniqueInt = 0
  91. while 1:
  92. try:
  93. ob = Object.Get(name)
  94. # Okay, this is working, so lets make a new name
  95. name += '.' + str(uniqueInt)
  96. uniqueInt +=1
  97. except:
  98. if NMesh.GetRaw(name) == None:
  99. return name
  100. else:
  101. name += '.' + str(uniqueInt)
  102. uniqueInt +=1
  103.  
  104.  
  105. #==================================================================================#
  106. # This loads data from .bobj file #
  107. #==================================================================================#
  108. def loadFile(filename):
  109. stripFilename = stripName( stripPath(filename) )
  110. meshname = "mesh_" + stripFilename
  111. #meshname = getUniqueName(meshname)
  112. globDIR = pathName(filename, stripPath(filename))
  113. loadBobj(filename, meshname)
  114. print ("bobj ",filename," from ",globDIR,"as",meshname)
  115.  
  116. #==================================================================================#
  117. # Create object with given name from file
  118. # warning - this is exported to elbeemworld.py!
  119. #==================================================================================#
  120. def loadBobj(filename,objname):
  121.  
  122. # Get the file name with no path or .obj
  123. postfixDelimiter = '.'
  124. postfix = filename[ filename.find(postfixDelimiter)+1 : ]
  125. #postfix = stripPostfix(filename)
  126.  
  127. if re.match( r'.*\.bobj$', filename):
  128. bfile = open(filename, 'rb')
  129. elif re.match( r'.*\.bobj\.gz$', filename):
  130. bfile = gzip.GzipFile( filename, "r" )
  131. else:
  132. print ("Error - invalid postfix '%s', filename '%s', aborting..." % (postfix,filename))
  133. return;
  134. verts = []
  135. faces = []
  136. mesh = bpy.data.meshes.new("mesh") # add a new mesh
  137. obj = bpy.data.objects.new(objname, mesh) # add a new object using the mesh
  138.  
  139. vertNumBytes = bfile.read(4)
  140. vertNum = struct.unpack("<l", vertNumBytes)[0]
  141. print("VN:",vertNum)
  142. for i in range(0,vertNum):
  143. vxb = bfile.read(4)
  144. vyb = bfile.read(4)
  145. vzb = bfile.read(4)
  146. vx = struct.unpack("f",vxb)[0]
  147. vy = struct.unpack("f",vyb)[0]
  148. vz = struct.unpack("f",vzb)[0]
  149. v = (vx,vy,vz)
  150. verts.append(v)
  151.  
  152. normNumBytes = bfile.read(4)
  153. normNum = struct.unpack("<l", normNumBytes)[0]
  154. print( "NN:",normNum)
  155. for i in range(0,normNum):
  156. nxb = bfile.read(4)
  157. nyb = bfile.read(4)
  158. nzb = bfile.read(4)
  159. nx = struct.unpack("f",nxb)[0]
  160. ny = struct.unpack("f",nyb)[0]
  161. nz = struct.unpack("f",nzb)[0]
  162.  
  163. faceNumBytes = bfile.read(4)
  164. faceNum = struct.unpack("<l", faceNumBytes)[0]
  165. print( "FN:",faceNum)
  166. for i in range(0,faceNum):
  167. fxb = bfile.read(4)
  168. fyb = bfile.read(4)
  169. fzb = bfile.read(4)
  170. fx = struct.unpack("<l",fxb)[0]
  171. fy = struct.unpack("<l",fyb)[0]
  172. fz = struct.unpack("<l",fzb)[0]
  173. v = (fx,fy,fz)
  174. faces.append(v)
  175. print(faces[1])
  176. mesh.from_pydata(verts, [], faces)
  177. mesh.update()
  178. obj.data = mesh
  179. bpy.context.scene.objects.link(obj)
  180. """
  181. # start creating mesh
  182. mesh = NMesh.GetRaw(objname) # CHECK ??
  183. if mesh == None:
  184. mesh = NMesh.GetRaw() # CHECK ??
  185. else:
  186. mesh.verts = []
  187. mesh.faces = []
  188.  
  189.  
  190. vertNumBytes = bfile.read(4)
  191. vertNum = struct.unpack("<l", vertNumBytes)[0]
  192. print("VN:",vertNum)
  193. for i in range(0,vertNum):
  194. vxb = bfile.read(4)
  195. vyb = bfile.read(4)
  196. vzb = bfile.read(4)
  197. vx = struct.unpack("f",vxb)[0]
  198. vy = struct.unpack("f",vyb)[0]
  199. vz = struct.unpack("f",vzb)[0]
  200. vert = NMesh.Vert(vx,vy,vz)
  201. vert.no[0] = 1
  202. vert.no[1] = 0
  203. vert.no[2] = 0
  204. mesh.verts.append( vert )
  205. #if i>(vertNum-10): print "V",i,":",vx,vy,vz
  206.  
  207. normNumBytes = bfile.read(4)
  208. normNum = struct.unpack("<l", normNumBytes)[0]
  209. print( "NN:",normNum)
  210. for i in range(0,normNum):
  211. nxb = bfile.read(4)
  212. nyb = bfile.read(4)
  213. nzb = bfile.read(4)
  214. nx = struct.unpack("f",nxb)[0]
  215. ny = struct.unpack("f",nyb)[0]
  216. nz = struct.unpack("f",nzb)[0]
  217. mesh.verts[i].no[0] = nx
  218. mesh.verts[i].no[1] = ny
  219. mesh.verts[i].no[2] = nz
  220. #if i>(normNum-10): print "V",i,":",nx,ny,nz
  221.  
  222. faceNumBytes = bfile.read(4)
  223. faceNum = struct.unpack("<l", faceNumBytes)[0]
  224. print( "FN:",faceNum)
  225. for i in range(0,faceNum):
  226. fxb = bfile.read(4)
  227. fyb = bfile.read(4)
  228. fzb = bfile.read(4)
  229. fx = struct.unpack("<l",fxb)[0]
  230. fy = struct.unpack("<l",fyb)[0]
  231. fz = struct.unpack("<l",fzb)[0]
  232. meshface = NMesh.Face()
  233. meshface.append(mesh.verts[fx])
  234. meshface.append(mesh.verts[fy])
  235. meshface.append(mesh.verts[fz])
  236. meshface.smooth = 1
  237.  
  238. mesh.faces.append( meshface )
  239. #if i>(faceNum-10): print "V",i,":",fx,fy,fz
  240.  
  241. obj = NMesh.PutRaw( mesh, objname, 0)
  242. if obj != None:
  243. obj.setName( objname )
  244. """
  245. # TODO FIXME , reuse vertices from surface mesh!
  246. bfile.close()
  247. return;
  248.  
  249. # blender pbrt test call
  250. #./blender_rtag2.36/blender -w -p 0 0 250 250 pbrtt1.blend -P exportPbrt.py ; pbrt expt.pbrt ; exrview expt.exr
  251.  
  252. #Window.FileSelector(loadFile, 'Import BOBJ')
  253.  
  254. def read_some_data(context, filepath, use_some_setting):
  255. """
  256. print("running read_some_data...")
  257. f = open(filepath, 'r', encoding='utf-8')
  258. data = f.read()
  259. f.close()
  260. # would normally load the data here
  261. print(data)
  262. """
  263. loadFile(filepath)
  264. return {'FINISHED'}
  265.  
  266.  
  267. # ImportHelper is a helper class, defines filename and
  268. # invoke() function which calls the file selector.
  269. from bpy_extras.io_utils import ImportHelper
  270. from bpy.props import StringProperty, BoolProperty, EnumProperty
  271. from bpy.types import Operator
  272.  
  273.  
  274. class ImportSomeData(Operator, ImportHelper):
  275. """This appears in the tooltip of the operator and in the generated docs"""
  276. bl_idname = "import_test.some_data" # important since its how bpy.ops.import_test.some_data is constructed
  277. bl_label = "Import Some Data"
  278.  
  279. # ImportHelper mixin class uses this
  280. filename_ext = ".bobj"
  281.  
  282. filter_glob = StringProperty(
  283. default="*.bobj",
  284. options={'HIDDEN'},
  285. )
  286.  
  287. # List of operator properties, the attributes will be assigned
  288. # to the class instance from the operator settings before calling.
  289. use_setting = BoolProperty(
  290. name="Example Boolean",
  291. description="Example Tooltip",
  292. default=True,
  293. )
  294.  
  295. type = EnumProperty(
  296. name="Example Enum",
  297. description="Choose between two items",
  298. items=(('OPT_A', "First Option", "Description one"),
  299. ('OPT_B', "Second Option", "Description two")),
  300. default='OPT_A',
  301. )
  302.  
  303. def execute(self, context):
  304. return read_some_data(context, self.filepath, self.use_setting)
  305.  
  306. # Only needed if you want to add into a dynamic menu
  307. def menu_func_import(self, context):
  308. self.layout.operator(ImportSomeData.bl_idname, text="Text Import Operator")
  309.  
  310.  
  311. def register():
  312. bpy.utils.register_class(ImportSomeData)
  313. bpy.types.INFO_MT_file_import.append(menu_func_import)
  314.  
  315.  
  316. def unregister():
  317. bpy.utils.unregister_class(ImportSomeData)
  318. bpy.types.INFO_MT_file_import.remove(menu_func_import)
  319.  
  320.  
  321. if __name__ == "__main__":
  322. register()
  323.  
  324. # test call
  325. bpy.ops.import_test.some_data('INVOKE_DEFAULT')
  326.  
  327.  
Runtime error #stdin #stdout #stderr 0.05s 62856KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
  File "prog.py", line 1
    /* package whatever; // don't place package name! */
                               ^
SyntaxError: end of line (EOL) while scanning string literal