[Bf-extensions-cvs] [b1a19799] master: io_import_scene_lwo: moved to contrib: T63750
meta-androcto
noreply at git.blender.org
Fri May 24 04:01:08 CEST 2019
Commit: b1a19799d2ec0dc320b8064d281ee81a1f018b9a
Author: meta-androcto
Date: Fri May 24 12:00:45 2019 +1000
Branches: master
https://developer.blender.org/rBACb1a19799d2ec0dc320b8064d281ee81a1f018b9a
io_import_scene_lwo: moved to contrib: T63750
===================================================================
A io_import_scene_lwo.py
===================================================================
diff --git a/io_import_scene_lwo.py b/io_import_scene_lwo.py
new file mode 100644
index 00000000..cca58358
--- /dev/null
+++ b/io_import_scene_lwo.py
@@ -0,0 +1,1263 @@
+# ##### 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 #####
+
+bl_info= {
+ "name": "Import LightWave Objects",
+ "author": "Ken Nign (Ken9)",
+ "version": (1, 2),
+ "blender": (2, 57, 0),
+ "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": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
+ "Scripts/Import-Export/LightWave_Object",
+ "category": "Import-Export",
+}
+
+# Copyright (c) Ken Nign 2010
+# ken at virginpi.com
+#
+# Version 1.3 - Aug 11, 2011
+#
+# 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.
+#
+# Blender is limited to only 8 UV Texture and 8 Vertex Color maps,
+# thus only the first 8 of each can be imported.
+#
+# History:
+#
+# 1.3 Fixed CC Edge Weight loading.
+#
+# 1.2 Added Absolute Morph and CC Edge Weight support.
+# Made edge creation safer.
+# 1.0 First Release
+
+
+import os
+import struct
+import chunk
+
+import bpy
+import mathutils
+from mathutils.geometry import tessellate_polygon
+
+
+class _obj_layer(object):
+ __slots__ = (
+ "name",
+ "index",
+ "parent_index",
+ "pivot",
+ "pols",
+ "bones",
+ "bone_names",
+ "bone_rolls",
+ "pnts",
+ "wmaps",
+ "colmaps",
+ "uvmaps",
+ "morphs",
+ "edge_weights",
+ "surf_tags",
+ "has_subds",
+ )
+ 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.edge_weights= {}
+ self.surf_tags= {}
+ self.has_subds= False
+
+
+class _obj_surf(object):
+ __slots__ = (
+ "bl_mat",
+ "name",
+ "source_name",
+ "colr",
+ "diff",
+ "lumi",
+ "spec",
+ "refl",
+ "rblr",
+ "tran",
+ "rind",
+ "tblr",
+ "trnl",
+ "glos",
+ "shrp",
+ "smooth",
+ )
+
+ 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,
+ USE_EXISTING_MATERIALS=False):
+ """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':
+ # LWOB and LWLO are the old format, 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, USE_EXISTING_MATERIALS)
+
+ 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, False)
+ elif vmap_type == b'SPOT':
+ read_morph(rootchunk.read(), layers, True)
+ 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)
+ elif vmad_type == b'WGHT':
+ # We only read the Edge Weight map if it's there.
+ read_weight_vmad(rootchunk.read(), layers)
+ 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:
+ # 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:", 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(), layer
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-extensions-cvs
mailing list