Initial revision
[blender.git] / intern / python / objexport / objimport.py
1 #! /usr/bin/env python
2
3 #######################
4 # (c) Jan Walter 2000 #
5 #######################
6
7 # CVS
8 # $Author$
9 # $Date$
10 # $RCSfile$
11 # $Revision$
12
13 import BlendImport
14 import os
15 import sys
16 import string
17
18 def usage():
19     print "usage: python objimport.py objfile"
20
21 def testObjImport(filename):
22     print filename
23     vcount = 0
24     vncount = 0
25     fcount = 0
26     file = open(filename, "r")
27     lines = file.readlines()
28     linenumber = 1
29     for line in lines:
30         words = string.split(line)
31         if words and words[0] == "#":
32             pass # ignore comments
33         elif words and words[0] == "v":
34             vcount = vcount + 1
35             x = string.atof(words[1])
36             y = string.atof(words[2])
37             z = string.atof(words[3])
38             ##print "Vertex %s: (%s, %s, %s)" % (vcount, x, y, z)
39             index = BlendImport.addVertex(x, y, z)
40             ##print "addVertex(%s)" % index
41         elif words and words[0] == "vn":
42             vncount = vncount + 1
43             i = string.atof(words[1])
44             j = string.atof(words[2])
45             k = string.atof(words[3])
46             ##print "VertexNormal %s: (%s, %s, %s)" % (vncount, i, j, k)
47             index = BlendImport.addVertexNormal(i, j, k)
48             ##print "addVertexNormal(%s)" % index
49         elif words and words[0] == "f":
50             fcount = fcount + 1
51             vi = [] # vertex  indices
52             ti = [] # texture indices
53             ni = [] # normal  indices
54             words = words[1:]
55             lcount = len(words)
56             for index in (xrange(lcount)):
57                 vtn = string.split(words[index], "/")
58                 vi.append(string.atoi(vtn[0]))
59                 if len(vtn) > 1 and vtn[1]:
60                     ti.append(string.atoi(vtn[1]))
61                 if len(vtn) > 2 and vtn[2]:
62                     ni.append(string.atoi(vtn[2]))
63             ##print "Face %s: (%s, %s, %s)" % (fcount, vi, ti, ni)
64             index = BlendImport.addFace(vi, ti, ni)
65             ##print "addFace(%s)" % index
66         elif words and words[0] == "mtllib":
67             # try to export materials
68             directory, dummy = os.path.split(filename)
69             filename = os.path.join(directory, words[1])
70             try:
71                 file = open(filename, "r")
72             except:
73                 print "no material file %s" % filename
74             else:
75                 file = open(filename, "r")
76                 line = file.readline()
77                 while line:
78                     words = string.split(line)
79                     if words and words[0] == "newmtl":
80                         name = words[1]
81                         file.readline() # Ns .
82                         file.readline() # d .
83                         file.readline() # illum .
84                         line = file.readline()
85                         words = string.split(line)
86                         Kd = [string.atof(words[1]),
87                               string.atof(words[2]),
88                               string.atof(words[3])]
89                         line = file.readline()
90                         words = string.split(line)
91                         Ks = [string.atof(words[1]),
92                               string.atof(words[2]),
93                               string.atof(words[3])]
94                         line = file.readline()
95                         words = string.split(line)
96                         Ka = [string.atof(words[1]),
97                               string.atof(words[2]),
98                               string.atof(words[3])]
99                         index = BlendImport.addMaterial(name, Kd, Ks, Ka)
100                     line = file.readline()
101                 file.close()
102         elif words and words[0] == "usemtl":
103             name = words[1]
104             BlendImport.setCurrentMaterial(name)
105         elif words:
106             pass
107             ##print "%s: %s" % (linenumber, words)
108         linenumber = linenumber + 1
109     file.close()
110     # export to OpenInventor
111     BlendImport.OpenInventor().export("test.iv",
112                                       useNormals = 1, useMaterials = 1)
113
114 if __name__ == "__main__":
115     if len(sys.argv) != 2:
116         usage()
117     else:
118         filename = sys.argv[1]
119         testObjImport(filename)