3966a9be88a95dfbf19ffbd2eebab35615969be2
[blender.git] / release / scripts / radiosity_import.py
1 #!BPY
2
3 """
4 Name: 'Radiosity (.radio)...'
5 Blender: 232
6 Group: 'Import'
7 Tooltip: 'Import Radiosity File Format (.radio) with vertex colors'
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 imports Radiosity files to Blender.
17
18 Usage:
19
20 Execute this script from the "File->Import" menu and choose a Radiosity file to
21 open.
22 """
23
24
25 # $Id$
26 #
27 # +---------------------------------------------------------+
28 # | Copyright (c) 2002 Anthony D'Agostino                   |
29 # | http://www.redrival.com/scorpius                        |
30 # | scorpius@netzero.com                                    |
31 # | April 11, 2002                                          |
32 # | Released under the Blender Artistic Licence (BAL)       |
33 # | Import Export Suite v0.5                                |
34 # +---------------------------------------------------------+
35 # | Read and write Radiosity File Format (*.radio)          |
36 # +---------------------------------------------------------+
37
38 import Blender, mod_meshtools
39 #import time
40
41 try:
42         import struct
43 except:
44         msg = "Error: you need a full Python install to run this script."
45         mod_meshtools.print_boxed(msg)
46         Blender.Draw.PupMenu("ERROR%t|"+msg)
47
48 # ===============================
49 # ====== Read Radio Format ======
50 # ===============================
51 def read(filename):
52         #start = time.clock()
53         file = open(filename, "rb")
54         mesh = Blender.NMesh.GetRaw()
55         #mesh.addMaterial(Blender.Material.New())
56
57         # === Object Name ===
58         namelen, = struct.unpack("<h",  file.read(2))
59         objname, = struct.unpack("<"+`namelen`+"s", file.read(namelen))
60
61         # === Vertex List ===
62         numverts, = struct.unpack("<l", file.read(4))
63         for i in range(numverts):
64                 if not i%100 and mod_meshtools.show_progress:
65                         Blender.Window.DrawProgressBar(float(i)/numverts, "Reading Verts")
66                 x, y, z = struct.unpack("<fff", file.read(12))
67                 mesh.verts.append(Blender.NMesh.Vert(x, y, z))
68
69         # === Face List ===
70         numfaces, = struct.unpack("<l", file.read(4))
71         for i in range(numfaces):
72                 if not i%100 and mod_meshtools.show_progress:
73                         Blender.Window.DrawProgressBar(float(i)/numfaces, "Reading Faces")
74
75                 face = Blender.NMesh.Face()
76                 numfaceverts, = struct.unpack("<b", file.read(1))
77
78                 for j in range(numfaceverts):
79                         index, = struct.unpack("<h", file.read(2))
80                         face.v.append(mesh.verts[index])
81
82                 for j in range(4):
83                         r, g, b, a = struct.unpack("<BBBB", file.read(4))
84                         vertexcolor = Blender.NMesh.Col(r, g, b, a)
85                         face.col.append(vertexcolor)
86
87                 if len(face.v) == 3:
88                         face.uv = [ (0,0), (0,1), (1,1) ]
89                 else:
90                         face.uv = [ (0,0), (0,1), (1,1), (1,0) ]
91
92                 face.mode = 0
93                 mesh.faces.append(face)
94
95         # ->tools.create_mesh(verts, faces, objname):
96         Blender.NMesh.PutRaw(mesh, objname)
97         object = Blender.Object.GetSelected()
98         object[0].name=objname
99         # ->tools.create_mesh(verts, faces, objname):
100
101         Blender.Window.DrawProgressBar(1.0, '')  # clear progressbar
102         file.close()
103         #end = time.clock()
104         #seconds = " in %.2f %s" % (end-start, "seconds")
105         message = "Successfully imported " + Blender.sys.basename(filename)# + seconds
106         mod_meshtools.print_boxed(message)
107
108 def fs_callback(filename):
109         read(filename)
110
111 Blender.Window.FileSelector(fs_callback, "Import Radio")