85b8ae8e59861a7a95330e3390a404edd61aac83
[blender.git] / release / scripts / radiosity_export.py
1 #!BPY
2
3 """
4 Name: 'Radiosity...'
5 Blender: 232
6 Group: 'Export'
7 Tooltip: 'Export selected mesh (with vertex colors) to Radiosity File Format (*.radio)'
8 """
9
10 # $Id$
11 #
12 # +---------------------------------------------------------+
13 # | Copyright (c) 2002 Anthony D'Agostino                   |
14 # | http://www.redrival.com/scorpius                        |
15 # | scorpius@netzero.com                                    |
16 # | April 11, 2002                                          |
17 # | Released under the Blender Artistic Licence (BAL)       |
18 # | Import Export Suite v0.5                                |
19 # +---------------------------------------------------------+
20 # | Read and write Radiosity File Format (*.radio)          |
21 # +---------------------------------------------------------+
22
23 import Blender, mod_meshtools
24 #import time
25
26 try:
27         import struct
28 except:
29         msg = "Error: you need a full Python install to run this script."
30         mod_meshtools.print_boxed(msg)
31         Blender.Draw.PupMenu("ERROR%t|"+msg)
32
33 # ================================
34 # ====== Write Radio Format ======
35 # ================================
36 def write(filename):
37         #start = time.clock()
38         file = open(filename, "wb")
39
40         objects = Blender.Object.GetSelected()
41         objname = objects[0].name
42         meshname = objects[0].data.name
43         mesh = Blender.NMesh.GetRaw(meshname)
44         obj = Blender.Object.Get(objname)
45
46         if not mod_meshtools.has_vertex_colors(mesh):
47                 message = "Please assign vertex colors before exporting. \n"
48                 message += objname + " object was not saved."
49                 mod_meshtools.print_boxed(message)
50                 Blender.Draw.PupMenu("ERROR%t|"+message)
51                 return
52
53         # === Object Name ===
54         file.write(struct.pack("<h", len(objname)))
55         file.write(struct.pack("<"+`len(objname)`+"s", objname))
56
57         # === Vertex List ===
58         file.write(struct.pack("<l", len(mesh.verts)))
59         for i in range(len(mesh.verts)):
60                 if not i%100 and mod_meshtools.show_progress:
61                         Blender.Window.DrawProgressBar(float(i)/len(mesh.verts), "Writing Verts")
62
63                 x, y, z = mesh.verts[i].co
64                 file.write(struct.pack("<fff", x, y, z))
65
66         # === Face List ===
67         file.write(struct.pack("<l", len(mesh.faces)))
68         for i in range(len(mesh.faces)):
69                 if not i%100 and mod_meshtools.show_progress:
70                         Blender.Window.DrawProgressBar(float(i)/len(mesh.faces), "Writing Faces")
71
72                 file.write(struct.pack("<b", len(mesh.faces[i].v)))
73                 for j in range(len(mesh.faces[i].v)):
74                         file.write(struct.pack("<h", mesh.faces[i].v[j].index))
75
76                 for j in range(4): # .col always has a length of 4
77                         file.write(struct.pack("<BBBB", mesh.faces[i].col[j].r,
78                                                                                         mesh.faces[i].col[j].g,
79                                                                                         mesh.faces[i].col[j].b,
80                                                                                         mesh.faces[i].col[j].a))
81
82         Blender.Window.DrawProgressBar(1.0, '')  # clear progressbar
83         file.close()
84         #end = time.clock()
85         #seconds = " in %.2f %s" % (end-start, "seconds")
86         message = "Successfully exported " + Blender.sys.basename(filename)# + seconds
87         mod_meshtools.print_boxed(message)
88
89 def fs_callback(filename):
90         if filename.find('.radio', -6) <= 0: filename += '.radio'
91         write(filename)
92
93 Blender.Window.FileSelector(fs_callback, "Radio Export")