[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [1937] trunk/py/scripts/addons/ io_scene_x3d/export_x3d.py: minor speedup, cleanup to x3d export

Campbell Barton ideasman42 at gmail.com
Mon May 16 18:03:12 CEST 2011


Revision: 1937
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=1937
Author:   campbellbarton
Date:     2011-05-16 16:03:11 +0000 (Mon, 16 May 2011)
Log Message:
-----------
minor speedup, cleanup to x3d export

Modified Paths:
--------------
    trunk/py/scripts/addons/io_scene_x3d/export_x3d.py

Modified: trunk/py/scripts/addons/io_scene_x3d/export_x3d.py
===================================================================
--- trunk/py/scripts/addons/io_scene_x3d/export_x3d.py	2011-05-16 13:22:33 UTC (rev 1936)
+++ trunk/py/scripts/addons/io_scene_x3d/export_x3d.py	2011-05-16 16:03:11 UTC (rev 1937)
@@ -42,8 +42,8 @@
 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 @@
         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 @@
         self.file.write("<X3D version=\"3.0\" profile=\"Immersive\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema-instance\" xsd:noNamespaceSchemaLocation=\"http://www.web3d.org/specifications/x3d-3.0.xsd\">\n")
         self.file.write("<head>\n")
         self.file.write("\t<meta name=\"filename\" content=\"%s\" />\n" % bfile)
-        # self.file.write("\t<meta name=\"filename\" content=\"%s\" />\n" % sys.basename(bfile))
         self.file.write("\t<meta name=\"generator\" content=\"Blender %s\" />\n" % bpy.app.version_string)
-        # self.file.write("\t<meta name=\"generator\" content=\"Blender %s\" />\n" % Blender.Get('version'))
         self.file.write("\t<meta name=\"translator\" content=\"X3D exporter v1.55 (2006/01/17)\" />\n")
         self.file.write("</head>\n")
         self.file.write("<Scene>\n")
@@ -161,8 +154,8 @@
 
     def writeViewpoint(self, ob, mat, scene):
         loc, quat, scale = mat.decompose()
-        self.file.write("<Viewpoint DEF=\"%s\" " % (self.cleanStr(ob.name)))
-        self.file.write("description=\"%s\" " % (ob.name))
+        self.file.write("<Viewpoint DEF=\"%s\" " % self.cleanStr(ob.name))
+        self.file.write("description=\"%s\" " % ob.name)
         self.file.write("centerOfRotation=\"0 0 0\" ")
         self.file.write("position=\"%3.2f %3.2f %3.2f\" " % loc[:])
         self.file.write("orientation=\"%3.2f %3.2f %3.2f %3.2f\" " % (quat.axis[:] + (quat.angle, )))
@@ -180,8 +173,8 @@
             mtype = 1 if mtype == 'LINEAR' else 2
         # if (mtype == 1 or mtype == 2):
             self.file.write("<Fog fogType=\"%s\" " % self.namesFog[mtype])
-            self.file.write("color=\"%s %s %s\" " % round_color(world.horizon_color, self.cp))
-            self.file.write("visibilityRange=\"%s\" />\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 @@
         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 @@
         dx, dy, dz = matrix_direction(mtx)
         self.file.write("<DirectionalLight DEF=\"%s\" " % safeName)
         self.file.write("ambientIntensity=\"%.4f\" " % amb_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("intensity=\"%.4f\" " % intensity)
         self.file.write("direction=\"%.4f %.4f %.4f\" />\n\n" % (dx, dy, dz))
 
@@ -242,7 +235,7 @@
         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 @@
 
         self.file.write("<PointLight DEF=\"%s\" " % safeName)
         self.file.write("ambientIntensity=\"%.4f\" " % amb_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("intensity=\"%.4f\" " % intensity)
         self.file.write("radius=\"%.4f\" " % lamp.distance)
@@ -285,35 +278,30 @@
         if not mesh.faces:
             return
 
-        mode = []
-        # mode = 0
-        if mesh.uv_textures.active:
-        # if mesh.faceUV:
-            for face in mesh.uv_textures.active.data:
-            # for face in mesh.faces:
-                if face.use_halo and 'HALO' not in mode:
-                    mode += ['HALO']
-                if face.use_billboard and 'BILLBOARD' not in mode:
-                    mode += ['BILLBOARD']
-                if face.use_object_color and 'OBJECT_COLOR' not in mode:
-                    mode += ['OBJECT_COLOR']
-                if face.use_collision and 'COLLISION' not in mode:
-                    mode += ['COLLISION']
-                # mode |= face.mode
+        texface_use_halo = texface_use_billboard = texface_use_collision = 0
 
-        if 'HALO' in mode and self.halonode == 0:
-        # if mode & Mesh.FaceModes.HALO and self.halonode == 0:
+        if mesh.uv_textures.active:  # if mesh.faceUV:
+            for face in mesh.uv_textures.active.data:  # for face in mesh.faces:
+                texface_use_halo |= face.use_halo
+                texface_use_billboard |= face.use_billboard
+                texface_use_collision |= face.use_collision
+                # texface_use_object_color |= face.use_object_color
+
+        if texface_use_halo and self.halonode == 0:
             self.write_indented("<Billboard axisOfRotation=\"0 0 0\">\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("<Billboard axisOfRotation=\"0 1 0\">\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("<Collision enabled=\"false\">\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("<Transform DEF=\"%s\" " % shape_name_x3d, 1)
@@ -574,12 +562,12 @@
                 specColor = emitColor = diffuseColor
 
             self.write_indented("<Material DEF=\"MA_%s\" " % matName, 1)
-            self.file.write("diffuseColor=\"%s %s %s\" " % round_color(diffuseColor, self.cp))
-            self.file.write("specularColor=\"%s %s %s\" " % round_color(specColor, self.cp))
-            self.file.write("emissiveColor=\"%s %s %s\" \n" % round_color(emitColor, self.cp))
-            self.write_indented("ambientIntensity=\"%s\" " % (round(ambient, self.cp)))
-            self.file.write("shininess=\"%s\" " % (round(shininess, self.cp)))
-            self.file.write("transparency=\"%s\" />" % (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 @@
             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 @@
 
         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("<Background DEF=\"%s\" " % self.secureName(worldname))
         # No Skytype - just Hor color
         if blending == (False, False, False):
-            self.file.write("groundColor=\"%s %s %s\" " % grd_triple)
-            self.file.write("skyColor=\"%s %s %s\" " % grd_triple)
+            self.file.write("groundColor=\"%.3f %.3f %.3f\" " % grd_triple)
+            self.file.write("skyColor=\"%.3f %.3f %.3f\" " % grd_triple)
         # Blend Gradient
         elif blending == (True, False, False):
-            self.file.write("groundColor=\"%s %s %s, " % grd_triple)
-            self.file.write("%s %s %s\" groundAngle=\"1.57, 1.57\" " % mix_triple)
-            self.file.write("skyColor=\"%s %s %s, " % sky_triple)
-            self.file.write("%s %s %s\" skyAngle=\"1.57, 1.57\" " % mix_triple)
+            self.file.write("groundColor=\"%.3f %.3f %.3f, " % grd_triple)

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list