1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
#!BPY
"""
Name: 'VideoScape with Vertex Colors (.obj)...'
Blender: 232
Group: 'Export'
Tooltip: 'Export selected mesh to VideoScape File Format (.obj)'
"""
__author__ = "Anthony D'Agostino (Scorpius)"
__url__ = ("blender", "elysiun",
"Author's homepage, http://www.redrival.com/scorpius")
__version__ = "Part of IOSuite 0.5"
__bpydoc__ = """\
This script exports meshes (including vertex colors) to VideoScape File Format.
The VideoScape file format is a simple format that is natively supported
in Blender. I wrote this module because Blender's internal exporter
doesn't export vertex colors correctly. Check the source for a *fast* algorithm for
averaging vertex colors.
Usage:<br>
Select meshes to be exported and run this script from "File->Export" menu.
Supported:<br>
Exports meshes only. Hint: use ALT-C to convert non-mesh objects,
and CTRL-ALT-A if you have "dupliverts" objects.
Notes:<br>
Before exporting, the mesh must have vertex colors. Here's how to assign them:
1. Use radiosity!
2. Set up lights and materials, select a mesh, switch the drawing mode
to "textured," press the VKEY.
3. Press the VKEY and paint manually.
4. Use a custom script to calculate and apply simple diffuse shading and
specular highlights to the vertex colors.
5. The Videoscape format also allows vertex colors to be specified.
"""
# $Id$
#
# +---------------------------------------------------------+
# | Copyright (c) 2001 Anthony D'Agostino |
# | http://www.redrival.com/scorpius |
# | scorpius@netzero.com |
# | June 5, 2001 |
# | Released under the Blender Artistic Licence (BAL) |
# | Import Export Suite v0.5 |
# +---------------------------------------------------------+
# | Write Videoscape File Format (*.obj NOT WAVEFRONT OBJ) |
# +---------------------------------------------------------+
import Blender, mod_meshtools
#import time
# =====================================
# ====== Write VideoScape Format ======
# =====================================
def write(filename):
#start = time.clock()
file = open(filename, "wb")
objects = Blender.Object.GetSelected()
objname = objects[0].name
meshname = objects[0].data.name
mesh = Blender.NMesh.GetRaw(meshname)
obj = Blender.Object.Get(objname)
if not mod_meshtools.has_vertex_colors(mesh):
message = "Please assign vertex colors before exporting.\n"
message += objname + " object was not saved."
mod_meshtools.print_boxed(message)
return
vcols = average_vertexcolors(mesh)
# === Write Videoscape Header ===
file.write("GOUR\n")
file.write("%d\n" % len(mesh.verts))
# === Write Vertex List & Vertex Colors ===
for i in range(len(mesh.verts)):
if not i%100 and mod_meshtools.show_progress:
Blender.Window.DrawProgressBar(float(i)/len(mesh.verts), "Writing Verts")
file.write("% f % f % f 0x" % tuple(mesh.verts[i].co))
for j in range(len(vcols[i])):
file.write("%02X" % vcols[i][j])
file.write("\n")
# === Write Face List ===
for i in range(len(mesh.faces)):
if not i%100 and mod_meshtools.show_progress:
Blender.Window.DrawProgressBar(float(i)/len(mesh.faces), "Writing Faces")
file.write("%d " % len(mesh.faces[i].v)) # numfaceverts
for j in range(len(mesh.faces[i].v)):
file.write("%d " % mesh.faces[i].v[j].index)
file.write("\n")
Blender.Window.DrawProgressBar(1.0, '') # clear progressbar
file.close()
#end = time.clock()
#seconds = " in %.2f %s" % (end-start, "seconds")
message = "Successfully exported " + Blender.sys.basename(filename)# + seconds
mod_meshtools.print_boxed(message)
# ===========================================
# === Vector Operations for Vertex Colors ===
# ===========================================
vcolor_add = lambda u, v: [u[0]+v[0], u[1]+v[1], u[2]+v[2], u[3]+v[3]]
vcolor_div = lambda u, s: [u[0]/s, u[1]/s, u[2]/s, u[3]/s]
# ========================================
# === Average All Vertex Colors (Fast) ===
# ========================================
def average_vertexcolors(mesh, debug=0):
vertexcolors = {}
for i in range(len(mesh.faces)): # get all vcolors that share this vertex
if not i%100 and mod_meshtools.show_progress: Blender.Window.DrawProgressBar(float(i)/len(mesh.verts), "Finding Shared VColors")
for j in range(len(mesh.faces[i].v)):
index = mesh.faces[i].v[j].index
color = mesh.faces[i].col[j]
r,g,b,a = color.r, color.g, color.b, color.a
vertexcolors.setdefault(index, []).append([r,g,b,a])
if debug: print 'before'; vcprint(vertexcolors)
for i in range(len(vertexcolors)): # average them
if not i%100 and mod_meshtools.show_progress: Blender.Window.DrawProgressBar(float(i)/len(mesh.verts), "Averaging Vertex Colors")
vcolor = [0,0,0,0] # rgba
for j in range(len(vertexcolors[i])):
vcolor = vcolor_add(vcolor, vertexcolors[i][j])
shared = len(vertexcolors[i])
vertexcolors[i] = vcolor_div(vcolor, shared)
if debug: print 'after'; vcprint(vertexcolors)
return vertexcolors
# ========================================
# === Average all Vertex Colors Slow 1 ===
# ========================================
def average_vertexcolors_slow_1(mesh, debug=0):
vertexcolors = []
i = 0
for vertex in mesh.verts:
if not i%100 and mod_meshtools.show_progress: Blender.Window.DrawProgressBar(float(i)/len(mesh.verts), "Averaging Vertex Colors")
i += 1
vcolor = [0,0,0,0] # rgba
shared = 0
for face in mesh.faces:
if vertex in face.v:
index = face.v.index(vertex)
color = face.col[index]
r,g,b,a = color.r, color.g, color.b, color.a
vcolor = vcolor_add(vcolor, [r,g,b,a])
shared += 1
if not shared: print "Error, vertex %d is not shared." % i; shared += 1
vertexcolors.append(vcolor_div(vcolor, shared))
if debug: print 'after'; vcprint(vertexcolors)
return vertexcolors
# ========================================
# === Average all Vertex Colors Slow 2 ===
# ========================================
def average_vertexcolors_slow_2(mesh, debug=0):
vertexcolors = []
for i in range(len(mesh.verts)):
if not i%100 and mod_meshtools.show_progress: Blender.Window.DrawProgressBar(float(i)/len(mesh.verts), "Averaging Vertex Colors")
vcolor = [0,0,0,0] # rgba
shared = 0
for j in range(len(mesh.faces)):
if mesh.verts[i] in mesh.faces[j].v:
index = mesh.faces[j].v.index(mesh.verts[i])
color = mesh.faces[j].col[index]
r,g,b,a = color.r, color.g, color.b, color.a
vcolor = vcolor_add(vcolor, [r,g,b,a])
shared += 1
vertexcolors.append(vcolor_div(vcolor, shared))
if debug: print 'after'; vcprint(vertexcolors)
return vertexcolors
# ========================================
# === Average all Vertex Colors Slow 3 ===
# ========================================
def average_vertexcolors_slow_3(mesh, debug=0):
vertexcolors = []
for i in range(len(mesh.verts)):
if not i%100 and mod_meshtools.show_progress: Blender.Window.DrawProgressBar(float(i)/len(mesh.verts), "Averaging Vertex Colors")
vcolor = [0,0,0,0] # rgba
shared = 0
for j in range(len(mesh.faces)):
if len(mesh.faces[j].v) == 4:
v1,v2,v3,v4 = mesh.faces[j].v
faceverts = v1.index, v2.index, v3.index, v4.index
else:
v1,v2,v3 = mesh.faces[j].v
faceverts = v1.index, v2.index, v3.index
if i in faceverts:
index = mesh.faces[j].v.index(mesh.verts[i])
color = mesh.faces[j].col[index]
r,g,b,a = color.r, color.g, color.b, color.a
vcolor = vcolor_add(vcolor, [r,g,b,a])
shared += 1
vertexcolors.append(vcolor_div(vcolor, shared))
if debug: print 'after'; vcprint(vertexcolors)
return vertexcolors
def fs_callback(filename):
if filename.find('.obj', -4) <= 0: filename += '.VIDEOSCAPE.obj'
write(filename)
Blender.Window.FileSelector(fs_callback, "Export VideoScape")
# filename = "VIDEOSCAPE_" + objname + ".obj"
# filename = 'nul'
# file = open(filename, "wb")
# debug = 0
# time_functions = 1
# time_loop = 0
#
# if time_functions:
# funcs = [ average_vertexcolors,
# average_vertexcolors_slow_1,
# average_vertexcolors_slow_2,
# average_vertexcolors_slow_3 ]
#
# print
# for func in funcs:
# start = time.clock()
# vcols = func(mesh, debug)
# end = time.clock()
# seconds = "in %.2f %s" % (end-start, "seconds")
# print func.__name__, "finished in", seconds
#
# elif time_loop:
# total = 0
# loops = 6
# for i in range(loops):
# start = time.clock()
# vcols = average_vertexcolors(mesh, debug)
# end = time.clock()
# total += (end-start)
# print "Total: %5.2f Avg: %.2f " % (total, total/loops)
# else:
# start = time.clock()
# vcols = average_vertexcolors(mesh, debug)
# # =====================================
# # === Print Vertex Colors for Debug ===
# # =====================================
# def vcprint(data):
# print type(data)
# for i in range(len(data)):
# print "%2d" % i,
# for j in range(len(data[i])):
# try:
# print "[%3d %3d %3d %3d]" % tuple(data[i][j]), # before
# except:
# print "[%3d]" % data[i][j], # after
# print
# print
#
|