Blender 2.5 *.mesh Importer w/ Data Unpacker

Results 1 to 12 of 12
  1. #1
    Legendary Phantom* is offline
    True MemberRank
    Aug 2006 Join Date
    262Posts

    Blender 2.5 *.mesh Importer w/ Data Unpacker

    Warning: I have never played Dekaron and these posts are for sharing knowledge only. All python source code should be regarded as software prototyping unless otherwise stated.

    Installation:
    1. Download and install Blender 2.5 Alpha 0 from Blender.org
    2. Download and install Python 3.1.x from Python.org (Windows Users)
    3. Copy and paste source code to Blender scripts directory (<Blender_Directory>/.blender/scripts/io) as import_scene_dekaron_mesh.py


    Usage:
    1. Click File > Import > Dekaron (.mesh)
    2. Select *.mesh file
    3. Click Import Dekaron Mesh


    License:
    Source:
    Copy and save as: import_scene_dekaron_mesh.py

    Code:
    # Copyright (c) 2009-2012 AJ
    # 
    # Permission is hereby granted, free of charge, to any person obtaining a copy
    # of this software and associated documentation files (the "Software"), to deal
    # in the Software without restriction, including without limitation the rights
    # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    # copies of the Software, and to permit persons to whom the Software is
    # furnished to do so, subject to the following conditions:
    # 
    # The above copyright notice and this permission notice shall be included in
    # all copies or substantial portions of the Software.
    # 
    # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    # THE SOFTWARE.
    #
    # Version 0.0.1
    #     * Initial release
    
    __author__ = 'AJ'
    __email__ = ''
    __url__ = ('blender', 'elysiun', 'Project homepage, http://www.ragezone.com/')
    __version__ = '0.0.1'
    __bpydoc__ = ''' \
    Import Dekaron mesh files
    '''
    
    
    import bpy
    import dynamic_menu
    import io
    import os
    import struct
    
    
    def create_dekaron_mesh(dekaron_mesh_name, dekaron_mesh_vertices_co, dekaron_mesh_vertices_uv, dekaron_mesh_indices):
        dekaron_mesh = bpy.data.add_mesh(dekaron_mesh_name)
        
        dekaron_mesh.add_geometry(int(len(dekaron_mesh_vertices_co) / 3), 0, int(len(dekaron_mesh_indices) / 4))
        
        dekaron_mesh.verts.foreach_set("co", dekaron_mesh_vertices_co)
        
        dekaron_mesh.faces.foreach_set("verts_raw", dekaron_mesh_indices)
        
        if dekaron_mesh.faces is not None and \
           dekaron_mesh_vertices_uv is not None and \
           len(dekaron_mesh_vertices_uv) > 0:
            dekaron_mesh.add_uv_texture()
            
            for i, dekaron_mesh_face in enumerate(dekaron_mesh.faces):
                dekaron_mesh_texture_face = dekaron_mesh.uv_textures[0].data[i]
                
                dekaron_mesh_texture_face_uvs = [dekaron_mesh_vertices_uv[j] for j in dekaron_mesh_face.verts]
                
                dekaron_mesh_texture_face.uv1 = dekaron_mesh_texture_face_uvs[0]
                dekaron_mesh_texture_face.uv2 = dekaron_mesh_texture_face_uvs[1]
                dekaron_mesh_texture_face.uv3 = dekaron_mesh_texture_face_uvs[2]
        
        dekaron_mesh.update()
        
        dekaron_mesh_object = bpy.data.add_object("MESH", dekaron_mesh_name)
        
        dekaron_mesh_object.data = dekaron_mesh
        
        return dekaron_mesh_object
    
    
    def parse_dekaron_mesh_file_mesh0(dekaron_mesh_file):
        dekaron_mesh_file_mesh_name_length = struct.unpack('<I', dekaron_mesh_file.read(4))[0]
        
        dekaron_mesh_file_mesh_name = dekaron_mesh_file.read(dekaron_mesh_file_mesh_name_length)
        
        dekaron_mesh_file_mesh_aabb = struct.unpack('<6f', dekaron_mesh_file.read(24))[0]
        
        dekaron_mesh_file_mesh_vertex_count = struct.unpack('<I', dekaron_mesh_file.read(4))[0]
        
        dekaron_mesh_file_mesh_vertices_co = []
        
        for i in range(dekaron_mesh_file_mesh_vertex_count):
            dekaron_mesh_file_mesh_vertices_co.extend(list(struct.unpack('3f', dekaron_mesh_file.read(12))))
            
            dekaron_mesh_file.seek(28, os.SEEK_CUR)
        
        dekaron_mesh_file_mesh_index_count = struct.unpack('<I', dekaron_mesh_file.read(4))[0]
        
        dekaron_mesh_file_mesh_indices = []
        
        for i in range(int(dekaron_mesh_file_mesh_index_count / 3)):
            dekaron_mesh_file_mesh_indices.extend(list(struct.unpack('<3H', dekaron_mesh_file.read(6))))
            dekaron_mesh_file_mesh_indices.append(0)
        
        if len(dekaron_mesh_file_mesh_name) <= 0:
            dekaron_mesh_file_mesh_name = os.path.splitext(os.path.basename(dekaron_mesh_file.name))[0]
        
        dekaron_mesh_file_mesh_bone_count = struct.unpack('<I', dekaron_mesh_file.read(4))[0]
        
        for i in range(dekaron_mesh_file_mesh_bone_count):
            dekaron_mesh_file_mesh_bone_name_length = struct.unpack('<I', dekaron_mesh_file.read(4))[0]
            
            dekaron_mesh_file_mesh_bone_name = dekaron_mesh_file.read(dekaron_mesh_file_mesh_bone_name_length)
        
        return create_dekaron_mesh(dekaron_mesh_file_mesh_name, \
                                   dekaron_mesh_file_mesh_vertices_co, \
                                   None, \
                                   dekaron_mesh_file_mesh_indices)
    
    
    def parse_dekaron_mesh_file_mesh3(dekaron_mesh_file):
        def is_face_degenerate(face_indices):
            if face_indices[0] == face_indices[1]:
                return True
            elif face_indices[0] == face_indices[2]:
                return True
            elif face_indices[1] == face_indices[2]:
                return True
            
            return False
        
        dekaron_mesh_file_mesh_aabb = struct.unpack('<6f', dekaron_mesh_file.read(24))[0]
        
        dekaron_mesh_file_mesh_unknown_count0 = struct.unpack('<I', dekaron_mesh_file.read(4))[0]
        
        dekaron_mesh_file_mesh_indices = []
        
        for i in range(dekaron_mesh_file_mesh_unknown_count0):
            dekaron_mesh_file_mesh_unknown0, \
            dekaron_mesh_file_mesh_unknown1, \
            dekaron_mesh_file_mesh_unknown2, \
            dekaron_mesh_file_mesh_unknown3, \
            dekaron_mesh_file_mesh_index_count = struct.unpack('<IIIII', dekaron_mesh_file.read(20))
            
            if dekaron_mesh_file_mesh_index_count > 0:
                face_indices = list(struct.unpack('<2H', dekaron_mesh_file.read(4)))
                
                for j in range(2, dekaron_mesh_file_mesh_index_count):
                    face_indices.append(struct.unpack('<H', dekaron_mesh_file.read(2))[0])
                    
                    face_indices_copy = face_indices[:]
                    
                    if not j % 2:
                        face_indices_copy.reverse()
                    
                    if is_face_degenerate(face_indices_copy) is False:
                        dekaron_mesh_file_mesh_indices.extend(face_indices_copy)
                        dekaron_mesh_file_mesh_indices.append(0)
                    
                    face_indices.pop(0)
        
        dekaron_mesh_file_mesh_vertex_count = struct.unpack('<I', dekaron_mesh_file.read(4))[0]
        
        dekaron_mesh_file_mesh_vertices_co = []
        dekaron_mesh_file_mesh_vertices_no = []
        dekaron_mesh_file_mesh_vertices_uv1 = []
        
        for i in range(dekaron_mesh_file_mesh_vertex_count):
            dekaron_mesh_file_mesh_vertices_co.extend(list(struct.unpack('3f', dekaron_mesh_file.read(12))))
            dekaron_mesh_file_mesh_vertices_no.extend(list(struct.unpack('3f', dekaron_mesh_file.read(12))))
            dekaron_mesh_file_mesh_vertex_uv1 = list(struct.unpack('2f', dekaron_mesh_file.read(8)))
            
            dekaron_mesh_file_mesh_vertex_uv1[1] = (1.0 - dekaron_mesh_file_mesh_vertex_uv1[1]) - 1.0
            
            dekaron_mesh_file_mesh_vertices_uv1.append(dekaron_mesh_file_mesh_vertex_uv1)
            
            dekaron_mesh_file.seek(24, os.SEEK_CUR)
        
        dekaron_mesh_file_mesh_bone_count = struct.unpack('<I', dekaron_mesh_file.read(4))[0]
        
        for i in range(dekaron_mesh_file_mesh_bone_count):
            dekaron_mesh_file_mesh_bone_name_length = struct.unpack('<I', dekaron_mesh_file.read(4))[0]
            
            dekaron_mesh_file_mesh_bone_name = dekaron_mesh_file.read(dekaron_mesh_file_mesh_bone_name_length)
        
        dekaron_mesh_file.seek(4, os.SEEK_CUR)
        
        return create_dekaron_mesh(os.path.splitext(os.path.basename(dekaron_mesh_file.name))[0], \
                                   dekaron_mesh_file_mesh_vertices_co, \
                                   dekaron_mesh_file_mesh_vertices_uv1, \
                                   dekaron_mesh_file_mesh_indices)
    
    
    def parse_dekaron_mesh_file_meshes(dekaron_mesh_file, dekaron_mesh_type):
        dekaron_mesh_file_mesh_count = struct.unpack('<I', dekaron_mesh_file.read(4))[0]
        
        dekaron_mesh_objects = []
        
        for i in range(dekaron_mesh_file_mesh_count):
            if dekaron_mesh_type == 0:
                dekaron_mesh_objects.append(parse_dekaron_mesh_file_mesh0(dekaron_mesh_file))
            elif dekaron_mesh_type == 3:
                dekaron_mesh_objects.append(parse_dekaron_mesh_file_mesh3(dekaron_mesh_file))
        
        return dekaron_mesh_objects
    
    
    def parse_dekaron_mesh_file(dekaron_mesh_file):
        dekaron_mesh_file_header_magic, \
        dekaron_mesh_file_header_unknown0, \
        dekaron_mesh_file_header_unknown1, \
        dekaron_mesh_file_header_unknown2, \
        dekaron_mesh_file_header_mesh_data_address0, \
        dekaron_mesh_file_header_mesh_data_size0, \
        dekaron_mesh_file_header_mesh_data_address1, \
        dekaron_mesh_file_header_mesh_data_size1, \
        dekaron_mesh_file_header_mesh_data_address2, \
        dekaron_mesh_file_header_mesh_data_size2, \
        dekaron_mesh_file_header_mesh_data_address3, \
        dekaron_mesh_file_header_mesh_data_size3 = struct.unpack('<IIIIIIIIIIII', dekaron_mesh_file.read(48))
        
        dekaron_mesh_file_header_work_path = dekaron_mesh_file.read(64)
        
        if dekaron_mesh_file_header_magic != 0x20031117:
            return
        
        dekaron_mesh_objects = []
        
        # Parse collision meshes
        if dekaron_mesh_file_header_mesh_data_size0 >= 4:
            dekaron_mesh_file.seek(dekaron_mesh_file_header_mesh_data_address0)
            
            dekaron_mesh_objects.extend(parse_dekaron_mesh_file_meshes(dekaron_mesh_file, 0))
        
        # Parse meshes
        if dekaron_mesh_file_header_mesh_data_size3 >= 4:
            dekaron_mesh_file.seek(dekaron_mesh_file_header_mesh_data_address3)
            
            dekaron_mesh_objects.extend(parse_dekaron_mesh_file_meshes(dekaron_mesh_file, 3))
        
        return dekaron_mesh_objects
    
    
    def import_dekaron_mesh_file_from_path(dekaron_mesh_file_path, context):   
    #    dekaron_mesh_scene = bpy.data.scenes.new()
    #    
    #    dekaron_mesh_scene.make_current()
    #    
        with io.open(dekaron_mesh_file_path, mode='r+b') as dekaron_mesh_file:
            dekaron_mesh_objects = parse_dekaron_mesh_file(dekaron_mesh_file)
            
            for dekaron_mesh_object in dekaron_mesh_objects:
                context.scene.objects.link(dekaron_mesh_object)
    
    
    class ImportDekaronMeshFile(bpy.types.Operator):
        '''Import Dekaron mesh'''
        
        bl_idname = "import_scene.dekaron_mesh"
        bl_label = 'Import Dekaron Mesh'
        
        path = bpy.props.StringProperty(name="File Path", description="Dekaron mesh file path", maxlen= 1024, default= "")
        
        def execute(self, context):
            import_dekaron_mesh_file_from_path(self.properties.path, context)
            
            return ('FINISHED',)
        
        def invoke(self, context, event):
            window_manager = context.manager
            
            window_manager.add_fileselect(self)
            
            return ('RUNNING_MODAL',)
    
    
    bpy.ops.add(ImportDekaronMeshFile)
    
    menu_function = lambda self, context: self.layout.operator(ImportDekaronMeshFile.bl_idname, text="Dekaron (.mesh)...")
    menu_item = dynamic_menu.add(bpy.types.INFO_MT_file_import, menu_function)


    ---------- Post added at 03:48 PM ---------- Previous post was at 03:46 PM ----------

    Data Unpacker:

    Usage:
    1. Open Command Prompt/Console/Terminal
    2. Type: C:\Python31\python.exe dekaron_unpacker.py --directory="<Dekaron_Directory>\data"


    License:
    Source:
    Copy and save as: dekaron_unpacker.py

    Code:
    # Copyright (c) 2009-2012 AJ
    # 
    # Permission is hereby granted, free of charge, to any person obtaining a copy
    # of this software and associated documentation files (the "Software"), to deal
    # in the Software without restriction, including without limitation the rights
    # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    # copies of the Software, and to permit persons to whom the Software is
    # furnished to do so, subject to the following conditions:
    # 
    # The above copyright notice and this permission notice shall be included in
    # all copies or substantial portions of the Software.
    # 
    # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    # THE SOFTWARE.
    
    
    import getopt
    import mmap
    import os
    import struct
    import sys
    
    
    def dekaron_unpack_parse_header_file(pack_header_file, pack_directory_name):
        pack_header_file.seek(400, os.SEEK_CUR)
        
        pack_chunk_file_count = struct.unpack('<I', pack_header_file.read(4))[0]
        
        for i in range(pack_chunk_file_count):
            pack_chunk_file_name_length = struct.unpack('<I', pack_header_file.read(4))[0]
            
            pack_chunk_file_name = pack_header_file.read(pack_chunk_file_name_length)
            
            pack_chunk_file_name = os.path.join(pack_directory_name, pack_chunk_file_name)
            
            with open(pack_chunk_file_name, 'r+b') as pack_chunk_file:
                pack_chunk_file_memory_map = mmap.mmap(pack_chunk_file.fileno(), 0)
                
                pack_chunk_file_directory_count = struct.unpack('<I', pack_header_file.read(4))[0]
                
                for b in xrange(pack_chunk_file_directory_count):
                    pack_chunk_file_directroy_name_length = struct.unpack('<I', pack_header_file.read(4))[0]
                    
                    pack_chunk_file_directroy_name = pack_header_file.read(pack_chunk_file_directroy_name_length)
                    
                    pack_chunk_file_directroy_name = os.path.join(pack_directory_name, pack_chunk_file_directroy_name)
                
                pack_chunk_file_size, \
                pack_chunk_file_unknown0, \
                pack_chunk_file_file_count, \
                pack_chunk_file_unknown1 = struct.unpack('<4I', pack_header_file.read(16))
                
                print("pack file: {0} ({1:X} bytes)".format(pack_chunk_file_name, pack_chunk_file_size))
                
                for j in range(pack_chunk_file_file_count):
                    pack_chunk_file_file_name_length = struct.unpack('<I', pack_header_file.read(4))[0]
                    
                    pack_chunk_file_file_name = pack_header_file.read(pack_chunk_file_file_name_length)
                    
                    pack_chunk_file_file_name = pack_chunk_file_file_name.replace('/', os.sep)
                    
                    pack_chunk_file_file_name = os.path.join(pack_directory_name, pack_chunk_file_file_name[6:])
                    
                    pack_chunk_file_file_pointer, \
                    pack_chunk_file_file_unknown0, \
                    pack_chunk_file_file_size, \
                    pack_chunk_file_file_unknown2, \
                    pack_chunk_file_file_unknown3 = struct.unpack('<5I', pack_header_file.read(20))
                    
                    pack_chunk_file_memory_map.seek(pack_chunk_file_file_pointer)
                    
                    pack_chunk_file_file_directory = os.path.dirname(pack_chunk_file_file_name)
                    
                    if os.path.exists(pack_chunk_file_file_directory) is False:
                        os.makedirs(pack_chunk_file_file_directory)
                        
                        print("created directory structure: {0}".format(pack_chunk_file_file_directory))
                    
                    with open(pack_chunk_file_file_name, 'w+b') as pack_chunk_file_file:
                        pack_chunk_file_file.write(pack_chunk_file_memory_map.read(pack_chunk_file_file_size))
                    
                    print("unpacked file: {0} ({1:X} bytes)".format(pack_chunk_file_file_name, pack_chunk_file_file_size))
                
                pack_chunk_file_memory_map.close()
    
    
    def dekaron_unpack(pack_directory_name):
        pack_header_file_name = os.path.join(pack_directory_name, 'pack.hdr')
        
        if os.path.exists(pack_directory_name) is False:
            return
        
        with open(pack_header_file_name, 'r+b') as pack_header_file:
            dekaron_unpack_parse_header_file(pack_header_file, pack_directory_name)
    
    
    def main(argv):
        pack_directory_name = None
        
        if len(argv) == 0:
            pack_directory_name = os.getcwd()
        elif len(argv) == 1:
            try:
                options, arguments = getopt.getopt(argv, 'd', ['directory='])
            except getopt.GetoptError:
                sys.exit(2)
            
            for option, argument in options:
                if option in ('d', '--directory'):
                    pack_directory_name = argument
        
        if pack_directory_name is None or \
           os.path.exists(pack_directory_name) is False or \
           os.path.isdir(pack_directory_name) is False:
                sys.exit(2)
        else:
            dekaron_unpack(pack_directory_name)
    
    
    if __name__ == "__main__":
        main(sys.argv[1:])
    Last edited by Phantom*; 22-11-14 at 04:06 AM. Reason: Fixed variable name in unpacker


  2. #2
    Ultimate Member mime is offline
    InactiveRank
    Mar 2007 Join Date
    195Posts

    Re: Blender 2.5 *.mesh Importer w/ Data Unpacker

    Good job. Thanks :)

  3. #3
    Account Inactive dbed is offline
    InactiveRank
    Jul 2004 Join Date
    BrasilLocation
    44Posts

    Re: Blender 2.5 *.mesh Importer w/ Data Unpacker

    nice guy, tkx

  4. #4
    Account Inactive stefano.aguiar is offline
    InactiveRank
    Jul 2008 Join Date
    20Posts

    Re: Blender 2.5 *.mesh Importer w/ Data Unpacker

    Very good friend now you could do for "Aion" it has many cool things
    is there any way to export to our friend "MilkShape 3D 1.8.2"

    would do the same system of Cabal? for the textures?

    friend as I can for the textures? I'm not getting any way could help?
    Last edited by stefano.aguiar; 13-02-10 at 04:59 PM.

  5. #5
    Account Inactive KlutcH is offline
    InactiveRank
    Mar 2010 Join Date
    1Posts

    Re: Blender 2.5 *.mesh Importer w/ Data Unpacker

    Hi I did as you said but am having problems loading the script...It says line 34 Import error no module named dynamic_menu, any way to fix this?

  6. #6
    Account Inactive pixellegolas is offline
    InactiveRank
    Feb 2009 Join Date
    11Posts

    Re: Blender 2.5 *.mesh Importer w/ Data Unpacker

    I don't have dekaron but I will probably get it because of this, thanks. By the way, is it bones etc or just model?

  7. #7
    Account Inactive javas is offline
    InactiveRank
    Jan 2005 Join Date
    BrazilLocation
    418Posts

    Re: Blender 2.5 *.mesh Importer w/ Data Unpacker

    Very nice work Phantom*,thx a lot man :)
    Btw can you create a Exporter script (.3ds) too for blender please ?
    Thx in advanced.

  8. #8
    Member DJPittbull is offline
    MemberRank
    Mar 2007 Join Date
    43Posts

    Re: Blender 2.5 *.mesh Importer w/ Data Unpacker

    Heey can some 1 help me.. ??

    ive download this Blender 2.5 Alpha 2 cant find any blender 2.5 alpha 0 :S

    can some 1 give me direct download links for this and how to i add the source..??
    just open new txt file and save it as what u said ?? and if i press file import it doesnt show dekaron.mesh pls i want this i need help!!

    THNX IN ADVANCE

    ---------- Post added at 04:00 PM ---------- Previous post was at 03:35 PM ----------

    nvm i found an alpha 0 ;)

  9. #9
    Account Inactive xXCreationzXx is offline
    InactiveRank
    Apr 2010 Join Date
    2Posts

    Re: Blender 2.5 *.mesh Importer w/ Data Unpacker

    mmmmk this all worked great an all but i have a little problem i copied the import script pasted it in notepad saved it as .py installed blender 2.5 alpha 0 an python 3.1.4 i imported it an it worked but i get the following lines

    RNA_string_set: IMPORT_SCENE_OT_dekaron_mesh.filename not found
    RNA_string_set: IMPORT_SCENE_OT_dekaron_mesh.directory not found
    Poll
    Poll

    but it still imports then when i try to export to 3ds format (need to import it into 3ds max) i get this

    RNA_string_set: EXPORT_OT_autodesk_3ds.filename not found
    RNA_string_set: EXPORT_OT_autodesk_3ds.directory not found
    3ds export time 0.03

    it exports but when i try to open it with 3ds max it says its an invalid file any help? thanks.

  10. #10
    Newbie Plantonix is offline
    MemberRank
    Nov 2011 Join Date
    1Posts

    Re: Blender 2.5 *.mesh Importer w/ Data Unpacker

    Like! :D

  11. #11
    Newbie yarin1115 is offline
    MemberRank
    May 2012 Join Date
    2Posts

    Re: Blender 2.5 *.mesh Importer w/ Data Unpacker

    I dont know how do this.

    Data Unpacker:

    Usage:
    Open Command Prompt/Console/Terminal
    Type: C:\Python31\python.exe dekaron_unpacker.py --directory="<Dekaron_Directory>\data\"

    I tired some hours I dont know how do this, Please Help.

  12. #12
    Account Inactive rasty is offline
    InactiveRank
    May 2013 Join Date
    1Posts

    Re: Blender 2.5 *.mesh Importer w/ Data Unpacker

    Hello, can anyone teach me how i can do that? please i need to make .mesh to .obj

    please thanks !



Advertisement