[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [3124] trunk/py/scripts/addons/ io_mesh_ply/import_ply.py: PLY importer - support for textures

Dalai Felinto dfelinto at gmail.com
Fri Mar 16 05:39:59 CET 2012


Revision: 3124
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=3124
Author:   dfelinto
Date:     2012-03-16 04:39:41 +0000 (Fri, 16 Mar 2012)
Log Message:
-----------
PLY importer - support for textures
thanks Campbell for review and tweaks

Modified Paths:
--------------
    trunk/py/scripts/addons/io_mesh_ply/import_ply.py

Modified: trunk/py/scripts/addons/io_mesh_ply/import_ply.py
===================================================================
--- trunk/py/scripts/addons/io_mesh_ply/import_ply.py	2012-03-15 20:04:44 UTC (rev 3123)
+++ trunk/py/scripts/addons/io_mesh_ply/import_ply.py	2012-03-16 04:39:41 UTC (rev 3124)
@@ -126,6 +126,7 @@
 
 def read(filepath):
     format = b''
+    texture = b''
     version = b'1.0'
     format_specs = {b'binary_little_endian': '<',
                     b'binary_big_endian': '>',
@@ -162,13 +163,22 @@
             continue
         if tokens[0] == b'end_header':
             break
-        elif tokens[0] == b'comment' or tokens[0] == b'obj_info':
+        elif tokens[0] == b'comment':
+            if len(tokens) < 2:
+                continue
+            elif tokens[1] == b'TextureFile':
+                if len(tokens) < 4:
+                    print('Invalid texture line')
+                else:
+                    texture = tokens[2]
             continue
+        elif tokens[0] == b'obj_info':
+            continue
         elif tokens[0] == b'format':
             if len(tokens) < 3:
                 print('Invalid format line')
                 return None
-            if tokens[1] not in format_specs:  # .keys(): # keys is implicit
+            if tokens[1] not in format_specs:
                 print('Unknown format', tokens[1])
                 return None
             if tokens[2] != version:
@@ -201,7 +211,7 @@
 
     file.close()
 
-    return obj_spec, obj
+    return obj_spec, obj, texture
 
 
 import bpy
@@ -213,7 +223,7 @@
     # from bpy_extras.image_utils import load_image  # UNUSED
 
     t = time.time()
-    obj_spec, obj = read(filepath)
+    obj_spec, obj, texture = read(filepath)
     if obj is None:
         print('Invalid file')
         return
@@ -321,8 +331,34 @@
     mesh.validate()
     mesh.update()
 
+    if texture and uvindices:
+
+        import os
+        import sys
+        from bpy_extras.image_utils import load_image
+
+        encoding = sys.getfilesystemencoding()
+        encoded_texture = texture.decode(encoding=encoding)
+        name = bpy.path.display_name_from_filepath(texture)
+        image = load_image(encoded_texture, os.path.dirname(filepath), recursive=True, place_holder=True)
+
+        if image:
+            texture = bpy.data.textures.new(name=name, type='IMAGE')
+            texture.image = image
+
+            material = bpy.data.materials.new(name=name)
+            material.use_shadeless = True
+
+            mtex = material.texture_slots.add()
+            mtex.texture = texture
+            mtex.texture_coords = 'UV'
+            mtex.use_map_color_diffuse = True
+
+            mesh.materials.append(material)
+            for face in mesh.uv_textures[0].data:
+                face.image = image
+
     scn = bpy.context.scene
-    #scn.objects.selected = [] # XXX25
 
     obj = bpy.data.objects.new(ply_name, mesh)
     scn.objects.link(obj)



More information about the Bf-extensions-cvs mailing list