[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [1612] trunk/py/scripts/addons/ io_scene_m3: commit 53dd5d4cdded47233bcd9ebf42e44c6fd4886c8f

Cory Perry killogge at gmail.com
Wed Feb 16 10:13:18 CET 2011


Revision: 1612
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=1612
Author:   muraj
Date:     2011-02-16 09:13:17 +0000 (Wed, 16 Feb 2011)
Log Message:
-----------
commit 53dd5d4cdded47233bcd9ebf42e44c6fd4886c8f
Author: Cory Perry <killogge at gmail.com>
Date:   Wed Feb 16 03:51:42 2011 -0500

    Updated to latest svn branch of blender (r34892).
    * Lowered influence of normal map for better looking results.
    * Modifed code to utilize io_utils module
    * Minor updates to blender info structs

Revision Links:
--------------
    http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=34892

Modified Paths:
--------------
    trunk/py/scripts/addons/io_scene_m3/__init__.py
    trunk/py/scripts/addons/io_scene_m3/import_m3.py

Property Changed:
----------------
    trunk/py/scripts/addons/io_scene_m3/


Property changes on: trunk/py/scripts/addons/io_scene_m3
___________________________________________________________________
Added: svn:ignore
   + *.pyc
README*
SC2_models
.git


Modified: trunk/py/scripts/addons/io_scene_m3/__init__.py
===================================================================
--- trunk/py/scripts/addons/io_scene_m3/__init__.py	2011-02-16 05:35:49 UTC (rev 1611)
+++ trunk/py/scripts/addons/io_scene_m3/__init__.py	2011-02-16 09:13:17 UTC (rev 1612)
@@ -16,61 +16,82 @@
 #
 # ##### END GPL LICENSE BLOCK #####
 
-MAJOR_VERSION = 0
-MINOR_VERSION = 2
-BLENDER_VERSION = (2, 54, 0)
-__version__ = "%d.%d.0" % (MAJOR_VERSION, MINOR_VERSION)
+# <pep8 compliant>
+
 bl_info = {
-    'name': 'Import: M3 (.m3)',
+    'name': 'Blizzard M3 format',
     'author': 'Cory Perry',
-    'version': (0, 2, 0),
-    'blender': (2, 5, 4),
-    "api": 31878,
-    "location": "File > Import",
-    "warning": "",
-    "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/Scripts/"\
-        "Import-Export/M3_Import",
-    "tracker_url": "http://projects.blender.org/tracker/index.php?"\
-        "func=detail&aid=24017",
-    "category": "Import-Export",
-    "description": "This script imports m3 format files to Blender."}
+    'version': (0, 2, 1),
+    'blender': (2, 5, 6),
+    'api': 34893,
+    'location': 'File > Import-Export',
+    'description': 'This script imports the Blizzard M3 format (.m3)',
+    'warning': '',
+    'wiki_url': 'http://wiki.blender.org/index.php/Extensions:2.5/Py/Scripts/'\
+        'Import-Export/M3_Import',
+    'tracker_url': 'http://projects.blender.org/tracker/index.php?'\
+        'func=detail&aid=24017',
+    'category': 'Import-Export'}
 
+
+# To support reload properly, try to access a package var, if it's there,
+# reload everything
 if "bpy" in locals():
     import imp
-    imp.reload(import_m3)
-    #imp.reload(export_m3)
-else:
-    pass
-    #from . import import_m3
-    #from . import export_m3
+    if 'import_m3' in locals():
+        imp.reload(import_m3)
+#   if 'export_m3' in locals():
+#       imp.reload(export_m3)
 
+import time
+import datetime
 import bpy
+from bpy.props import *
+from io_utils import ImportHelper, ExportHelper
 
-def menu_import(self, context):
-    self.layout.operator(import_m3.M3Importer.bl_idname, \
-        text="Blizzard M3 (.m3)").filepath = "*.m3"
 
+class ImportM3(bpy.types.Operator, ImportHelper):
+    '''Import from M3 file format (.m3)'''
+    bl_idname = 'import_scene.blizzard_m3'
+    bl_label = 'Import M3'
 
-#def menu_export(self, context):
-#    from io_mesh_raw import export_raw
-#    import os
-#    default_path = os.path.splitext(bpy.data.filepath)[0] + ".raw"
-#    self.layout.operator(export_raw.RawExporter.bl_idname, \
-#        text="Raw Faces (.raw)").filepath = default_path
+    filename_ext = '.m3'
+    filter_glob = StringProperty(default='*.m3', options={'HIDDEN'})
 
+    use_image_search = BoolProperty(name='Image Search',
+                        description='Search subdirectories for any associated'\
+                                    'images', default=True)
+
+    def execute(self, context):
+        from . import import_m3
+        print('Importing file', self.filepath)
+        t = time.mktime(datetime.datetime.now().timetuple())
+        with open(self.filepath, 'rb') as file:
+            import_m3.read(file, context, self)
+        t = time.mktime(datetime.datetime.now().timetuple()) - t
+        print('Finished importing in', t, 'seconds')
+        return {'FINISHED'}
+
+
+def menu_func_import(self, context):
+    self.layout.operator(ImportM3.bl_idname, text='Blizzard M3 (.m3)')
+
+
+#def menu_func_export(self, context):
+#   self.layout.operator(ExportM3.bl_idname, text='Blizzard M3 (.m3)')
+
+
 def register():
-    from . import import_m3
     bpy.utils.register_module(__name__)
+    bpy.types.INFO_MT_file_import.append(menu_func_import)
+#   bpy.types.INFO_MT_file_export.append(menu_func_export)
 
-    bpy.types.INFO_MT_file_import.append(menu_import)
-#    bpy.types.INFO_MT_file_export.append(menu_export)
 
-
 def unregister():
     bpy.utils.unregister_module(__name__)
+    bpy.types.INFO_MT_file_import.remove(menu_func_import)
+#   bpy.types.INFO_MT_file_export.remove(menu_func_export)
 
-    bpy.types.INFO_MT_file_import.remove(menu_import)
-#    bpy.types.INFO_MT_file_export.remove(menu_export)
 
 if __name__ == "__main__":
     register()

Modified: trunk/py/scripts/addons/io_scene_m3/import_m3.py
===================================================================
--- trunk/py/scripts/addons/io_scene_m3/import_m3.py	2011-02-16 05:35:49 UTC (rev 1611)
+++ trunk/py/scripts/addons/io_scene_m3/import_m3.py	2011-02-16 09:13:17 UTC (rev 1612)
@@ -17,7 +17,7 @@
 # ##### END GPL LICENSE BLOCK #####
 
 __author__ = "Cory Perry (muraj)"
-__version__ = "0.0.2"
+__version__ = "0.2.1"
 __bpydoc__ = """\
 This script imports m3 format files to Blender.
 
@@ -47,29 +47,17 @@
 open.
 
 Notes:<br>
+    Known issue with Thor.m3, seems to add a lot of unecessary verts.
     Generates the standard verts and faces lists.
 """
 
 import bpy
 import mathutils
-import time
-import datetime
 import struct
 import os.path
 from bpy.props import *
-##################
-#LOG = open('C:\m3_log.txt','w')
-LOG = None
+from io_utils import load_image
 
-
-def debug(*args):    # TODO: make a little more robust, right now just a hack
-    if LOG == None:
-        print(*args)
-    else:
-        for i in args:
-            LOG.write(str(i) + ' ')
-        LOG.write('\n')
-        LOG.flush()
 ##################
 ## Struct setup ##
 ##################
@@ -271,20 +259,20 @@
     global verFlag
     h = hdr(file)
     if h.magic[::-1] == b'MD34':
-        debug('m3_import: !WARNING! MD34 files not full tested...')
+        print('m3_import: !WARNING! MD34 files not full tested...')
         verFlag = True
     elif h.magic[::-1] == b'MD33':
         verFlag = False
     else:
         raise Exception('m3_import: !ERROR! Not a valid or supported m3 file')
     file.seek(h.ofsTag)    # Jump to the Tag table
-    debug('m3_import: !INFO! Reading TagTable...')
+    print('m3_import: !INFO! Reading TagTable...')
     tagTable = [Tag(file) for _ in range(h.nTag)]
     file.seek(tagTable[h.MODLref.refid].ofs)
     m = MODL(file, tagTable[h.MODLref.refid].version)
     if not m.flags & 0x20000:
         raise Exception('m3_import: !ERROR! Model doesn\'t have any vertices')
-    debug('m3_import: !INFO! Reading Vertices...')
+    print('m3_import: !INFO! Reading Vertices...')
     vert_flags = m.flags & 0x1E0000        # Mask out the vertex version
     file.seek(tagTable[m.views.refid].ofs)
     d = div(file)
@@ -292,7 +280,7 @@
     verts = [vertex(file, vert_flags) \
        for _ in range(tagTable[m.vert.refid].nTag // vertex.size(vert_flags))]
     file.seek(tagTable[d.faces.refid].ofs)
-    debug('m3_import: !INFO! Reading Faces...')
+    print('m3_import: !INFO! Reading Faces...')
     rawfaceTable = struct.unpack('H' * (tagTable[d.faces.refid].nTag), \
                     file.read(tagTable[d.faces.refid].nTag * 2))
     faceTable = []
@@ -300,7 +288,9 @@
         faceTable.append(rawfaceTable[i - 1])
         if i % 3 == 0:    # Add a zero for the fourth index to the face.
             faceTable.append(0)
-    debug('m3_import: !INFO! Adding Geometry...')
+    print("m3_import: !INFO! Read %d vertices and %d faces" \
+            % (len(verts), len(faceTable)))
+    print('m3_import: !INFO! Adding Geometry...')
     mesh = bpy.data.meshes.new(os.path.basename(op.properties.filepath))
     mobj = bpy.data.objects.new(os.path.basename(op.properties.filepath), mesh)
     context.scene.objects.link(mobj)
@@ -314,36 +304,36 @@
     mesh.vertices.foreach_set('normal', n)
     mesh.faces.foreach_set('vertices_raw', faceTable)
     uvtex = mesh.uv_textures.new()
-    debug(len(verts), len(faceTable))
     for i, face in enumerate(mesh.faces):
         uf = uvtex.data[i]
         uf.uv1 = verts[faceTable[i * 4 + 0]].uv
         uf.uv2 = verts[faceTable[i * 4 + 1]].uv
         uf.uv3 = verts[faceTable[i * 4 + 2]].uv
         uf.uv4 = (0, 0)
-    debug('m3_import: !INFO! Importing materials...')
+    print('m3_import: !INFO! Importing materials...')
     material = bpy.data.materials.new('Mat00')
     mesh.materials.append(material)
     file.seek(tagTable[m.materials.refid].ofs)
     mm = mat(file)
-    tex_map = [('use_map_diffuse', 0), ('use_map_specular', 2),\
+    tex_map = [('use_map_color_diffuse', 0), ('use_map_specular', 2),\
                 ('use_map_normal', 9)]
     for map, i in tex_map:
         file.seek(tagTable[mm.layers[i].refid].ofs)
         nref = layr(file).name
         file.seek(tagTable[nref.refid].ofs)
         name = bytes.decode(file.read(nref.entries - 1))
-        path = os.path.join(os.path.dirname(op.properties.filepath),\
-                os.path.basename(str(name)))
-        tex = bpy.data.textures.new(name=os.path.basename(path), type='IMAGE')
-        if os.path.exists(path):
-            tex.image = bpy.data.images.load(path)
-            debug("m3_import: !INFO! Loaded %s" % (path))
+        name = os.path.basename(str(name))
+        tex = bpy.data.textures.new(name=name, type='IMAGE')
+        tex.image = load_image(name, os.path.dirname(op.filepath))
+        if tex.image != None:
+            print("m3_import: !INFO! Loaded %s" % (name))
         else:
-            debug("m3_import: !WARNING! Cannot find texture \"%s\"" % (path))
+            print("m3_import: !WARNING! Cannot find texture \"%s\"" % (name))
         mtex = material.texture_slots.add()
         mtex.texture = tex
         mtex.texture_coords = 'UV'
+        if i == 9:
+            mtex.normal_factor = 0.1    # Just a guess, seems to look nice
         mtex.use_map_color_diffuse = (i == 0)
         setattr(mtex, map, True)
 



More information about the Bf-extensions-cvs mailing list