Added extra ranlib on libary files after being copied to the lib tree for OSX only.
[blender-staging.git] / intern / python / ribexport.py
1 #######################
2 # (c) Jan Walter 2000 #
3 #######################
4
5 # CVS
6 # $Author$
7 # $Date$
8 # $RCSfile$
9 # $Revision$
10
11 import Blender
12 import math
13
14 exportAnimations = 0
15
16 class RenderManExport:
17     def __init__(self, filename):
18         self.file    = open(filename, "w")
19         self.scene   = None
20         self.display = None
21
22     def export(self, scene):
23         global exportAnimations
24
25         print "exporting ..."
26         self.scene = scene
27         self.writeHeader()
28         self.display = Blender.getDisplaySettings()
29         if exportAnimations:
30             for frame in xrange(self.display.startFrame,
31                                 self.display.endFrame + 1):
32                 self.writeFrame(frame)
33         else:
34             self.writeFrame(self.display.currentFrame)
35         self.writeEnd()
36
37     def writeCamera(self):
38         camobj = self.scene.getCurrentCamera()
39         camera = Blender.getCamera(camobj.data)
40         factor = self.display.yResolution / float(self.display.xResolution)
41         self.file.write('Projection "perspective" "fov" [%s]\n' %
42                         (360.0 * math.atan(factor * 16.0 / camera.Lens) /
43                          math.pi))
44         self.file.write("Clipping %s %s\n" % (camera.ClSta, camera.ClEnd))
45         self.file.write("Transform [" +
46                         "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s]\n" %
47                         (camobj.inverseMatrix[0][0],
48                          camobj.inverseMatrix[0][1],
49                          -camobj.inverseMatrix[0][2],
50                          camobj.inverseMatrix[0][3],
51                          camobj.inverseMatrix[1][0],
52                          camobj.inverseMatrix[1][1],
53                          -camobj.inverseMatrix[1][2],
54                          camobj.inverseMatrix[1][3],
55                          camobj.inverseMatrix[2][0],
56                          camobj.inverseMatrix[2][1],
57                          -camobj.inverseMatrix[2][2],
58                          camobj.inverseMatrix[2][3],
59                          camobj.inverseMatrix[3][0],
60                          camobj.inverseMatrix[3][1],
61                          -camobj.inverseMatrix[3][2],
62                          camobj.inverseMatrix[3][3]))
63
64     def writeDisplaySettings(self, frame):
65         self.file.write("Format %s %s %s\n" % (self.display.xResolution,
66                                                self.display.yResolution,
67                                                self.display.pixelAspectRatio))
68         self.file.write('Display "%s" "file" "rgba"\n' %
69                         ("frame" + "%04d" % frame + ".tif"))
70
71     def writeEnd(self):
72         self.file.close()
73         print "... finished"
74
75     def writeFrame(self, frame):
76         print "frame:", frame
77         Blender.setCurrentFrame(frame)
78         self.file.write("FrameBegin %s\n" % (frame - self.display.startFrame))
79         self.writeDisplaySettings(frame)
80         self.writeCamera()
81         self.writeWorld()
82         self.file.write("FrameEnd\n")
83
84     def writeHeader(self):
85         self.file.write("##RenderMan RIB-Structure 1.0\n")
86         self.file.write("version 3.03\n")
87
88     def writeIdentifier(self, name):
89         self.file.write("%s\n" % ("#" * (len(name) + 4)))
90         self.file.write("# %s #\n" % name)
91         self.file.write("%s\n" % ("#" * (len(name) + 4)))
92
93     def writeLamp(self, name, num):
94         self.writeIdentifier(name)
95         lampobj = Blender.getObject(name)
96         lamp    = Blender.getLamp(lampobj.data)
97         x = lampobj.matrix[3][0] / lampobj.matrix[3][3]
98         y = lampobj.matrix[3][1] / lampobj.matrix[3][3]
99         z = lampobj.matrix[3][2] / lampobj.matrix[3][3]
100         self.file.write('LightSource "pointlight" %s ' % num +
101                         '"from" [%s %s %s] ' % (x, y, z) +
102                         '"lightcolor" [%s %s %s] ' % (lamp.R, lamp.G, lamp.B) +
103                         '"intensity" 50\n')
104
105     def writeMatrix(self, matrix):
106         self.file.write("Transform [" +
107                         "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s]\n" %
108                         (matrix[0][0], matrix[0][1],
109                          matrix[0][2], matrix[0][3],
110                          matrix[1][0], matrix[1][1],
111                          matrix[1][2], matrix[1][3],
112                          matrix[2][0], matrix[2][1],
113                          matrix[2][2], matrix[2][3],
114                          matrix[3][0], matrix[3][1],
115                          matrix[3][2], matrix[3][3]))
116
117     def writeObject(self, name):
118         if Blender.isMesh(name):
119             self.writeIdentifier(name)
120             meshobj = Blender.getObject(name)
121             mesh    = Blender.getMesh(meshobj.data)
122             if mesh.texcoords:
123                 self.file.write('Surface "paintedplastic" "texturename" ' +
124                                 '["%s.tif"]\n' % "st")
125             else:
126                 self.file.write('Surface "plastic"\n')
127             self.file.write("Color [%s %s %s]\n" % (0.8, 0.8, 0.8))
128             self.file.write("AttributeBegin\n")
129             self.writeMatrix(meshobj.matrix)
130             index = 0
131             for face in mesh.faces:
132                 if meshobj.materials and meshobj.materials[face[5]]:
133                     material = Blender.getMaterial(meshobj.materials[face[5]])
134                     self.file.write("Color [%s %s %s]\n" %
135                                     (material.R, material.G, material.B))
136                 if face[3]:
137                     # quad
138                     if face[4]: # smooth
139                         # first triangle
140                         self.file.write('Polygon "P" [ ')
141                         for i in xrange(3):
142                             self.file.write("%s %s %s " %
143                                             (mesh.vertices[face[i]][0],
144                                              mesh.vertices[face[i]][1],
145                                              mesh.vertices[face[i]][2]))
146                         self.file.write('] "N" [ ')
147                         for i in xrange(3):
148                             self.file.write("%s %s %s " %
149                                             (mesh.normals[face[i]][0],
150                                              mesh.normals[face[i]][1],
151                                              mesh.normals[face[i]][2]))
152                         if mesh.colors:
153                             self.file.write('] "Cs" [ ')
154                             for i in xrange(3):
155                                 self.file.write("%s %s %s " %
156                                                 (mesh.colors[face[i]][0],
157                                                  mesh.colors[face[i]][1],
158                                                  mesh.colors[face[i]][2]))
159                             self.file.write(']\n')
160                         if mesh.texcoords:
161                             self.file.write('] "st" [ ')
162                             for i in xrange(3):
163                                 self.file.write("%s %s " %
164                                                 (mesh.texcoords[index+i][0],
165                                                  1.0 -
166                                                  mesh.texcoords[index+i][1]))
167                             self.file.write(']\n')
168                         else:
169                             self.file.write(']\n')
170                         # second triangle
171                         self.file.write('Polygon "P" [ ')
172                         for i in [0, 2, 3]:
173                             self.file.write("%s %s %s " %
174                                             (mesh.vertices[face[i]][0],
175                                              mesh.vertices[face[i]][1],
176                                              mesh.vertices[face[i]][2]))
177                         self.file.write('] "N" [ ')
178                         for i in [0, 2, 3]:
179                             self.file.write("%s %s %s " %
180                                             (mesh.normals[face[i]][0],
181                                              mesh.normals[face[i]][1],
182                                              mesh.normals[face[i]][2]))
183                         if mesh.colors:
184                             self.file.write('] "Cs" [ ')
185                             for i in [0, 2, 3]:
186                                 self.file.write("%s %s %s " %
187                                                 (mesh.colors[face[i]][0],
188                                                  mesh.colors[face[i]][1],
189                                                  mesh.colors[face[i]][2]))
190                             self.file.write(']\n')
191                         if mesh.texcoords:
192                             self.file.write('] "st" [ ')
193                             for i in [0, 2, 3]:
194                                 self.file.write("%s %s " %
195                                                 (mesh.texcoords[index+i][0],
196                                                  1.0 -
197                                                  mesh.texcoords[index+i][1]))
198                             self.file.write(']\n')
199                         else:
200                             self.file.write(']\n')
201                     else: # not smooth
202                         # first triangle
203                         self.file.write('Polygon "P" [ ')
204                         for i in xrange(3):
205                             self.file.write("%s %s %s " %
206                                             (mesh.vertices[face[i]][0],
207                                              mesh.vertices[face[i]][1],
208                                              mesh.vertices[face[i]][2]))
209                         if mesh.colors:
210                             self.file.write('] "Cs" [ ')
211                             for i in xrange(3):
212                                 self.file.write("%s %s %s " %
213                                                 (mesh.colors[face[i]][0],
214                                                  mesh.colors[face[i]][1],
215                                                  mesh.colors[face[i]][2]))
216                             self.file.write(']\n')
217                         if mesh.texcoords:
218                             self.file.write('] "st" [ ')
219                             for i in xrange(3):
220                                 self.file.write("%s %s " %
221                                                 (mesh.texcoords[index+i][0],
222                                                  1.0 -
223                                                  mesh.texcoords[index+i][1]))
224                             self.file.write(']\n')
225                         else:
226                             self.file.write(']\n')
227                         # second triangle
228                         self.file.write('Polygon "P" [ ')
229                         for i in [0, 2, 3]:
230                             self.file.write("%s %s %s " %
231                                             (mesh.vertices[face[i]][0],
232                                              mesh.vertices[face[i]][1],
233                                              mesh.vertices[face[i]][2]))
234                         if mesh.colors:
235                             self.file.write('] "Cs" [ ')
236                             for i in [0, 2, 3]:
237                                 self.file.write("%s %s %s " %
238                                                 (mesh.colors[face[i]][0],
239                                                  mesh.colors[face[i]][1],
240                                                  mesh.colors[face[i]][2]))
241                             self.file.write(']\n')
242                         if mesh.texcoords:
243                             self.file.write('] "st" [ ')
244                             for i in [0, 2, 3]:
245                                 self.file.write("%s %s " %
246                                                 (mesh.texcoords[index+i][0],
247                                                  1.0 -
248                                                  mesh.texcoords[index+i][1]))
249                             self.file.write(']\n')
250                         else:
251                             self.file.write(']\n')
252                 else:
253                     # triangle
254                     if face[4]: # smooth
255                         self.file.write('Polygon "P" [ ')
256                         for i in xrange(3):
257                             self.file.write("%s %s %s " %
258                                             (mesh.vertices[face[i]][0],
259                                              mesh.vertices[face[i]][1],
260                                              mesh.vertices[face[i]][2]))
261                         self.file.write('] "N" [ ')
262                         for i in xrange(3):
263                             self.file.write("%s %s %s " %
264                                             (mesh.normals[face[i]][0],
265                                              mesh.normals[face[i]][1],
266                                              mesh.normals[face[i]][2]))
267                         if mesh.colors:
268                             self.file.write('] "Cs" [ ')
269                             for i in xrange(3):
270                                 self.file.write("%s %s %s " %
271                                                 (mesh.colors[face[i]][0],
272                                                  mesh.colors[face[i]][1],
273                                                  mesh.colors[face[i]][2]))
274                             self.file.write(']\n')
275                         if mesh.texcoords:
276                             self.file.write('] "st" [ ')
277                             for i in xrange(3):
278                                 self.file.write("%s %s " %
279                                                 (mesh.texcoords[index+i][0],
280                                                  1.0 -
281                                                  mesh.texcoords[index+i][1]))
282                             self.file.write(']\n')
283                         else:
284                             self.file.write(']\n')
285                     else: # not smooth
286                         self.file.write('Polygon "P" [ ')
287                         for i in xrange(3):
288                             self.file.write("%s %s %s " %
289                                             (mesh.vertices[face[i]][0],
290                                              mesh.vertices[face[i]][1],
291                                              mesh.vertices[face[i]][2]))
292                         if mesh.colors:
293                             self.file.write('] "Cs" [ ')
294                             for i in xrange(3):
295                                 self.file.write("%s %s %s " %
296                                                 (mesh.colors[face[i]][0],
297                                                  mesh.colors[face[i]][1],
298                                                  mesh.colors[face[i]][2]))
299                             self.file.write(']\n')
300                         if mesh.texcoords:
301                             self.file.write('] "st" [ ')
302                             for i in xrange(3):
303                                 self.file.write("%s %s " %
304                                                 (mesh.texcoords[index+i][0],
305                                                  1.0 -
306                                                  mesh.texcoords[index+i][1]))
307                             self.file.write(']\n')
308                         else:
309                             self.file.write(']\n')
310                 index = index + 4
311             self.file.write("AttributeEnd\n")
312         else:
313             print "Sorry can export meshes only ..."
314
315     def writeWorld(self):
316         self.file.write("WorldBegin\n")
317         self.file.write('Attribute "light" "shadows" "on"\n')
318         # first all lights
319         lamps = 0
320         for name in self.scene.objects:
321             if Blender.isLamp(name):
322                 lamps = lamps + 1
323                 self.writeLamp(name, lamps)
324         # now all objects which are not a camera or a light
325         for name in self.scene.objects:
326             if not Blender.isCamera(name) and not Blender.isLamp(name):
327                 self.writeObject(name)
328         self.file.write("WorldEnd\n")
329
330 ribexport = RenderManExport("test.rib")
331 scene = Blender.getCurrentScene()
332 ribexport.export(scene)