#!BPY
"""
Name: 'Binary Obj (.bobj)...'
Blender: 237
Group: 'Import'
Tooltip: 'Load a Binary OBJ File'
"""
__author__ = "Nils Thuerey, Campbell Barton"
__url__ = ["blender", "elysiun"]
__version__ = "0.9"
__bpydoc__ = """\
This script imports BOBJ files to Blender.
Usage:
Run this script from "File->Import" menu and then load the desired BOBJ file.
"""
# $Id: bobj_import.py,v 1.3 2005/07/15 11:37:06 sinithue Exp $
#
# --------------------------------------------------------------------------
# BOBJ Import v0.9 by Nils Thuerey, Campbell Barton (AKA Ideasman)
# --------------------------------------------------------------------------
# ***** 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 *****
# --------------------------------------------------------------------------
globDIR = ''
#==============================================#
# Return directory, where is file #
#==============================================#
def pathName(path,name):
length=len(path)
for CH in range(1, length):
if path[length-CH:] == name:
path = path[:length-CH]
break
return path
#==============================================#
# Strips the slashes from the back of a string #
#==============================================#
def stripPath(path):
for CH in range(len(path), 0, -1):
if path[CH-1] == "/" or path[CH-1] == "\\":
path = path[CH:]
break
return path
#====================================================#
# Strips the prefix off the name before writing #
#====================================================#
def stripName(name): # name is a string
prefixDelimiter = '.'
return name[ : name.find(prefixDelimiter) ]
from Blender import *
import struct
import gzip
import re
#======================================================================#
# Returns unique name of object (preserve overwriting existing meshes) #
#======================================================================#
def getUniqueName(name):
uniqueInt = 0
while 1:
try:
ob = Object.Get(name)
# Okay, this is working, so lets make a new name
name += '.' + str(uniqueInt)
uniqueInt +=1
except:
if NMesh.GetRaw(name) == None:
return name
else:
name += '.' + str(uniqueInt)
uniqueInt +=1
#==================================================================================#
# This loads data from .bobj file #
#==================================================================================#
def loadFile(filename):
stripFilename = stripName( stripPath(filename) )
meshname = "mesh_" + stripFilename
meshname = getUniqueName(meshname)
globDIR = pathName(filename, stripPath(filename))
time1 = sys.time()
loadBobj(filename, meshname)
print "bobj ",filename," from ",globDIR,"as",meshname," import time: ", sys.time() - time1
#==================================================================================#
# Create object with given name from file
# warning - this is exported to elbeemworld.py!
#==================================================================================#
def loadBobj(filename,objname):
# Get the file name with no path or .obj
postfixDelimiter = '.'
postfix = filename[ filename.find(postfixDelimiter)+1 : ]
#postfix = stripPostfix(filename)
if re.match( r'.*\.bobj$', filename):
bfile = open(filename, 'rb')
elif re.match( r'.*\.bobj\.gz$', filename):
bfile = gzip.GzipFile( filename, "r" )
else:
print ("Error - invalid postfix '%s', filename '%s', aborting..." % (postfix,filename))
return;
# start creating mesh
mesh = NMesh.GetRaw(objname) # CHECK ??
if mesh == None:
mesh = NMesh.GetRaw() # CHECK ??
else:
mesh.verts = []
mesh.faces = []
vertNumBytes = bfile.read(4)
vertNum = struct.unpack("<l", vertNumBytes)[0]
print "VN:",vertNum
for i in range(0,vertNum):
vxb = bfile.read(4)
vyb = bfile.read(4)
vzb = bfile.read(4)
vx = struct.unpack("f",vxb)[0]
vy = struct.unpack("f",vyb)[0]
vz = struct.unpack("f",vzb)[0]
vert = NMesh.Vert(vx,vy,vz)
vert.no[0] = 1
vert.no[1] = 0
vert.no[2] = 0
mesh.verts.append( vert )
#if i>(vertNum-10): print "V",i,":",vx,vy,vz
normNumBytes = bfile.read(4)
normNum = struct.unpack("<l", normNumBytes)[0]
print "NN:",normNum
for i in range(0,normNum):
nxb = bfile.read(4)
nyb = bfile.read(4)
nzb = bfile.read(4)
nx = struct.unpack("f",nxb)[0]
ny = struct.unpack("f",nyb)[0]
nz = struct.unpack("f",nzb)[0]
mesh.verts[i].no[0] = nx
mesh.verts[i].no[1] = ny
mesh.verts[i].no[2] = nz
#if i>(normNum-10): print "V",i,":",nx,ny,nz
faceNumBytes = bfile.read(4)
faceNum = struct.unpack("<l", faceNumBytes)[0]
print "FN:",faceNum
for i in range(0,faceNum):
fxb = bfile.read(4)
fyb = bfile.read(4)
fzb = bfile.read(4)
fx = struct.unpack("<l",fxb)[0]
fy = struct.unpack("<l",fyb)[0]
fz = struct.unpack("<l",fzb)[0]
meshface = NMesh.Face()
meshface.append(mesh.verts[fx])
meshface.append(mesh.verts[fy])
meshface.append(mesh.verts[fz])
meshface.smooth = 1
mesh.faces.append( meshface )
#if i>(faceNum-10): print "V",i,":",fx,fy,fz
obj = NMesh.PutRaw( mesh, objname, 0)
if obj != None:
obj.setName( objname )
# TODO FIXME , reuse vertices from surface mesh!
bfile.close()
return;
# blender pbrt test call
#./blender_rtag2.36/blender -w -p 0 0 250 250 pbrtt1.blend -P exportPbrt.py ; pbrt expt.pbrt ; exrview expt.exr
Window.FileSelector(loadFile, 'Import BOBJ')