[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [1943] trunk/py/scripts/addons/ io_scene_x3d/export_x3d.py: make x3d script less OO, also remove some commented code and improve formatting.

Campbell Barton ideasman42 at gmail.com
Tue May 17 06:49:21 CEST 2011


Revision: 1943
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=1943
Author:   campbellbarton
Date:     2011-05-17 04:49:20 +0000 (Tue, 17 May 2011)
Log Message:
-----------
make x3d script less OO, also remove some commented code and improve formatting.

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-17 02:50:56 UTC (rev 1942)
+++ trunk/py/scripts/addons/io_scene_x3d/export_x3d.py	2011-05-17 04:49:20 UTC (rev 1943)
@@ -41,6 +41,32 @@
 
 from bpy_extras.io_utils import create_derived_objects, free_derived_objects
 
+x3d_names_reserved = {"Anchor", "Appearance", "Arc2D", "ArcClose2D", "AudioClip", "Background", "Billboard",
+                      "BooleanFilter", "BooleanSequencer", "BooleanToggle", "BooleanTrigger", "Box", "Circle2D",
+                      "Collision", "Color", "ColorInterpolator", "ColorRGBA", "component", "Cone", "connect",
+                      "Contour2D", "ContourPolyline2D", "Coordinate", "CoordinateDouble", "CoordinateInterpolator",
+                      "CoordinateInterpolator2D", "Cylinder", "CylinderSensor", "DirectionalLight", "Disk2D",
+                      "ElevationGrid", "EspduTransform", "EXPORT", "ExternProtoDeclare", "Extrusion", "field",
+                      "fieldValue", "FillProperties", "Fog", "FontStyle", "GeoCoordinate", "GeoElevationGrid",
+                      "GeoLocationLocation", "GeoLOD", "GeoMetadata", "GeoOrigin", "GeoPositionInterpolator",
+                      "GeoTouchSensor", "GeoViewpoint", "Group", "HAnimDisplacer", "HAnimHumanoid", "HAnimJoint",
+                      "HAnimSegment", "HAnimSite", "head", "ImageTexture", "IMPORT", "IndexedFaceSet",
+                      "IndexedLineSet", "IndexedTriangleFanSet", "IndexedTriangleSet", "IndexedTriangleStripSet",
+                      "Inline", "IntegerSequencer", "IntegerTrigger", "IS", "KeySensor", "LineProperties", "LineSet",
+                      "LoadSensor", "LOD", "Material", "meta", "MetadataDouble", "MetadataFloat", "MetadataInteger",
+                      "MetadataSet", "MetadataString", "MovieTexture", "MultiTexture", "MultiTextureCoordinate",
+                      "MultiTextureTransform", "NavigationInfo", "Normal", "NormalInterpolator", "NurbsCurve",
+                      "NurbsCurve2D", "NurbsOrientationInterpolator", "NurbsPatchSurface",
+                      "NurbsPositionInterpolator", "NurbsSet", "NurbsSurfaceInterpolator", "NurbsSweptSurface",
+                      "NurbsSwungSurface", "NurbsTextureCoordinate", "NurbsTrimmedSurface", "OrientationInterpolator",
+                      "PixelTexture", "PlaneSensor", "PointLight", "PointSet", "Polyline2D", "Polypoint2D",
+                      "PositionInterpolator", "PositionInterpolator2D", "ProtoBody", "ProtoDeclare", "ProtoInstance",
+                      "ProtoInterface", "ProximitySensor", "ReceiverPdu", "Rectangle2D", "ROUTE", "ScalarInterpolator",
+                      "Scene", "Script", "Shape", "SignalPdu", "Sound", "Sphere", "SphereSensor", "SpotLight", "StaticGroup",
+                      "StringSensor", "Switch", "Text", "TextureBackground", "TextureCoordinate", "TextureCoordinateGenerator",
+                      "TextureTransform", "TimeSensor", "TimeTrigger", "TouchSensor", "Transform", "TransmitterPdu",
+                      "TriangleFanSet", "TriangleSet", "TriangleSet2D", "TriangleStripSet", "Viewpoint", "VisibilitySensor",
+                      "WorldInfo", "X3D", "XvlShell", "VertexShader", "FragmentShader", "MultiShaderAppearance", "ShaderAppearance"}
 
 def clamp_color(col):
     return tuple([max(min(c, 1.0), 0.0) for c in col])
@@ -50,6 +76,21 @@
     return (mathutils.Vector((0.0, 0.0, -1.0)) * mtx.to_3x3()).normalized()[:]
 
 
+def clean_str(name, prefix='rsvd_'):
+    """cleanStr(name,prefix) - try to create a valid VRML DEF name from object name"""
+
+    newName = name
+
+    if newName in x3d_names_reserved:
+        newName = '%s%s' % (prefix, newName)
+
+    if newName[0].isdigit():
+        newName = "%s%s" % ("_", newName)
+
+    for bad in [' ', '"', '#', "'", ', ', '.', '[', '\\', ']', '{', '}']:
+        newName = newName.replace(bad, "_")
+    return newName
+
 ##########################################################
 # Functions for writing output file
 ##########################################################
@@ -57,17 +98,13 @@
 
 class x3d_class:
 
+    namesFog = ("", "LINEAR", "EXPONENTIAL", "")
+
     def __init__(self, filepath):
         #--- public you can change these ---
-        self.proto = 1
-        self.billnode = 0
-        self.halonode = 0
-        self.collnode = 0
-
         self.global_matrix = mathutils.Matrix.Rotation(-(math.pi / 2.0), 4, 'X')
 
         #--- class private don't touch ---
-        self.indentLevel = 0  # keeps track of current indenting
         self.filepath = filepath
         self.file = None
         if filepath.lower().endswith('.x3dz'):
@@ -80,89 +117,53 @@
 
         if self.file is None:
             self.file = open(self.filepath, "w", encoding="utf8", newline="\n")
-
-        self.bNav = 0
         self.nodeID = 0
-        self.namesReserved = {"Anchor", "Appearance", "Arc2D", "ArcClose2D", "AudioClip", "Background", "Billboard",
-                             "BooleanFilter", "BooleanSequencer", "BooleanToggle", "BooleanTrigger", "Box", "Circle2D",
-                             "Collision", "Color", "ColorInterpolator", "ColorRGBA", "component", "Cone", "connect",
-                             "Contour2D", "ContourPolyline2D", "Coordinate", "CoordinateDouble", "CoordinateInterpolator",
-                             "CoordinateInterpolator2D", "Cylinder", "CylinderSensor", "DirectionalLight", "Disk2D",
-                             "ElevationGrid", "EspduTransform", "EXPORT", "ExternProtoDeclare", "Extrusion", "field",
-                             "fieldValue", "FillProperties", "Fog", "FontStyle", "GeoCoordinate", "GeoElevationGrid",
-                             "GeoLocationLocation", "GeoLOD", "GeoMetadata", "GeoOrigin", "GeoPositionInterpolator",
-                             "GeoTouchSensor", "GeoViewpoint", "Group", "HAnimDisplacer", "HAnimHumanoid", "HAnimJoint",
-                             "HAnimSegment", "HAnimSite", "head", "ImageTexture", "IMPORT", "IndexedFaceSet",
-                             "IndexedLineSet", "IndexedTriangleFanSet", "IndexedTriangleSet", "IndexedTriangleStripSet",
-                             "Inline", "IntegerSequencer", "IntegerTrigger", "IS", "KeySensor", "LineProperties", "LineSet",
-                             "LoadSensor", "LOD", "Material", "meta", "MetadataDouble", "MetadataFloat", "MetadataInteger",
-                             "MetadataSet", "MetadataString", "MovieTexture", "MultiTexture", "MultiTextureCoordinate",
-                             "MultiTextureTransform", "NavigationInfo", "Normal", "NormalInterpolator", "NurbsCurve",
-                             "NurbsCurve2D", "NurbsOrientationInterpolator", "NurbsPatchSurface",
-                             "NurbsPositionInterpolator", "NurbsSet", "NurbsSurfaceInterpolator", "NurbsSweptSurface",
-                             "NurbsSwungSurface", "NurbsTextureCoordinate", "NurbsTrimmedSurface", "OrientationInterpolator",
-                             "PixelTexture", "PlaneSensor", "PointLight", "PointSet", "Polyline2D", "Polypoint2D",
-                             "PositionInterpolator", "PositionInterpolator2D", "ProtoBody", "ProtoDeclare", "ProtoInstance",
-                             "ProtoInterface", "ProximitySensor", "ReceiverPdu", "Rectangle2D", "ROUTE", "ScalarInterpolator",
-                             "Scene", "Script", "Shape", "SignalPdu", "Sound", "Sphere", "SphereSensor", "SpotLight", "StaticGroup",
-                             "StringSensor", "Switch", "Text", "TextureBackground", "TextureCoordinate", "TextureCoordinateGenerator",
-                             "TextureTransform", "TimeSensor", "TimeTrigger", "TouchSensor", "Transform", "TransmitterPdu",
-                             "TriangleFanSet", "TriangleSet", "TriangleSet2D", "TriangleStripSet", "Viewpoint", "VisibilitySensor",
-                             "WorldInfo", "X3D", "XvlShell", "VertexShader", "FragmentShader", "MultiShaderAppearance", "ShaderAppearance"}
 
-        self.namesFog = ("", "LINEAR", "EXPONENTIAL", "")
-
 ##########################################################
 # Writing nodes routines
 ##########################################################
 
-    def writeHeader(self):
+    @staticmethod
+    def writeHeader(fw, ident):
+        filepath = fw.__self__.name
         #bfile = sys.expandpath( Blender.Get('filepath') ).replace('<', '&lt').replace('>', '&gt')
-        bfile = repr(os.path.basename(self.filepath).replace('<', '&lt').replace('>', '&gt'))[1:-1]  # use outfile name
-        self.file.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
-        self.file.write("<!DOCTYPE X3D PUBLIC \"ISO//Web3D//DTD X3D 3.0//EN\" \"http://www.web3d.org/specifications/x3d-3.0.dtd\">\n")
-        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=\"generator\" content=\"Blender %s\" />\n" % bpy.app.version_string)
-        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")
+        bfile = repr(os.path.basename(filepath).replace('<', '&lt').replace('>', '&gt'))[1:-1]  # use outfile name
+        fw("%s<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" % ident)
+        fw("%s<!DOCTYPE X3D PUBLIC \"ISO//Web3D//DTD X3D 3.0//EN\" \"http://www.web3d.org/specifications/x3d-3.0.dtd\">\n" % ident)
+        fw("%s<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" % ident)
+        ident += "\t"
+        fw("%s<head>\n" % ident)
+        ident += "\t"
+        fw("%s<meta name=\"filename\" content=\"%s\" />\n" % (ident, bfile))
+        fw("%s<meta name=\"generator\" content=\"Blender %s\" />\n" % (ident, bpy.app.version_string))

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list