From c8bbe3cc89d2838195418695331e4995f2d04ccf Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 16 May 2011 16:03:11 +0000 Subject: minor speedup, cleanup to x3d export --- io_scene_x3d/export_x3d.py | 178 ++++++++++++++++++++------------------------- 1 file changed, 77 insertions(+), 101 deletions(-) (limited to 'io_scene_x3d') diff --git a/io_scene_x3d/export_x3d.py b/io_scene_x3d/export_x3d.py index 4fa73b3b..111814e3 100644 --- a/io_scene_x3d/export_x3d.py +++ b/io_scene_x3d/export_x3d.py @@ -42,8 +42,8 @@ import mathutils from bpy_extras.io_utils import create_derived_objects, free_derived_objects -def round_color(col, cp): - return tuple([round(max(min(c, 1.0), 0.0), cp) for c in col]) +def clamp_color(col): + return tuple([max(min(c, 1.0), 0.0) for c in col]) def matrix_direction(mtx): @@ -63,11 +63,6 @@ class x3d_class: self.billnode = 0 self.halonode = 0 self.collnode = 0 - self.verbose = 2 # level of verbosity in console 0-none, 1-some, 2-most - self.cp = 3 # decimals for material color values 0.000 - 1.000 - self.vp = 3 # decimals for vertex coordinate values 0.000 - n.000 - self.tp = 3 # decimals for texture coordinate values 0.000 - 1.000 - self.it = 3 self.global_matrix = mathutils.Matrix.Rotation(-(math.pi / 2.0), 4, 'X') @@ -129,9 +124,7 @@ class x3d_class: self.file.write("\n") self.file.write("\n") self.file.write("\t\n" % bfile) - # self.file.write("\t\n" % sys.basename(bfile)) self.file.write("\t\n" % bpy.app.version_string) - # self.file.write("\t\n" % Blender.Get('version')) self.file.write("\t\n") self.file.write("\n") self.file.write("\n") @@ -161,8 +154,8 @@ class x3d_class: def writeViewpoint(self, ob, mat, scene): loc, quat, scale = mat.decompose() - self.file.write("\n\n" % round(mparam.depth, self.cp)) + self.file.write("color=\"%.3f %.3f %.3f\" " % clamp_color(world.horizon_color)) + self.file.write("visibilityRange=\"%.3f\" />\n\n" % mparam.depth) else: return @@ -213,7 +206,7 @@ class x3d_class: self.file.write("radius=\"%.4f\" " % radius) self.file.write("ambientIntensity=\"%.4f\" " % amb_intensity) self.file.write("intensity=\"%.4f\" " % intensity) - self.file.write("color=\"%.4f %.4f %.4f\" " % round_color(lamp.color, 4)) + self.file.write("color=\"%.4f %.4f %.4f\" " % clamp_color(lamp.color)) self.file.write("beamWidth=\"%.4f\" " % beamWidth) self.file.write("cutOffAngle=\"%.4f\" " % cutOffAngle) self.file.write("direction=\"%.4f %.4f %.4f\" " % (dx, dy, dz)) @@ -233,7 +226,7 @@ class x3d_class: dx, dy, dz = matrix_direction(mtx) self.file.write("\n\n" % (dx, dy, dz)) @@ -242,7 +235,7 @@ class x3d_class: if world: ambi = world.ambient_color # ambi = world.amb - amb_intensity = ((float(ambi[0] + ambi[1] + ambi[2])) / 3) / 2.5 + amb_intensity = ((float(ambi[0] + ambi[1] + ambi[2])) / 3.0) / 2.5 else: ambi = 0.0 amb_intensity = 0.0 @@ -252,7 +245,7 @@ class x3d_class: self.file.write("\n", 1) self.halonode = 1 - elif 'BILLBOARD' in mode and self.billnode == 0: - # elif mode & Mesh.FaceModes.BILLBOARD and self.billnode == 0: + elif texface_use_billboard and self.billnode == 0: self.write_indented("\n", 1) self.billnode = 1 - elif 'COLLISION' not in mode and self.collnode == 0: - # elif not mode & Mesh.FaceModes.DYNAMIC and self.collnode == 0: + elif texface_use_collision and self.collnode == 0: self.write_indented("\n", 1) self.collnode = 1 + del texface_use_halo + del texface_use_billboard + del texface_use_collision + # del texface_use_object_color + loc, quat, sca = mtx.decompose() self.write_indented("" % (round(transp, self.cp))) + self.file.write("diffuseColor=\"%.3f %.3f %.3f\" " % clamp_color(diffuseColor)) + self.file.write("specularColor=\"%.3f %.3f %.3f\" " % clamp_color(specColor)) + self.file.write("emissiveColor=\"%.3f %.3f %.3f\" \n" % clamp_color(emitColor)) + self.write_indented("ambientIntensity=\"%.3f\" " % ambient) + self.file.write("shininess=\"%.3f\" " % shininess) + self.file.write("transparency=\"%s\" />" % transp) self.write_indented("\n", -1) def writeImageTexture(self, image): @@ -607,7 +595,7 @@ class x3d_class: self.file.write("url='%s' />" % " ".join(["\"%s\"" % f.replace("\\", "/") for f in images])) self.write_indented("\n", -1) - def writeBackground(self, world, alltextures): + def writeBackground(self, world): if world: worldname = world.name else: @@ -615,47 +603,43 @@ class x3d_class: blending = world.use_sky_blend, world.use_sky_paper, world.use_sky_real - grd_triple = round_color(world.horizon_color, self.cp) - sky_triple = round_color(world.zenith_color, self.cp) - mix_triple = round_color(((grd_triple[i] + sky_triple[i]) / 2.0 for i in range(3)), self.cp) + grd_triple = clamp_color(world.horizon_color) + sky_triple = clamp_color(world.zenith_color) + mix_triple = clamp_color((grd_triple[i] + sky_triple[i]) / 2.0 for i in range(3)) self.file.write("\n\n") ########################################################## # export routine ########################################################## - def export(self, scene, world, alltextures, - use_apply_modifiers=False, - use_selection=True, - EXPORT_TRI=False, - ): + def export(self, scene, + use_apply_modifiers=False, + use_selection=True, + EXPORT_TRI=False, + ): + + world = scene.world # tag un-exported IDs bpy.data.meshes.tag(False) @@ -701,7 +687,7 @@ class x3d_class: self.writeHeader() # self.writeScript() self.writeNavigationInfo(scene) - self.writeBackground(world, alltextures) + self.writeBackground(world) self.writeFog(world) self.proto = 0 @@ -787,10 +773,10 @@ class x3d_class: newName = '%s%s' % (prefix, newName) if newName[0].isdigit(): - newName = "%s%s" % ('_', newName) + newName = "%s%s" % ("_", newName) for bad in [' ', '"', '#', "'", ', ', '.', '[', '\\', ']', '{', '}']: - newName = newName.replace(bad, '_') + newName = newName.replace(bad, "_") return newName def faceToString(self, face): @@ -821,10 +807,6 @@ class x3d_class: print("Debug: mesh.faces=%d" % len(mesh.faces)) print("Debug: mesh.materials=%d" % len(mesh.materials)) - # s="%s %s %s" % ( - # round(c.r/255.0,self.cp), - # round(c.g/255.0,self.cp), - # round(c.b/255.0,self.cp)) return s # For writing well formed VRML code @@ -862,14 +844,8 @@ def save(operator, context, filepath="", if bpy.ops.object.mode_set.poll(): bpy.ops.object.mode_set(mode='OBJECT') - # XXX these are global textures while .Get() returned only scene's? - alltextures = bpy.data.textures - # alltextures = Blender.Texture.Get() - wrlexport = x3d_class(filepath) wrlexport.export(scene, - world, - alltextures, use_apply_modifiers=use_apply_modifiers, use_selection=use_selection, EXPORT_TRI=use_triangulate, -- cgit v1.2.3