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