Initial revision
This commit is contained in:
157
intern/python/objexport/BlendImport.py
Normal file
157
intern/python/objexport/BlendImport.py
Normal file
@@ -0,0 +1,157 @@
|
||||
#! /usr/bin/env python
|
||||
|
||||
#######################
|
||||
# (c) Jan Walter 2000 #
|
||||
#######################
|
||||
|
||||
# CVS
|
||||
# $Author$
|
||||
# $Date$
|
||||
# $RCSfile$
|
||||
# $Revision$
|
||||
|
||||
TAB = " "
|
||||
|
||||
vertices = []
|
||||
vertexNormals = []
|
||||
faces = []
|
||||
materials = []
|
||||
switchMaterial = []
|
||||
currentMaterial = []
|
||||
materialIndex = 0
|
||||
|
||||
def addVertex(x, y, z):
|
||||
global vertices
|
||||
vertices.append([x, y, z])
|
||||
return len(vertices)
|
||||
|
||||
def addVertexNormal(i, j, k):
|
||||
global vertexNormals
|
||||
vertexNormals.append([i, j, k])
|
||||
return len(vertexNormals)
|
||||
|
||||
def addFace(vi, ti, ni):
|
||||
global faces
|
||||
faces.append([vi, ti, ni])
|
||||
return len(faces)
|
||||
|
||||
def addMaterial(name, Kd, Ks, Ka):
|
||||
global materials
|
||||
materials.append([name, Kd, Ks, Ka])
|
||||
return len(materials)
|
||||
|
||||
def findMaterial(name):
|
||||
global materials
|
||||
for material in materials:
|
||||
if material[0] == name:
|
||||
return material
|
||||
|
||||
def setCurrentMaterial(name):
|
||||
global switchMaterial, faces, currentMaterial
|
||||
switchMaterial.append(len(faces))
|
||||
currentMaterial.append(name)
|
||||
|
||||
class OpenInventor:
|
||||
def export(self, filename, useNormals = 1, useMaterials = 1):
|
||||
global vertices, vertexNormals, faces, materials, switchMaterial
|
||||
global currentMaterial, materialIndex
|
||||
file = open(filename, "w")
|
||||
file.write("#Inventor V2.1 ascii\n\n")
|
||||
file.write("Separator {\n")
|
||||
############
|
||||
# vertices #
|
||||
############
|
||||
file.write("%sCoordinate3 {\n" % (TAB, ))
|
||||
file.write("%spoint [ \n" % (TAB*2, ))
|
||||
for i in xrange(len(vertices)-1):
|
||||
x, y, z = vertices[i]
|
||||
file.write("%s %s %s %s,\n" % (TAB*2, x, y, z))
|
||||
x, y, z = vertices[i+1]
|
||||
file.write("%s %s %s %s\n" % (TAB*2, x, y, z))
|
||||
file.write("%s ] \n" % (TAB*2, ))
|
||||
file.write("%s}\n" % (TAB, ))
|
||||
###########
|
||||
# normals #
|
||||
###########
|
||||
if useNormals:
|
||||
file.write("%sNormal {\n" % (TAB, ))
|
||||
file.write("%svector [ \n" % (TAB*2, ))
|
||||
for i in xrange(len(vertexNormals)-1):
|
||||
x, y, z = vertexNormals[i]
|
||||
file.write("%s %s %s %s,\n" % (TAB*2, x, y, z))
|
||||
x, y, z = vertexNormals[i-1]
|
||||
file.write("%s %s %s %s\n" % (TAB*2, x, y, z))
|
||||
file.write("%s ] \n" % (TAB*2, ))
|
||||
file.write("%s}\n" % (TAB, ))
|
||||
#########
|
||||
# faces #
|
||||
#########
|
||||
switchMaterial.append(len(faces))
|
||||
for si in xrange(len(switchMaterial) - 1):
|
||||
i1, i2 = switchMaterial[si], switchMaterial[si+1]
|
||||
# --------------
|
||||
# write material
|
||||
# --------------
|
||||
if materials:
|
||||
name = currentMaterial[materialIndex]
|
||||
material = findMaterial(name)
|
||||
if useMaterials:
|
||||
file.write("%sMaterial {\n" % (TAB, ))
|
||||
file.write("%sambientColor %s %s %s\n" %
|
||||
(TAB*2,
|
||||
material[3][0],
|
||||
material[3][1],
|
||||
material[3][2]))
|
||||
file.write("%sdiffuseColor %s %s %s\n" %
|
||||
(TAB*2,
|
||||
material[1][0],
|
||||
material[1][1],
|
||||
material[1][2]))
|
||||
file.write("%sspecularColor %s %s %s\n" %
|
||||
(TAB*2,
|
||||
material[2][0],
|
||||
material[2][1],
|
||||
material[2][2]))
|
||||
file.write("%s}\n" % (TAB, ))
|
||||
# -----------
|
||||
# write faces
|
||||
# -----------
|
||||
file.write("%sIndexedFaceSet {\n" % (TAB, ))
|
||||
# indices for vertices
|
||||
file.write("%scoordIndex [ \n" % (TAB*2, ))
|
||||
for i in xrange(i1, i2-1):
|
||||
indices = faces[i][0]
|
||||
istring = ""
|
||||
for index in indices:
|
||||
# indices begin with 0 in OpenInventor
|
||||
istring = istring + "%s, " % (index - 1, )
|
||||
file.write("%s %s-1, \n" % (TAB*2, istring))
|
||||
indices = faces[i+1][0]
|
||||
istring = ""
|
||||
for index in indices:
|
||||
# indices begin with 0 in OpenInventor
|
||||
istring = istring + "%s, " % (index - 1, )
|
||||
file.write("%s %s-1\n" % (TAB*2, istring))
|
||||
file.write("%s ] \n" % (TAB*2, ))
|
||||
# update materialIndex
|
||||
materialIndex = materialIndex + 1
|
||||
if useNormals:
|
||||
# indices for normals
|
||||
file.write("%snormalIndex [ \n" % (TAB*2, ))
|
||||
for i in xrange(i1, i2-1):
|
||||
indices = faces[i][2]
|
||||
istring = ""
|
||||
for index in indices:
|
||||
# indices begin with 0 in OpenInventor
|
||||
istring = istring + "%s, " % (index - 1, )
|
||||
file.write("%s %s-1, \n" % (TAB*2, istring))
|
||||
indices = faces[i+1][2]
|
||||
istring = ""
|
||||
for index in indices:
|
||||
# indices begin with 0 in OpenInventor
|
||||
istring = istring + "%s, " % (index - 1, )
|
||||
file.write("%s %s-1\n" % (TAB*2, istring))
|
||||
file.write("%s ] \n" % (TAB*2, ))
|
||||
file.write("%s}\n" % (TAB, ))
|
||||
file.write("}\n")
|
||||
file.close()
|
119
intern/python/objexport/objimport.py
Normal file
119
intern/python/objexport/objimport.py
Normal file
@@ -0,0 +1,119 @@
|
||||
#! /usr/bin/env python
|
||||
|
||||
#######################
|
||||
# (c) Jan Walter 2000 #
|
||||
#######################
|
||||
|
||||
# CVS
|
||||
# $Author$
|
||||
# $Date$
|
||||
# $RCSfile$
|
||||
# $Revision$
|
||||
|
||||
import BlendImport
|
||||
import os
|
||||
import sys
|
||||
import string
|
||||
|
||||
def usage():
|
||||
print "usage: python objimport.py objfile"
|
||||
|
||||
def testObjImport(filename):
|
||||
print filename
|
||||
vcount = 0
|
||||
vncount = 0
|
||||
fcount = 0
|
||||
file = open(filename, "r")
|
||||
lines = file.readlines()
|
||||
linenumber = 1
|
||||
for line in lines:
|
||||
words = string.split(line)
|
||||
if words and words[0] == "#":
|
||||
pass # ignore comments
|
||||
elif words and words[0] == "v":
|
||||
vcount = vcount + 1
|
||||
x = string.atof(words[1])
|
||||
y = string.atof(words[2])
|
||||
z = string.atof(words[3])
|
||||
##print "Vertex %s: (%s, %s, %s)" % (vcount, x, y, z)
|
||||
index = BlendImport.addVertex(x, y, z)
|
||||
##print "addVertex(%s)" % index
|
||||
elif words and words[0] == "vn":
|
||||
vncount = vncount + 1
|
||||
i = string.atof(words[1])
|
||||
j = string.atof(words[2])
|
||||
k = string.atof(words[3])
|
||||
##print "VertexNormal %s: (%s, %s, %s)" % (vncount, i, j, k)
|
||||
index = BlendImport.addVertexNormal(i, j, k)
|
||||
##print "addVertexNormal(%s)" % index
|
||||
elif words and words[0] == "f":
|
||||
fcount = fcount + 1
|
||||
vi = [] # vertex indices
|
||||
ti = [] # texture indices
|
||||
ni = [] # normal indices
|
||||
words = words[1:]
|
||||
lcount = len(words)
|
||||
for index in (xrange(lcount)):
|
||||
vtn = string.split(words[index], "/")
|
||||
vi.append(string.atoi(vtn[0]))
|
||||
if len(vtn) > 1 and vtn[1]:
|
||||
ti.append(string.atoi(vtn[1]))
|
||||
if len(vtn) > 2 and vtn[2]:
|
||||
ni.append(string.atoi(vtn[2]))
|
||||
##print "Face %s: (%s, %s, %s)" % (fcount, vi, ti, ni)
|
||||
index = BlendImport.addFace(vi, ti, ni)
|
||||
##print "addFace(%s)" % index
|
||||
elif words and words[0] == "mtllib":
|
||||
# try to export materials
|
||||
directory, dummy = os.path.split(filename)
|
||||
filename = os.path.join(directory, words[1])
|
||||
try:
|
||||
file = open(filename, "r")
|
||||
except:
|
||||
print "no material file %s" % filename
|
||||
else:
|
||||
file = open(filename, "r")
|
||||
line = file.readline()
|
||||
while line:
|
||||
words = string.split(line)
|
||||
if words and words[0] == "newmtl":
|
||||
name = words[1]
|
||||
file.readline() # Ns .
|
||||
file.readline() # d .
|
||||
file.readline() # illum .
|
||||
line = file.readline()
|
||||
words = string.split(line)
|
||||
Kd = [string.atof(words[1]),
|
||||
string.atof(words[2]),
|
||||
string.atof(words[3])]
|
||||
line = file.readline()
|
||||
words = string.split(line)
|
||||
Ks = [string.atof(words[1]),
|
||||
string.atof(words[2]),
|
||||
string.atof(words[3])]
|
||||
line = file.readline()
|
||||
words = string.split(line)
|
||||
Ka = [string.atof(words[1]),
|
||||
string.atof(words[2]),
|
||||
string.atof(words[3])]
|
||||
index = BlendImport.addMaterial(name, Kd, Ks, Ka)
|
||||
line = file.readline()
|
||||
file.close()
|
||||
elif words and words[0] == "usemtl":
|
||||
name = words[1]
|
||||
BlendImport.setCurrentMaterial(name)
|
||||
elif words:
|
||||
pass
|
||||
##print "%s: %s" % (linenumber, words)
|
||||
linenumber = linenumber + 1
|
||||
file.close()
|
||||
# export to OpenInventor
|
||||
BlendImport.OpenInventor().export("test.iv",
|
||||
useNormals = 1, useMaterials = 1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 2:
|
||||
usage()
|
||||
else:
|
||||
filename = sys.argv[1]
|
||||
testObjImport(filename)
|
Reference in New Issue
Block a user