Ejemplo n.º 1
0
def export_xrefs(file, selected_only):
    # build list of xrefs to export
    xref_objects = []
    for obj in bpy.data.objects:
        if obj.name.startswith("xref:"):
            if (selected_only and obj
                    in bpy.context.selected_objects) or not selected_only:
                xref_objects.append(obj)

    # export xrefs
    if len(xref_objects) > 0:
        bin.write_file_header(file, "xrefs")
        num_xrefs = 0
        xref_num_offset = file.tell()
        file.write(struct.pack('L', 0))
        for obj in xref_objects:
            num_xrefs += 1
            #write matrix
            bin.write_matrix3x4(file, obj.matrix_basis)

            # write xref name
            xref_name = get_undupe_name(obj.name[5:]) + "\x00max"
            null_length = 32 - len(xref_name)

            file.write(bytes(xref_name, 'utf-8'))
            file.write(bytes('\x00' * null_length, 'utf-8'))

        file_length = file.tell() - xref_num_offset
        file.seek(xref_num_offset - 4, 0)
        file.write(struct.pack('LL', file_length, num_xrefs))
        file.seek(0, 2)
    else:
        return
Ejemplo n.º 2
0
def export_xrefs(file, selected_only):
    # build list of xrefs to export
    xref_objects = []
    for obj in bpy.data.objects:
        if obj.name.startswith("xref:"):
            if (selected_only and obj in bpy.context.selected_objects) or not selected_only:
                xref_objects.append(obj)

    # export xrefs
    if len(xref_objects) > 0:
        bin.write_file_header(file, "xrefs")
        num_xrefs = 0
        xref_num_offset = file.tell()
        file.write(struct.pack('L', 0))
        for obj in xref_objects:
            num_xrefs += 1
            #write matrix
            bin.write_matrix3x4(file, obj.matrix_basis)
           
            # write xref name
            xref_name = get_undupe_name(obj.name[5:]) + "\x00max"
            null_length = 32 - len(xref_name)
            
            file.write(bytes(xref_name, 'utf-8'))
            file.write(bytes('\x00' * null_length, 'utf-8'))
                
        file_length = file.tell() - xref_num_offset
        file.seek(xref_num_offset - 4, 0)
        file.write(struct.pack('LL', file_length, num_xrefs))
        file.seek(0, 2)
    else:
        return
Ejemplo n.º 3
0
def export_shaders(file, replace_words, materials, type="byte"):
    # First paintjob replace word. If this isn't added we get paintjobs-1 paintjobs :(
    replace_words.insert(0, ['$!*%&INVALIDMATERIAL&%*!$', '$!*%&INVALIDMATERIAL&%*!$'])
    # write file header and record offset
    bin.write_file_header(file, "shaders")
    shaders_data_offset = file.tell()
    # prepare shaders header
    shadertype_raw = len(replace_words)
    if type == "byte":
        shadertype_raw += 128
    shaders_per_paintjob = len(materials)
    # write header
    file.write(struct.pack('LL', shadertype_raw, shaders_per_paintjob))
    # write material sets
    for rwa in replace_words:
        # export a material set
        for mtl in materials:
            #bname = mtl.name
            
            #if mtl.active_texture is not None:
            #    bname = mtl.active_texture.name  # use texture name instead
            
            # handle material name replacement
            mtl_name = handle_replace_logic(rwa[0], rwa[1], get_undupe_name(mtl.name))
            
            if mtl_name.startswith('mm2:notexture') or mtl_name.startswith('age:notexture'):
                # matte material
                print("Material " + mtl_name + " is a matte material")
                for val in range(3):
                  print("diffuse color [" + str(val) + "]=" + str(mtl.diffuse_color[val]))
                bin.write_angel_string(file, '')
            else:
                # has texture
                bin.write_angel_string(file, mtl_name)

            # calculate alpha for writing
            mtl_alpha = 1
            if mtl.use_transparency:
                mtl_alpha = mtl.alpha

            if type == "byte":
                bin.write_color4d(file, mtl.diffuse_color, mtl_alpha)
                bin.write_color4d(file, mtl.diffuse_color, mtl_alpha)
                bin.write_color4d(file, mtl.specular_color)
            elif type == "float":
                bin.write_color4f(file, mtl.diffuse_color, mtl_alpha)
                bin.write_color4f(file, mtl.diffuse_color, mtl_alpha)
                bin.write_color4f(file, mtl.specular_color)
                # ????
                file.write(struct.pack('ffff', 0, 0, 0, 1))

            # shininess
            file.write(struct.pack('f', mtl.raytrace_mirror.reflect_factor))

    # write file length
    shaders_file_length = file.tell() - shaders_data_offset
    file.seek(shaders_data_offset - 4)
    file.write(struct.pack('L', shaders_file_length))
    file.seek(0, 2)
Ejemplo n.º 4
0
def export_meshes(file, meshlist, options):
    for obj in meshlist:
        # write FILE header for mesh name
        bin.write_file_header(file, get_undupe_name(obj.name))
        file_data_start_offset = file.tell()
        
        # create temp mesh
        temp_mesh = obj.to_mesh(bpy.context.scene, apply_modifiers_G, 'PREVIEW')
        
        # get bmesh
        bm = bmesh.new()
        bm.from_mesh(temp_mesh)
        bm_tris = bm.calc_tessface()
        
        # get mesh infos
        export_mats = helper.get_used_materials(obj, apply_modifiers_G)
        total_verts = len(bm.verts)
        total_faces = int(len(bm_tris) * 3)
        num_sections = len(export_mats)
        
        #build FVF
        FVF_FLAGS = FVF(("D3DFVF_XYZ", "D3DFVF_NORMAL", "D3DFVF_TEX1"))
        for mat in obj.data.materials:
            if mat is not None and mat.use_shadeless:
                # undo the previous flag since we arent
                # going to write normals
                FVF_FLAGS.clear_flag("D3DFVF_NORMAL")
                break
        if "VC_DIFFUSE" in options:
            FVF_FLAGS.set_flag("D3DFVF_DIFFUSE")
        if "VC_SPECULAR" in options:
            FVF_FLAGS.set_flag("D3DFVF_SPECULAR")

        # do we need a matrix file. Only for H object
        if ((obj.location[0] != 0 or obj.location[1] != 0 or obj.location[2] != 0) and obj.name.upper().endswith("_H")):
            helper.write_matrix(obj.name, obj, pkg_path)

        # write mesh data header
        file.write(struct.pack('LLLLL', num_sections, total_verts, total_faces, num_sections, FVF_FLAGS.value))

        # write sections
        cur_material_index = -1
        for mat_slot in obj.material_slots:
            # are we exporting this material?
            cur_material_index += 1
            if not cur_material_index in export_mats:
              continue
        
            # build the mesh data we need
            cmtl_indices, cmtl_verts, cmtl_uvs, cmtl_cols = helper.prepare_mesh_data(bm, cur_material_index, bm_tris)

            # mesh remap done. we will now write our strip
            num_strips = 1
            section_flags = 0
            shader_offset = material_remap_table[helper.get_material_offset(obj.material_slots[cur_material_index].material)]
            
            # write strip to file
            file.write(struct.pack('HHL', num_strips, section_flags, shader_offset))
            strip_primType = 3
            strip_vertices = len(cmtl_verts)
            file.write(struct.pack('LL', strip_primType, strip_vertices))
            
            # write vertices
            for cv in range(len(cmtl_verts)):
                export_vert = cmtl_verts[cv]
                export_vert_location = (obj.matrix_world * export_vert.co) - obj.location
                bin.write_float3(file, (export_vert_location[0], export_vert_location[2], export_vert_location[1] * -1))
                if FVF_FLAGS.has_flag("D3DFVF_NORMAL"):
                    bin.write_float3(file, (export_vert.normal[0], export_vert.normal[2], export_vert.normal[1] * -1))
                if FVF_FLAGS.has_flag("D3DFVF_DIFFUSE"):
                    bin.write_color4d(file, cmtl_cols[cv])
                if FVF_FLAGS.has_flag("D3DFVF_SPECULAR"):
                    bin.write_color4d(file, cmtl_cols[cv])
                uv_data = cmtl_uvs[cv]
                bin.write_float2(file, (uv_data[0], (uv_data[1] - 1) * -1))
            
            # write indices
            strip_indices_len = int(len(cmtl_indices) * 3)
            file.write(struct.pack('L', strip_indices_len))
            for ply in cmtl_indices:
                file.write(struct.pack('HHH', ply[0], ply[1], ply[2]))
        
        # clean up temp_mesh
        bpy.data.meshes.remove(temp_mesh)
        bm.free()
		
        # write FILE length
        file_data_length = file.tell() - file_data_start_offset
        file.seek(file_data_start_offset - 4)
        file.write(struct.pack('L', file_data_length))
        file.seek(0, 2)
Ejemplo n.º 5
0
def export_offset(file):
    bin.write_file_header(file, "offset", 12)
    file.write(struct.pack('fff', 0, 0, 0))
Ejemplo n.º 6
0
def export_meshes(file, meshlist, options):
    for obj in meshlist:
        # write FILE header for mesh name
        bin.write_file_header(file, get_undupe_name(obj.name))
        file_data_start_offset = file.tell()

        # create temp mesh
        temp_mesh = obj.to_mesh(bpy.context.scene, apply_modifiers_G,
                                'PREVIEW')

        # get bmesh
        bm = bmesh.new()
        bm.from_mesh(temp_mesh)
        bm_tris = bm.calc_tessface()

        # get mesh infos
        export_mats = helper.get_used_materials(obj, apply_modifiers_G)
        total_verts = len(bm.verts)
        total_faces = int(len(bm_tris) * 3)
        num_sections = len(export_mats)

        #build FVF
        FVF_FLAGS = FVF(("D3DFVF_XYZ", "D3DFVF_NORMAL", "D3DFVF_TEX1"))
        for mat in obj.data.materials:
            if mat is not None and mat.use_shadeless:
                # undo the previous flag since we arent
                # going to write normals
                FVF_FLAGS.clear_flag("D3DFVF_NORMAL")
                break
        if "VC_DIFFUSE" in options:
            FVF_FLAGS.set_flag("D3DFVF_DIFFUSE")
        if "VC_SPECULAR" in options:
            FVF_FLAGS.set_flag("D3DFVF_SPECULAR")

        # do we need a matrix file. Only for H object
        if ((obj.location[0] != 0 or obj.location[1] != 0
             or obj.location[2] != 0) and obj.name.upper().endswith("_H")):
            helper.write_matrix(obj.name, obj, pkg_path)

        # write mesh data header
        file.write(
            struct.pack('LLLLL', num_sections, total_verts, total_faces,
                        num_sections, FVF_FLAGS.value))

        # write sections
        cur_material_index = -1
        for mat_slot in obj.material_slots:
            # are we exporting this material?
            cur_material_index += 1
            if not cur_material_index in export_mats:
                continue

            # build the mesh data we need
            cmtl_indices, cmtl_verts, cmtl_uvs, cmtl_cols = helper.prepare_mesh_data(
                bm, cur_material_index, bm_tris)

            # mesh remap done. we will now write our strip
            num_strips = 1
            section_flags = 0
            shader_offset = material_remap_table[helper.get_material_offset(
                obj.material_slots[cur_material_index].material)]

            # write strip to file
            file.write(
                struct.pack('HHL', num_strips, section_flags, shader_offset))
            strip_primType = 3
            strip_vertices = len(cmtl_verts)
            file.write(struct.pack('LL', strip_primType, strip_vertices))

            # write vertices
            for cv in range(len(cmtl_verts)):
                export_vert = cmtl_verts[cv]
                export_vert_location = (obj.matrix_world *
                                        export_vert.co) - obj.location
                bin.write_float3(
                    file, (export_vert_location[0], export_vert_location[2],
                           export_vert_location[1] * -1))
                if FVF_FLAGS.has_flag("D3DFVF_NORMAL"):
                    bin.write_float3(
                        file, (export_vert.normal[0], export_vert.normal[2],
                               export_vert.normal[1] * -1))
                if FVF_FLAGS.has_flag("D3DFVF_DIFFUSE"):
                    bin.write_color4d(file, cmtl_cols[cv])
                if FVF_FLAGS.has_flag("D3DFVF_SPECULAR"):
                    bin.write_color4d(file, cmtl_cols[cv])
                uv_data = cmtl_uvs[cv]
                bin.write_float2(file, (uv_data[0], (uv_data[1] - 1) * -1))

            # write indices
            strip_indices_len = int(len(cmtl_indices) * 3)
            file.write(struct.pack('L', strip_indices_len))
            for ply in cmtl_indices:
                file.write(struct.pack('HHH', ply[0], ply[1], ply[2]))

        # clean up temp_mesh
        bpy.data.meshes.remove(temp_mesh)
        bm.free()

        # write FILE length
        file_data_length = file.tell() - file_data_start_offset
        file.seek(file_data_start_offset - 4)
        file.write(struct.pack('L', file_data_length))
        file.seek(0, 2)
Ejemplo n.º 7
0
def export_shaders(file, replace_words, materials, type="byte"):
    # First paintjob replace word. If this isn't added we get paintjobs-1 paintjobs :(
    replace_words.insert(
        0, ['$!*%&INVALIDMATERIAL&%*!$', '$!*%&INVALIDMATERIAL&%*!$'])
    # write file header and record offset
    bin.write_file_header(file, "shaders")
    shaders_data_offset = file.tell()
    # prepare shaders header
    shadertype_raw = len(replace_words)
    if type == "byte":
        shadertype_raw += 128
    shaders_per_paintjob = len(materials)
    # write header
    file.write(struct.pack('LL', shadertype_raw, shaders_per_paintjob))
    # write material sets
    for rwa in replace_words:
        # export a material set
        for mtl in materials:
            #bname = mtl.name

            #if mtl.active_texture is not None:
            #    bname = mtl.active_texture.name  # use texture name instead

            # handle material name replacement
            mtl_name = handle_replace_logic(rwa[0], rwa[1],
                                            get_undupe_name(mtl.name))

            if mtl_name.startswith('mm2:notexture') or mtl_name.startswith(
                    'age:notexture'):
                # matte material
                print("Material " + mtl_name + " is a matte material")
                for val in range(3):
                    print("diffuse color [" + str(val) + "]=" +
                          str(mtl.diffuse_color[val]))
                bin.write_angel_string(file, '')
            else:
                # has texture
                bin.write_angel_string(file, mtl_name)

            # calculate alpha for writing
            mtl_alpha = 1
            if mtl.use_transparency:
                mtl_alpha = mtl.alpha

            if type == "byte":
                bin.write_color4d(file, mtl.diffuse_color, mtl_alpha)
                bin.write_color4d(file, mtl.diffuse_color, mtl_alpha)
                bin.write_color4d(file, mtl.specular_color)
            elif type == "float":
                bin.write_color4f(file, mtl.diffuse_color, mtl_alpha)
                bin.write_color4f(file, mtl.diffuse_color, mtl_alpha)
                bin.write_color4f(file, mtl.specular_color)
                # ????
                file.write(struct.pack('ffff', 0, 0, 0, 1))

            # shininess
            file.write(struct.pack('f', mtl.raytrace_mirror.reflect_factor))

    # write file length
    shaders_file_length = file.tell() - shaders_data_offset
    file.seek(shaders_data_offset - 4)
    file.write(struct.pack('L', shaders_file_length))
    file.seek(0, 2)
Ejemplo n.º 8
0
def export_offset(file):
    bin.write_file_header(file, "offset", 12)
    file.write(struct.pack('fff', 0, 0, 0))
Ejemplo n.º 9
0
def export_geometry(file, meshlist, options):
    for obj in meshlist:
        # write FILE header for mesh name
        bin.write_file_header(file, helper.get_undupe_name(obj.name))
        file_data_start_offset = file.tell()

        # don't multiply vertices for objects requring a matrix3x4 export
        premultiply_vertices = not helper.is_matrix_object(obj)

        # create temp mesh
        temp_mesh = None
        if "MODIFIERS" in options:
            dg = bpy.context.evaluated_depsgraph_get()
            eval_obj = obj.evaluated_get(dg)
            temp_mesh = eval_obj.to_mesh()
        else:
            temp_mesh = obj.to_mesh()

        # get bmesh
        bm = bmesh.new()
        bm.from_mesh(temp_mesh)
        bm_tris = bm.calc_loop_triangles()

        # get mesh infos
        export_mats = export_helper.get_used_materials(obj, "MODIFIERS"
                                                       in options)
        total_verts = len(bm.verts)
        total_faces = int(len(bm_tris) * 3)
        num_sections = len(export_mats)

        # debug
        #print("Exporting object "  + str(obj.name) + ". total_verts=" + str(total_verts) + ", total_faces=" + str(total_faces) +", num_sections=" + str(num_sections))

        #build FVF
        FVF_FLAGS = FVF(("D3DFVF_XYZ", "D3DFVF_NORMAL", "D3DFVF_TEX1"))
        for mat in obj.data.materials:
            if mat is not None and export_helper.is_mat_shadeless(mat):
                # undo the previous flag since we arent
                # going to write normals
                FVF_FLAGS.clear_flag("D3DFVF_NORMAL")
                break
        if "VC_DIFFUSE" in options:
            FVF_FLAGS.set_flag("D3DFVF_DIFFUSE")
        if "VC_SPECULAR" in options:
            FVF_FLAGS.set_flag("D3DFVF_SPECULAR")

        # do we need a matrix file? Only for H object
        obj_offcenter = abs(obj.location[0]) > 0.001 or abs(
            obj.location[1]) > 0.001 or abs(obj.location[2]) > 0.001
        if obj_offcenter and obj.name.upper().endswith("_H"):
            export_helper.write_matrix(obj.name, obj, pkg_path)

        # write mesh data header
        file.write(
            struct.pack('LLLLL', num_sections, total_verts, total_faces,
                        num_sections, FVF_FLAGS.value))

        # write sections
        cur_material_index = -1
        for material in obj.data.materials:
            # get non cloned material
            real_material = material
            if material.cloned_from is not None:
                real_material = material.cloned_from

            # are we exporting this material?
            cur_material_index += 1
            if not real_material in export_mats:
                continue

            # build the mesh data we need
            cmtl_indices, cmtl_verts, cmtl_uvs, cmtl_cols = export_helper.prepare_mesh_data(
                bm, cur_material_index, bm_tris)

            # mesh remap done. we will now write our strip
            num_strips = 1
            section_flags = 0
            shader_offset = material_remap_table[real_material.name]

            # write strip to file
            file.write(
                struct.pack('HHL', num_strips, section_flags, shader_offset))
            strip_primType = 3
            strip_vertices = len(cmtl_verts)
            file.write(struct.pack('LL', strip_primType, strip_vertices))

            # write vertices
            for cv in range(len(cmtl_verts)):
                export_vert = cmtl_verts[cv]
                export_vert_location = (
                    (obj.matrix_world @ export_vert.co) -
                    obj.location) if premultiply_vertices else export_vert.co
                bin.write_float3(
                    file, helper.convert_vecspace_to_mm2(export_vert_location))
                if FVF_FLAGS.has_flag("D3DFVF_NORMAL"):
                    bin.write_float3(
                        file,
                        helper.convert_vecspace_to_mm2(export_vert.normal))
                if FVF_FLAGS.has_flag("D3DFVF_DIFFUSE"):
                    bin.write_color4d(file, cmtl_cols[cv])
                if FVF_FLAGS.has_flag("D3DFVF_SPECULAR"):
                    bin.write_color4d(file, cmtl_cols[cv])
                uv_data = cmtl_uvs[cv]
                bin.write_float2(file, (uv_data[0], (uv_data[1] - 1) * -1))

            # write indices
            strip_indices_len = int(len(cmtl_indices) * 3)
            file.write(struct.pack('L', strip_indices_len))
            for ply in cmtl_indices:
                file.write(struct.pack('HHH', ply[0], ply[1], ply[2]))

        # clean up temp_mesh
        bm.free()

        # write FILE length
        file_data_length = file.tell() - file_data_start_offset
        file.seek(file_data_start_offset - 4)
        file.write(struct.pack('L', file_data_length))
        file.seek(0, 2)
Ejemplo n.º 10
0
def export_shaders(file, context, type="byte"):
    # get our tool
    scene = context.scene
    angel = scene.angel

    variants = angel.variants
    num_variants = len(variants)
    write_dummy_variant = False

    if num_variants == 0:
        # Write a 'dummy' variant, created with the used materials
        write_dummy_variant = True
        num_variants = 1

    # write file header and record offset
    bin.write_file_header(file, "shaders")
    shaders_data_offset = file.tell()

    # prepare shaders header
    shadertype_raw = num_variants
    if type == "byte":
        shadertype_raw += 128
    shaders_per_paintjob = len(material_remap_table)

    # write header
    file.write(struct.pack('LL', shadertype_raw, shaders_per_paintjob))

    # write material sets
    ordered_material_remap = sorted(material_remap_table.items(),
                                    key=lambda x: x[1])
    if write_dummy_variant:
        # write out a variant generated on the fly with the material remap
        for material_kvp in ordered_material_remap:
            material_id = material_kvp[0]
            material = bpy.data.materials[material_id]

            # create a shader for it, and write it
            shader = export_helper.create_shader_from_material(material)
            shader.write(file, type)
    else:
        # write out user created variants
        for variant in variants:
            for material_kvp in ordered_material_remap:
                material_id = material_kvp[0]
                material = bpy.data.materials[material_id]

                # find this material in the variant
                for vm in variant.materials:
                    if vm.material.cloned_from is not None and vm.material.cloned_from == material:
                        material = vm.material
                        break

                # create a shader for it, and write it
                shader = export_helper.create_shader_from_material(material)
                shader.write(file, type)

    # write file length
    shaders_file_length = file.tell() - shaders_data_offset
    file.seek(shaders_data_offset - 4)
    file.write(struct.pack('L', shaders_file_length))
    file.seek(0, 2)