8aeb66fd7d01fa1eeffaf536ab8fc80d38858ccb
[blender.git] / release / scripts / radiosity_import.py
1 #!BPY
2
3 """
4 Name: 'Radiosity...'
5 Blender: 232
6 Group: 'Import'
7 Tooltip: 'Import Radiosity File Format (*.radio) with vertex colors'
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 # ====== Read Radio Format ======
35 # ===============================
36 def read(filename):
37         #start = time.clock()
38         file = open(filename, "rb")
39         mesh = Blender.NMesh.GetRaw()
40         #mesh.addMaterial(Blender.Material.New())
41
42         # === Object Name ===
43         namelen, = struct.unpack("<h",  file.read(2))
44         objname, = struct.unpack("<"+`namelen`+"s", file.read(namelen))
45
46         # === Vertex List ===
47         numverts, = struct.unpack("<l", file.read(4))
48         for i in range(numverts):
49                 if not i%100 and mod_meshtools.show_progress:
50                         Blender.Window.DrawProgressBar(float(i)/numverts, "Reading Verts")
51                 x, y, z = struct.unpack("<fff", file.read(12))
52                 mesh.verts.append(Blender.NMesh.Vert(x, y, z))
53
54         # === Face List ===
55         numfaces, = struct.unpack("<l", file.read(4))
56         for i in range(numfaces):
57                 if not i%100 and mod_meshtools.show_progress:
58                         Blender.Window.DrawProgressBar(float(i)/numfaces, "Reading Faces")
59
60                 face = Blender.NMesh.Face()
61                 numfaceverts, = struct.unpack("<b", file.read(1))
62
63                 for j in range(numfaceverts):
64                         index, = struct.unpack("<h", file.read(2))
65                         face.v.append(mesh.verts[index])
66
67                 for j in range(4):
68                         r, g, b, a = struct.unpack("<BBBB", file.read(4))
69                         vertexcolor = Blender.NMesh.Col(r, g, b, a)
70                         face.col.append(vertexcolor)
71
72                 if len(face.v) == 3:
73                         face.uv = [ (0,0), (0,1), (1,1) ]
74                 else:
75                         face.uv = [ (0,0), (0,1), (1,1), (1,0) ]
76
77                 face.mode = 0
78                 mesh.faces.append(face)
79
80         # ->tools.create_mesh(verts, faces, objname):
81         Blender.NMesh.PutRaw(mesh, objname)
82         object = Blender.Object.GetSelected()
83         object[0].name=objname
84         # ->tools.create_mesh(verts, faces, objname):
85
86         Blender.Window.DrawProgressBar(1.0, '')  # clear progressbar
87         file.close()
88         #end = time.clock()
89         #seconds = " in %.2f %s" % (end-start, "seconds")
90         message = "Successfully imported " + Blender.sys.basename(filename)# + seconds
91         mod_meshtools.print_boxed(message)
92
93 def fs_callback(filename):
94         read(filename)
95
96 Blender.Window.FileSelector(fs_callback, "Radio Import")