[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [4349] trunk/py/scripts/addons/ io_mesh_vrml2: add UV and texture support

Campbell Barton ideasman42 at gmail.com
Mon Mar 11 13:30:13 CET 2013


Revision: 4349
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=4349
Author:   campbellbarton
Date:     2013-03-11 12:30:13 +0000 (Mon, 11 Mar 2013)
Log Message:
-----------
add UV and texture support

Modified Paths:
--------------
    trunk/py/scripts/addons/io_mesh_vrml2/__init__.py
    trunk/py/scripts/addons/io_mesh_vrml2/export_vrml2.py

Modified: trunk/py/scripts/addons/io_mesh_vrml2/__init__.py
===================================================================
--- trunk/py/scripts/addons/io_mesh_vrml2/__init__.py	2013-03-11 11:14:35 UTC (rev 4348)
+++ trunk/py/scripts/addons/io_mesh_vrml2/__init__.py	2013-03-11 12:30:13 UTC (rev 4349)
@@ -42,24 +42,25 @@
 from bpy.props import CollectionProperty, StringProperty, BoolProperty, EnumProperty
 from bpy_extras.io_utils import ExportHelper
 
-class ExportPLY(bpy.types.Operator, ExportHelper):
-    """Export a single object as a Stanford PLY with normals, """ \
+class ExportVRML(bpy.types.Operator, ExportHelper):
+    """Export a single object as a VRML2, """ \
     """colors and texture coordinates"""
-    bl_idname = "export_mesh.ply"
+    bl_idname = "export_mesh.vrml2"
     bl_label = "Export PLY"
 
-    filename_ext = ".ply"
-    filter_glob = StringProperty(default="*.ply", options={'HIDDEN'})
+    filename_ext = ".wrl"
+    filter_glob = StringProperty(default="*.wrl", options={'HIDDEN'})
 
     use_mesh_modifiers = BoolProperty(
             name="Apply Modifiers",
             description="Apply Modifiers to the exported mesh",
             default=True,
             )
-    use_colors = BoolProperty(
+    use_color = BoolProperty(
             name="Vertex Colors",
             description="Export the active vertex color layer",
-            default=True)
+            default=True,
+            )
     color_type = EnumProperty(
             name='Color',
             items=(
@@ -67,6 +68,10 @@
             ('VERTEX', "Vertex Color", "")),
             default='MATERIAL',
             )
+    use_uv = BoolProperty(
+            name="Texture/UVs",
+            description="Export the active texture and UV coords",
+            default=True)
 
     @classmethod
     def poll(cls, context):
@@ -85,6 +90,8 @@
 
         row = layout.row()
         row.prop(self, "use_mesh_modifiers")
+        row = layout.row()
+        row.prop(self, "use_uv")
         row.prop(self, "use_colors")
         row = layout.row()
         row.active = self.use_colors
@@ -92,7 +99,7 @@
 
 
 def menu_func_export(self, context):
-    self.layout.operator(ExportPLY.bl_idname, text="VRML2 (.wrl)")
+    self.layout.operator(ExportVRML.bl_idname, text="VRML2 (.wrl)")
 
 
 def register():

Modified: trunk/py/scripts/addons/io_mesh_vrml2/export_vrml2.py
===================================================================
--- trunk/py/scripts/addons/io_mesh_vrml2/export_vrml2.py	2013-03-11 11:14:35 UTC (rev 4348)
+++ trunk/py/scripts/addons/io_mesh_vrml2/export_vrml2.py	2013-03-11 12:30:13 UTC (rev 4349)
@@ -20,14 +20,22 @@
 
 import bpy
 import bmesh
+import os
 
-def save_bmesh(fw, bm, use_color, color_type, material_colors):
+def save_bmesh(fw, bm,
+               use_color, color_type, material_colors,
+               use_uv, uv_image):
     fw('#VRML V2.0 utf8\n')
     fw('#modeled using blender3d http://blender.org\n')
     fw('Shape {\n')
     fw('\tappearance Appearance {\n')
-    fw('\t\tmaterial Material {\n')
-    fw('\t\t}\n')  # end 'Material'
+    if use_uv:
+        fw('\t\ttexture ImageTexture {\n')
+        fw('\t\t\turl "%s"\n' % os.path.basename(uv_image.filepath))
+        fw('\t\t}\n')  # end 'ImageTexture'
+    else:
+        fw('\t\tmaterial Material {\n')
+        fw('\t\t}\n')  # end 'Material'
     fw('\t}\n')  # end 'Appearance'
 
     fw('\tgeometry IndexedFaceSet {\n')
@@ -70,8 +78,9 @@
             del v
             fw(']\n')  # end 'color[]'
             fw('\t\t}\n')  # end 'Color'
-    
-    if use_color:
+
+        # ---
+
         if color_type == 'MATERIAL':
             fw('\t\tcolorIndex [ ')
             i = None
@@ -85,6 +94,29 @@
         elif color_type == 'VERTEX':
             pass
 
+    if use_uv:
+        fw('\t\ttexCoord TextureCoordinate {\n')
+        fw('\t\t\tpoint [ ')
+        v = None
+        uv_layer = bm.loops.layers.uv.active
+        assert(uv_layer is not None)
+        for f in bm.faces:
+            for l in f.loops:
+                fw("%.4f %.4f " % l[uv_layer].uv[:])
+
+        del f
+        fw(']\n')  # end 'point[]'
+        fw('\t\t}\n')  # end 'TextureCoordinate'
+
+        # ---
+
+        fw('\t\ttexCoordIndex [ ')
+        i = None
+        for i in range(0, len(bm.faces) * 3, 3):
+            fw("%d %d %d -1 " % (i, i + 1, i + 2))
+        del i
+        fw(']\n')  # end 'coordIndex[]'
+
     fw('\t\tcoordIndex [ ')
     f = fv = None
     for f in bm.faces:
@@ -97,8 +129,30 @@
     fw('}\n')  # end 'Shape'
 
 
-def save_object(fw, obj, use_mesh_modifiers, use_color, color_type):
-    
+def detect_default_image(obj, bm):
+    tex_layer = bm.faces.layers.tex.active
+    for f in bm.faces:
+        image = f[tex_layer].image
+        if image is not None:
+            return image
+    for m in obj.data.materials:
+        if m is not None:
+            # backwards so topmost are highest priority
+            for mtex in reversed(mat.texture_slots):
+                if mtex and mtex.use_map_color_diffuse:
+                    texture = mtex.texture
+                    if texture and texture.type == 'IMAGE':
+                        image = texture.image
+                        if image is not None:
+                            return image
+    return None
+
+
+def save_object(fw, obj,
+                use_mesh_modifiers,
+                use_color, color_type,
+                use_uv):
+
     assert(obj.type == 'MESH')
     
     # TODO use_mesh_modifiers
@@ -131,25 +185,42 @@
         else:
             assert(0)
 
+    if use_uv:
+        if bm.loops.layers.uv.active is None:
+            use_uv = False
+        uv_image = detect_default_image(obj, bm)
+        if uv_image is None:
+            use_uv = False
+
     material_colors = []
-    save_bmesh(fw, bm, use_color, color_type, material_colors)
+    save_bmesh(fw, bm,
+               use_color, color_type, material_colors,
+               use_uv, uv_image)
 
     bm.free()
 
-def save_object_fp(filepath, obj, use_mesh_modifiers, use_color, color_type):
-    file = filepath(filepath)
-    save_object(file.write, use_color, color_type)
+def save_object_fp(filepath, obj, use_mesh_modifiers,
+                   use_color, color_type,
+                   use_uv):
+    file = open(filepath, 'w', encoding='utf-8')
+    save_object(file.write, obj,
+                use_mesh_modifiers,
+                use_color, color_type,
+                use_uv)
     file.close()
 
 def save(operator,
          context,
          filepath="",
          use_mesh_modifiers=True,
-         use_colors=True,
-         color_type='MATERIAL'):
+         use_color=True,
+         color_type='MATERIAL',
+         use_uv=True):
 
     save_object_fp(filepath, context.object,
-                   use_mesh_modifiers, use_color, color_type)
+                   use_mesh_modifiers,
+                   use_color, color_type,
+                   use_uv)
 
     return {'FINISHED'}
 



More information about the Bf-extensions-cvs mailing list