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