[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [972] contrib/py/scripts/addons/ io_import_scene_lwo.py:

Ken Nign ken at virginpi.com
Fri Sep 3 03:19:59 CEST 2010


Revision: 972
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=972
Author:   ken9
Date:     2010-09-03 03:19:57 +0200 (Fri, 03 Sep 2010)

Log Message:
-----------


Added Paths:
-----------
    contrib/py/scripts/addons/io_import_scene_lwo.py

Added: contrib/py/scripts/addons/io_import_scene_lwo.py
===================================================================
--- contrib/py/scripts/addons/io_import_scene_lwo.py	                        (rev 0)
+++ contrib/py/scripts/addons/io_import_scene_lwo.py	2010-09-03 01:19:57 UTC (rev 972)
@@ -0,0 +1,1130 @@
+# ##### BEGIN GPL LICENSE BLOCK #####
+#
+#  This program is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU General Public License
+#  as published by the Free Software Foundation; either version 2
+#  of the License, or (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software Foundation,
+#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+# <pep8 compliant>
+
+# Copyright (c) Ken Nign 2010
+# ken at virginpi.com
+#
+# Version 1.0 - Sep 1, 2010
+#
+# Loads a LightWave .lwo object file, including the vertex maps such as
+# UV, Morph, Color and Weight maps.
+#
+# Will optionally create an Armature from an embedded Skelegon rig.
+#
+# Point orders are maintained so that .mdds can exchanged with other
+# 3D programs.
+#
+#
+# Notes:
+# NGons, polygons with more than 4 points are supported, but are
+# added (as triangles) after the vertex maps have been applied. Thus they
+# won't contain all the vertex data that the original ngon had.
+
+
+bl_addon_info= {
+    "name": "Import LightWave Objects",
+    "author": "Ken Nign (Ken9)",
+    "version": (1,0),
+    "blender": (2, 5, 3),
+    "api": 31704,
+    "location": "File > Import > LightWave Object (.lwo)",
+    "description": "Imports a LWO file including any UV, Morph and Color maps. Can convert Skelegons to an Armature.",
+    "warning": "",
+    "wiki_url": "",
+    "tracker_url": "",
+    "category": "Import/Export"}
+
+
+import os
+import io
+import time
+import struct
+import chunk
+
+import bpy
+import mathutils
+from geometry import PolyFill
+
+
+class _obj_layer():
+    def __init__(self):
+        self.name= ""
+        self.index= -1
+        self.parent_index= -1
+        self.pivot= [0,0,0]
+        self.pols= []
+        self.bones= []
+        self.bone_names= {}
+        self.bone_rolls= {}
+        self.pnts= []
+        self.wmaps= {}
+        self.colmaps= {}
+        self.uvmaps= {}
+        self.morphs= {}
+        self.surf_tags= {}
+        self.has_subds= False
+
+
+class _obj_surf():
+    def __init__(self):
+        self.bl_mat= None
+        self.name= "Default"
+        self.source_name= ""
+        self.colr= [1.0, 1.0, 1.0]
+        self.diff= 1.0   # Diffuse
+        self.lumi= 0.0   # Luminosity
+        self.spec= 0.0   # Specular
+        self.refl= 0.0   # Reflectivity
+        self.rblr= 0.0   # Reflection Bluring
+        self.tran= 0.0   # Transparency (the opposite of Blender's Alpha value)
+        self.rind= 1.0   # RT Transparency IOR
+        self.tblr= 0.0   # Refraction Bluring
+        self.trnl= 0.0   # Translucency
+        self.glos= 0.4   # Glossiness
+        self.shrp= 0.0   # Diffuse Sharpness
+        self.smooth= False  # Surface Smoothing
+        
+
+def load_lwo(filename,
+             context,
+             ADD_SUBD_MOD= True,
+             LOAD_HIDDEN= False,
+             SKEL_TO_ARM= True):
+    '''Read the LWO file, hand off to version specific function.'''
+    name, ext= os.path.splitext(os.path.basename(filename))
+    file= open(filename, 'rb')
+    
+    try:
+        header, chunk_size, chunk_name = struct.unpack(">4s1L4s", file.read(12))
+    except:
+        print("Error parsing file header!")
+        file.close()
+        return
+    
+    layers= []
+    surfs= {}
+    tags= []
+    # Gather the object data using the version specific handler.
+    if chunk_name == b'LWO2':
+        read_lwo2(file, filename, layers, surfs, tags, ADD_SUBD_MOD, LOAD_HIDDEN, SKEL_TO_ARM)
+    elif chunk_name == b'LWOB' or chunk_name == b'LWLO':    # LWLO is a layered object.
+        read_lwob(file, filename, layers, surfs, tags, ADD_SUBD_MOD)
+    else:
+        print("Not a supported file type!")
+        file.close()
+        return
+    
+    file.close()
+    
+    # With the data gathered, build the object(s).
+    build_objects(layers, surfs, tags, name, ADD_SUBD_MOD, SKEL_TO_ARM)
+    
+    layers= None
+    surfs.clear()
+    tags= None
+
+
+def read_lwo2(file, filename, layers, surfs, tags, add_subd_mod, load_hidden, skel_to_arm):
+    '''Read version 2 file, LW 6+.'''
+    handle_layer= True
+    last_pols_count= 0
+    just_read_bones= False
+    print("Importing LWO: " + filename + "\nLWO v2 Format")
+    
+    while True:
+        try:
+            rootchunk = chunk.Chunk(file)
+        except EOFError:
+            break
+        
+        if rootchunk.chunkname == b'TAGS':
+            read_tags(rootchunk.read(), tags)
+        elif rootchunk.chunkname == b'LAYR':
+            handle_layer= read_layr(rootchunk.read(), layers, load_hidden)
+        elif rootchunk.chunkname == b'PNTS' and handle_layer:
+            read_pnts(rootchunk.read(), layers)
+        elif rootchunk.chunkname == b'VMAP' and handle_layer:
+            vmap_type = rootchunk.read(4)
+            
+            if vmap_type == b'WGHT':
+                read_weightmap(rootchunk.read(), layers)
+            elif vmap_type == b'MORF':
+                read_morph(rootchunk.read(), layers)
+            elif vmap_type == b'TXUV':
+                read_uvmap(rootchunk.read(), layers)
+            elif vmap_type == b'RGB ' or vmap_type == b'RGBA':
+                read_colmap(rootchunk.read(), layers)
+            else:
+                rootchunk.skip()
+                
+        elif rootchunk.chunkname == b'VMAD' and handle_layer:
+            vmad_type= rootchunk.read(4)
+            
+            if vmad_type == b'TXUV':
+                read_uv_vmad(rootchunk.read(), layers, last_pols_count)
+            elif vmad_type == b'RGB ' or vmad_type == b'RGBA':
+                read_color_vmad(rootchunk.read(), layers, last_pols_count)
+            else:
+                rootchunk.skip()
+                
+        elif rootchunk.chunkname == b'POLS' and handle_layer:
+            face_type = rootchunk.read(4)
+            just_read_bones= False
+            # PTCH is LW's Subpatches, SUBD is CatmullClark.
+            if (face_type == b'FACE' or face_type == b'PTCH' or face_type == b'SUBD') and handle_layer:
+                last_pols_count= read_pols(rootchunk.read(), layers)
+                if face_type != b'FACE':
+                    layers[-1].has_subds= True
+            elif face_type == b'BONE' and handle_layer:
+                read_bones(rootchunk.read(), layers)
+                just_read_bones= True
+            else:
+                rootchunk.skip()
+                
+        elif rootchunk.chunkname == b'PTAG' and handle_layer:
+            tag_type,= struct.unpack("4s", rootchunk.read(4));
+            if tag_type == b'SURF' and not just_read_bones:
+                # We have to ignore the surface data if we just read a bones chunk.
+                read_surf_tags(rootchunk.read(), layers, last_pols_count)
+                
+            elif skel_to_arm:
+                if tag_type == b'BNUP':
+                    read_bone_tags(rootchunk.read(), layers, tags, 'BNUP')
+                elif tag_type == b'BONE':
+                    read_bone_tags(rootchunk.read(), layers, tags, 'BONE')
+                else:
+                    rootchunk.skip()
+            else:
+                rootchunk.skip()
+        elif rootchunk.chunkname == b'SURF':
+            read_surf(rootchunk.read(), surfs)
+        else:
+            #if handle_layer:
+                #print("Skipping Chunk: " + str(rootchunk.chunkname))
+            rootchunk.skip()
+
+
+def read_lwob(file, filename, layers, surfs, tags, add_subd_mod):
+    '''Read version 1 file, LW < 6.'''
+    last_pols_count= 0
+    print("Importing LWO: " + filename + "\nLWO v1 Format")
+    
+    while True:
+        try:
+            rootchunk = chunk.Chunk(file)
+        except EOFError:
+            break
+        
+        if rootchunk.chunkname == b'SRFS':
+            read_tags(rootchunk.read(), tags)
+        elif rootchunk.chunkname == b'LAYR':
+            read_layr_5(rootchunk.read(), layers)
+        elif rootchunk.chunkname == b'PNTS':
+            if len(layers) == 0:    # LWOB files have no LAYR chunk to set this up
+                nlayer= _obj_layer()
+                nlayer.name= "Layer 1"
+                layers.append(nlayer)
+            read_pnts(rootchunk.read(), layers)
+        elif rootchunk.chunkname == b'POLS':
+            last_pols_count= read_pols_5(rootchunk.read(), layers)
+        elif rootchunk.chunkname == b'PCHS':
+            last_pols_count= read_pols_5(rootchunk.read(), layers)
+            layers[-1].has_subds= True
+        elif rootchunk.chunkname == b'PTAG':
+            tag_type,= struct.unpack("4s", rootchunk.read(4));
+            if tag_type == b'SURF':
+                read_surf_tags_5(rootchunk.read(), layers, last_pols_count)
+            else:
+                rootchunk.skip()
+        elif rootchunk.chunkname == b'SURF':
+            read_surf_5(rootchunk.read(), surfs)
+        else:
+            # For Debugging \/.
+            #if handle_layer:
+                #print("Skipping Chunk: " + str(rootchunk.chunkname))
+            rootchunk.skip()
+
+
+def read_lwostring(raw_name):
+    '''Parse a zero-padded string.'''
+    i= -1
+    name= ''
+    for i in range(len(raw_name)):
+        if raw_name[i] == 0:
+            break
+        
+    name_len = i + 1
+    if name_len % 2 == 1:   # Test for oddness.
+        name_len += 1
+        
+    if i > 0:
+        # Some plugins put non-text strings in the tags chunk.
+        try:
+            name= raw_name[0:i].decode()
+        except:
+            name= "BAD_STRING"
+    
+    return name, name_len
+
+    
+def read_vx(pointdata):
+    '''Read a variable-length index.'''
+    if pointdata[0] != 255:
+        index= pointdata[0]*256 + pointdata[1]
+        size= 2
+    else:

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list