Added group defaultconfig to group mainmodule.
[blender-staging.git] / intern / python / ivexport.py
1 #######################
2 # (c) Jan Walter 2000 #
3 #######################
4
5 # CVS
6 # $Author$
7 # $Date$
8 # $RCSfile$
9 # $Revision$
10
11 import Blender
12
13 class InventorExport:
14     def __init__(self, filename):
15         self.file = open(filename, "w")
16
17     def beginObject(self, object):
18         self.file.write("  Separator {\n")
19
20     def endObject(self, object):
21         self.file.write("  }\n")
22
23     def export(self, scene):
24         print "exporting ..."
25         self.writeHeader()
26         for name in scene.objects:
27             object = Blender.getObject(name)
28             self.beginObject(object)
29             self.writeObject(object)
30             self.endObject(object)
31         self.writeEnd()
32
33     def writeEnd(self):
34         self.file.write("}\n")
35         self.file.close()
36         print "... finished"
37
38     def writeFaces(self, faces, smooth, colors, materials, texture):
39         self.file.write("    IndexedFaceSet {\n")
40         # colors
41         if colors:
42             self.file.write("      vertexProperty VertexProperty {\n")
43             self.file.write("        orderedRGBA [\n")
44             for color in colors[:-1]:
45                 r = hex(int(color[0] * 255))
46                 if len(r) == 3:
47                     r = r + "0"
48                 g = hex(int(color[1] * 255))
49                 if len(g) == 3:
50                     g = g + "0"
51                 b = hex(int(color[2] * 255))
52                 if len(b) == 3:
53                     b = b + "0"
54                 colstr = r + g[2:] + b[2:]
55                 self.file.write("                      %sff,\n" % colstr)
56             color = colors[-1]
57             r = hex(int(color[0] * 255))
58             if len(r) == 3:
59                 r = r + "0"
60             g = hex(int(color[1] * 255))
61             if len(g) == 3:
62                 g = g + "0"
63             b = hex(int(color[2] * 255))
64             if len(b) == 3:
65                 b = b + "0"
66             colstr = r + g[2:] + b[2:]
67             self.file.write("                      %sff\n" % colstr)
68             self.file.write("                    ]\n")
69             self.file.write("        materialBinding PER_VERTEX_INDEXED\n")
70             self.file.write("      }\n")
71         # coordinates
72         self.file.write("      coordIndex [\n")
73         for face in faces[:-1]:
74             if face[4] != smooth:
75                 pass
76             elif face[2] == 0 and face[3] == 0:
77                 print "can't export lines at the moment ..."
78             elif face[3] == 0:
79                 self.file.write("                    %s, %s, %s, -1,\n" %
80                                 (face[0], face[1], face[2]))
81             else:
82                 self.file.write("                    %s, %s, %s, %s, -1,\n"%
83                                 (face[0], face[1], face[2], face[3]))
84         face = faces[-1]
85         if face[4] != smooth:
86             pass
87         elif face[2] == 0 and face[3] == 0:
88             print "can't export lines at the moment ..."
89         elif face[3] == 0:
90             self.file.write("                    %s, %s, %s, -1,\n" %
91                             (face[0], face[1], face[2]))
92         else:
93             self.file.write("                    %s, %s, %s, %s, -1,\n"%
94                             (face[0], face[1], face[2], face[3]))
95         self.file.write("                 ]\n")
96         # materials
97         if not colors and materials:
98             self.file.write("      materialIndex [\n")
99             for face in faces[:-1]:
100                 if face[4] != smooth:
101                     pass
102                 else:
103                     self.file.write("                      %s,\n" % face[5])
104             face = faces[-1]
105             if face[4] != smooth:
106                 pass
107             else:
108                 self.file.write("                      %s\n" % face[5])
109             self.file.write("                    ]\n")
110         # texture coordinates
111         if texture:
112             self.file.write("      textureCoordIndex [\n")
113             index = 0
114             for face in faces:
115                 if face[3] == 0:
116                     self.file.write("                          " +
117                                     "%s, %s, %s, -1,\n" %
118                                     (index, index+1, index+2))
119                 else:
120                     self.file.write("                          " +
121                                     "%s, %s, %s, %s, -1,\n" %
122                                     (index, index+1, index+2, index+3))
123                 index = index + 4
124             self.file.write("                        ]\n")
125         self.file.write("    }\n")
126
127     def writeHeader(self):
128         self.file.write("#Inventor V2.1 ascii\n\n")
129         self.file.write("Separator {\n")
130         self.file.write("  ShapeHints {\n")
131         self.file.write("    vertexOrdering COUNTERCLOCKWISE\n")
132         self.file.write("  }\n")
133
134     def writeMaterials(self, materials):
135         if materials:
136             self.file.write("    Material {\n")
137             self.file.write("               diffuseColor [\n")
138             for name in materials[:-1]:
139                 material = Blender.getMaterial(name)
140                 self.file.write("                              %s %s %s,\n" %
141                                 (material.R, material.G, material.B))
142             name = materials[-1]
143             material = Blender.getMaterial(name)
144             self.file.write("                              %s %s %s\n" %
145                             (material.R, material.G, material.B))
146             self.file.write("                            ]\n")
147             self.file.write("             }\n")
148             self.file.write("    MaterialBinding {\n")
149             self.file.write("                      value PER_FACE_INDEXED\n")
150             self.file.write("                    }\n")
151
152     def writeMatrix(self, matrix):
153         self.file.write("    MatrixTransform {\n")
154         self.file.write("      matrix %s %s %s %s\n" %
155                         (matrix[0][0], matrix[0][1],
156                          matrix[0][2], matrix[0][3]))
157         self.file.write("             %s %s %s %s\n" %
158                         (matrix[1][0], matrix[1][1],
159                          matrix[1][2], matrix[1][3]))
160         self.file.write("             %s %s %s %s\n" %
161                         (matrix[2][0], matrix[2][1],
162                          matrix[2][2], matrix[2][3]))
163         self.file.write("             %s %s %s %s\n" %
164                         (matrix[3][0], matrix[3][1],
165                          matrix[3][2], matrix[3][3]))
166         self.file.write("    }\n")
167
168     def writeNormals(self, normals):
169         self.file.write("    Normal {\n")
170         self.file.write("      vector [\n")
171         for normal in normals[:-1]:
172             self.file.write("               %s %s %s,\n" %
173                             (normal[0], normal[1], normal[2]))
174         normal = normals[-1]
175         self.file.write("               %s %s %s\n" %
176                         (normal[0], normal[1], normal[2]))
177         self.file.write("             ]\n")
178         self.file.write("    }\n")
179
180     def writeObject(self, object):
181         if object.type == "Mesh":
182             mesh = Blender.getMesh(object.data)
183             self.writeMatrix(object.matrix)
184             self.writeMaterials(object.materials)
185             self.writeTexture(mesh.texture, mesh.texcoords)
186             self.writeVertices(mesh.vertices)
187             self.writeFaces(mesh.faces, 0, mesh.colors, object.materials,
188                             mesh.texture)
189             self.writeNormals(mesh.normals)
190             self.writeFaces(mesh.faces, 1, mesh.colors, object.materials,
191                             mesh.texture)
192         else:
193             print "can't export %s at the moment ..." % object.type
194
195     def writeTexture(self, texture, texcoords):
196         if texture:
197             self.file.write("    Texture2 {\n")
198             self.file.write('      filename "%s"\n' % texture)
199             self.file.write("    }\n")
200             self.file.write("    TextureCoordinate2 {\n")
201             self.file.write("      point [\n")
202             for texcoord in texcoords:
203                 self.file.write("              %s %s,\n" %
204                                 (texcoord[0], texcoord[1]))
205             self.file.write("            ]\n")
206             self.file.write("    }\n")
207             self.file.write("    TextureCoordinateBinding {\n")
208             self.file.write("      value PER_VERTEX_INDEXED\n")
209             self.file.write("    }\n")
210
211     def writeVertices(self, vertices):
212         self.file.write("    Coordinate3 {\n")
213         self.file.write("      point [\n")
214         for vertex in vertices[:-1]:
215             self.file.write("              %s %s %s,\n" %
216                             (vertex[0], vertex[1], vertex[2]))
217         vertex = vertices[-1]
218         self.file.write("              %s %s %s\n" %
219                         (vertex[0], vertex[1], vertex[2]))
220         self.file.write("            ]\n")
221         self.file.write("    }\n")
222
223 ivexport = InventorExport("test.iv")
224 scene = Blender.getCurrentScene()
225 ivexport.export(scene)