Пример #1
0
def _fill_piece_sections_7(root_object, object_list, bone_list, scene, vg_list,
                           used_materials, offset_matrix, scs_globals,
                           output_type):
    """
    Fills up "Piece" sections for file version 7 (exchange format).
    :param object_list:
    :param bone_list:
    :param scene:
    :param vg_list:
    :param used_materials:
    :param offset_matrix:
    :return:
    """
    piece_sections = []  # container for all "Pieces"
    global_vertex_count = 0
    global_face_count = 0
    global_edge_count = 0
    piece_index_obj = {}
    skin_list = []
    skin_weights_cnt = 0
    skin_clones_cnt = 0
    for piece_index, obj in enumerate(object_list):
        mat_world = obj.matrix_world
        piece_index_obj[piece_index] = obj
        object_materials = _object_utils.get_object_materials(
            obj)  # get object materials

        # Get Object's Mesh data and list of temporarily disabled "Edge Split" Modifiers...
        mesh = _object_utils.get_mesh(obj)

        # VERTICES
        # TEMPORAL VERTEX STREAM DATA FORMAT:
        # example: ('_POSITION', [(0.0, 0.0, 0.0), (0.0, 0.0, 0.0), ...])
        # example: ('_SCALAR', [(0.0), (0.0), ...])

        stream_pos = ('_POSITION', [])
        # stream_nor = ('_NORMAL', [])
        # if scs_globals.export_vertex_groups:
        vg_layers_for_export, streams_vg = _object_utils.get_stream_vgs(
            obj)  # get Vertex Group layers (SCALARs)

        vertex_stream_count = 1
        vertex_streams = []
        stream_vg_container = []
        # print('bone_list: %s' % str(bone_list.keys))
        for vert_i, vert in enumerate(mesh.vertices):
            position = offset_matrix.inverted() * mesh.vertices[vert_i].co
            # scs_position = io_utils.change_to_scs_xyz_coordinates(mat_world * position, scs_globals.export_scale) ## POSITION
            scs_position = Matrix.Scale(
                scs_globals.export_scale,
                4) * _convert_utils.scs_to_blend_matrix().inverted(
                ) * mat_world * position  # POSITION
            stream_pos[1].append(scs_position)
            # stream_nor[1].append(io_utils.get_vertex_normal(mesh, vert_i))              # NORMAL
            if scs_globals.export_vertex_groups:
                if streams_vg:
                    vg_i = 0
                    for vg in vg_layers_for_export:  # weights (even unused) all vertices become 0.0
                        if vg.name in vg_list:
                            vg_weight = (_object_utils.get_vertex_group(
                                vg, vert_i), )  # SCALARs
                            key = str("_SCALAR" + str(vg_i))
                            if vg_i == len(stream_vg_container) and len(
                                    stream_vg_container) != len(
                                        vg_layers_for_export):
                                stream_vg_container.append(
                                    (vg.name, key, [vg_weight]))
                            else:
                                stream_vg_container[vg_i][2].append(vg_weight)
                            vg_i += 1

                            # SKINNING (OLD STYLE FOR PIM VER. 7)
            # if scs_globals.export_anim_file == 'anim':
            if root_object.scs_props.scs_root_animated == 'anim':
                skin_vector = scs_position  # NOTE: Vertex position - from Maya scaled *10 (old & unused in game engine)
                skin_weights = []
                for group in vert.groups:
                    for vg in vg_layers_for_export:
                        if vg.index == group.group:
                            for bone_i, bone in enumerate(bone_list):
                                if vg.name == bone.name:
                                    skin_weights.append((bone_i, group.weight))
                                    skin_weights_cnt += 1
                                    # print('vert: %i - group: %r (%i) - %s' % (vert_i, vg.name, bone_i, str(group.weight)))
                                    break
                            break
                skin_clones = ((piece_index, vert_i), )
                skin_clones_cnt += 1
                skin_list.append((skin_vector, skin_weights, skin_clones))

                # ##
        vertex_streams.append(stream_pos)
        # print('\nstream_pos:\n  %s' % str(stream_pos))
        # vertex_streams.append(stream_nor)
        # print('\nstream_nor:\n  %s' % str(stream_nor))
        for vg_stream in stream_vg_container:
            vertex_stream_count += 1
            vertex_streams.append(vg_stream)
            # print('\nvg_stream:\n  %s' % str(vg_stream))
        # FACES
        # TEMPORAL FACE STREAM DATA FORMAT:
        # faces = [face_data, face_data, ...]
        # face_data = (material, [vertex indices], [face-vertex streams])
        # face_streams = [('_UV0', [(0.0, 0.0), (0.0, 0.0), ...]), ...]
        # example: [(0, [0, 1, 2], [('_UV0', [(0.0, 0.0), (0.0, 0.0)]), ('_UV0', [(0.0, 0.0), (0.0, 0.0)])]), (), ...]

        faces = []
        face_cnt = 0
        uv_layers_exists = 1
        rgb_layers_exists = 1
        # print('used_materials: %s' % str(used_materials))
        for face_i, face in enumerate(mesh.polygons):
            face_cnt += 1
            streams_uv = None
            streams_vcolor = None
            if uv_layers_exists:
                requested_uv_layers, streams_uv = _mesh_utils.get_stream_uvs(
                    mesh, scs_globals.active_uv_only)  # get UV layers (UVs)
                if not streams_uv:
                    uv_layers_exists = 0
            if rgb_layers_exists and scs_globals.export_vertex_color:
                if scs_globals.export_vertex_color_type_7 == 'rgb':
                    rgb_all_layers, streams_vcolor = _mesh_utils.get_stream_rgb(
                        mesh, output_type,
                        False)  # get Vertex Color layers (RGB)
                elif scs_globals.export_vertex_color_type_7 == 'rgbda':
                    rgb_all_layers, streams_vcolor = _mesh_utils.get_stream_rgb(
                        mesh, output_type, True)  # get Vertex Color layers (
                    # RGBdA)
                else:
                    streams_vcolor = None  # TODO: Alpha from another layer
                if not streams_vcolor:
                    rgb_layers_exists = 0
            mat_index = used_materials.index(
                object_materials[face.material_index])
            # print('face-mat_index: %s; object_materials[f-m_i]: %s; used_materials.index(o_m[f-m_i]): %s' % (face.material_index,
            # object_materials[face.material_index], used_materials.index(object_materials[face.material_index])))
            face_verts = []
            for vert in face.vertices:
                face_verts.append(vert)
            face_verts = face_verts[::-1]  # revert vertex order in face
            # print('face_verts: %s' % str(face_verts))
            face_streams = []
            stream_fv_nor = ("_NORMAL", [])
            stream_fv_uv_container = []
            stream_fv_rgb_container = []
            stream_names = {}
            for loop_index in range(face.loop_start,
                                    face.loop_start + face.loop_total):
                # edge_index = mesh.loops[loop_index].edge_index
                vert_index = mesh.loops[loop_index].vertex_index
                # print('face i.: %s\tloop i.: %s\tedge i.: %s\tvert i.: %s' % (face_i, loop_index, edge_index, vert_index))
                # Normals
                stream_fv_nor[1].append(
                    offset_matrix.inverted() *
                    Vector(_mesh_utils.get_vertex_normal(mesh, vert_index)))
                # UV Layers
                if streams_uv:
                    for uv_i, uv_l in enumerate(requested_uv_layers):
                        uv_values = _mesh_utils.get_face_vertex_uv(
                            uv_l.data, loop_index, uv_i)
                        key = str("_UV" + str(uv_i))
                        if uv_i == len(stream_fv_uv_container
                                       ) and len(stream_fv_uv_container
                                                 ) != len(requested_uv_layers):
                            stream_fv_uv_container.append((key, [uv_values]))
                            stream_names[key] = uv_l.name
                        else:
                            stream_fv_uv_container[uv_i][1].append(uv_values)
                            # Vertex Color Layers (RGB)
                if scs_globals.export_vertex_color:
                    if streams_vcolor:
                        for rgb_i, rgb_l in enumerate(rgb_all_layers):
                            if scs_globals.export_vertex_color_type_7 == 'rgb':
                                rgb_values = _mesh_utils.get_face_vertex_color(
                                    rgb_l.data, loop_index, False, rgb_i)
                                key = str("_RGB" + str(rgb_i))
                            elif scs_globals.export_vertex_color_type_7 == 'rgbda':
                                rgb_values = _mesh_utils.get_face_vertex_color(
                                    rgb_l.data, loop_index, True, rgb_i)
                                key = str("_RGBA" + str(rgb_i))
                            else:
                                streams_vcolor = None  # TODO: Alpha from another layer
                            if rgb_i == len(stream_fv_rgb_container
                                            ) and len(stream_fv_rgb_container
                                                      ) != len(rgb_all_layers):
                                stream_fv_rgb_container.append(
                                    (key, [rgb_values]))
                                stream_names[key] = rgb_l.name
                            else:
                                stream_fv_rgb_container[rgb_i][1].append(
                                    rgb_values)
                                # Data Assembling
            face_streams.append(stream_fv_nor)
            for stream in stream_fv_uv_container:
                face_streams.append(stream)
            for stream in stream_fv_rgb_container:
                face_streams.append(stream)
            face_data = (mat_index, face_verts, face_streams)
            faces.append(face_data)

        # SHARP EDGES
        sharp_edges = []
        for edge in mesh.edges:
            if edge.use_edge_sharp:
                sharp_edges.append(edge.vertices[:])

        # BUILD FACE SECTION
        faces_container = _SectionData("Faces")
        faces_container.props.append(("StreamCount", len(faces[0][2])))
        for face_i, face_data in enumerate(faces):
            face_container = _SectionData("Face")
            face_container.props.append(("Index", face_i))
            face_container.props.append(("Material", face_data[0]))
            face_container.props.append(("Indices", face_data[1]))
            for stream in face_data[2]:
                if stream[0] in stream_names:
                    face_container.sections.append(
                        _pix_container.make_vertex_stream(
                            stream, stream_names[stream[0]]))
                else:
                    face_container.sections.append(
                        _pix_container.make_vertex_stream(stream))
            faces_container.sections.append(face_container)

        # BUILD PIECE SECTION
        piece_section = _SectionData("Piece")
        piece_section.props.append(("Index", piece_index))
        global_vertex_count += len(stream_pos[1])
        piece_section.props.append(("VertexCount", len(stream_pos[1])))
        global_face_count += face_cnt
        piece_section.props.append(("FaceCount", face_cnt))
        global_edge_count += len(sharp_edges)
        piece_section.props.append(("EdgeCount", len(sharp_edges)))
        piece_section.props.append(("StreamCount", vertex_stream_count))
        piece_section.props.append(("", ""))
        # vertex streams...
        for stream in vertex_streams:
            if len(stream) == 3:
                # print('\nstream:\n  %s' % str(stream))
                piece_section.sections.append(
                    _pix_container.make_vertex_stream(stream[1:], stream[0]))
            else:
                piece_section.sections.append(
                    _pix_container.make_vertex_stream(stream))
                # faces...
        piece_section.sections.append(faces_container)

        # BUILD AND STORE EDGE SECTION
        if sharp_edges:
            edges_container = _SectionData("Edges")
            for edge in sharp_edges:
                edges_container.data.append(edge)
            piece_section.sections.append(edges_container)

        # STORE PIECE SECTION
        piece_sections.append(piece_section)  # add a piece
    return piece_sections, global_vertex_count, global_face_count, global_edge_count, piece_index_obj, skin_list, skin_weights_cnt, skin_clones_cnt
Пример #2
0
def _fill_piece_sections_7(root_object, object_list, bone_list, scene, vg_list, used_materials, offset_matrix, scs_globals, output_type):
    """
    Fills up "Piece" sections for file version 7 (exchange format).
    :param object_list:
    :param bone_list:
    :param scene:
    :param vg_list:
    :param used_materials:
    :param offset_matrix:
    :return:
    """
    piece_sections = []  # container for all "Pieces"
    global_vertex_count = 0
    global_face_count = 0
    global_edge_count = 0
    piece_index_obj = {}
    skin_list = []
    skin_weights_cnt = 0
    skin_clones_cnt = 0
    for piece_index, obj in enumerate(object_list):
        mat_world = obj.matrix_world
        piece_index_obj[piece_index] = obj
        object_materials = _object_utils.get_object_materials(obj)  # get object materials

        # Get Object's Mesh data and list of temporarily disabled "Edge Split" Modifiers...
        mesh = _object_utils.get_mesh(obj)

        # VERTICES
        # TEMPORAL VERTEX STREAM DATA FORMAT:
        # example: ('_POSITION', [(0.0, 0.0, 0.0), (0.0, 0.0, 0.0), ...])
        # example: ('_SCALAR', [(0.0), (0.0), ...])

        stream_pos = ('_POSITION', [])
        # stream_nor = ('_NORMAL', [])
        # if scs_globals.export_vertex_groups:
        vg_layers_for_export, streams_vg = _object_utils.get_stream_vgs(obj)  # get Vertex Group layers (SCALARs)

        vertex_stream_count = 1
        vertex_streams = []
        stream_vg_container = []
        # print('bone_list: %s' % str(bone_list.keys))
        for vert_i, vert in enumerate(mesh.vertices):
            position = offset_matrix.inverted() * mesh.vertices[vert_i].co
            # scs_position = io_utils.change_to_scs_xyz_coordinates(mat_world * position, scs_globals.export_scale) ## POSITION
            scs_position = Matrix.Scale(scs_globals.export_scale,
                                        4) * _convert_utils.scs_to_blend_matrix().inverted() * mat_world * position  # POSITION
            stream_pos[1].append(scs_position)
            # stream_nor[1].append(io_utils.get_vertex_normal(mesh, vert_i))              # NORMAL
            if scs_globals.export_vertex_groups:
                if streams_vg:
                    vg_i = 0
                    for vg in vg_layers_for_export:  # weights (even unused) all vertices become 0.0
                        if vg.name in vg_list:
                            vg_weight = (_object_utils.get_vertex_group(vg, vert_i),)  # SCALARs
                            key = str("_SCALAR" + str(vg_i))
                            if vg_i == len(stream_vg_container) and len(stream_vg_container) != len(vg_layers_for_export):
                                stream_vg_container.append((vg.name, key, [vg_weight]))
                            else:
                                stream_vg_container[vg_i][2].append(vg_weight)
                            vg_i += 1

                            # SKINNING (OLD STYLE FOR PIM VER. 7)
            # if scs_globals.export_anim_file == 'anim':
            if root_object.scs_props.scs_root_animated == 'anim':
                skin_vector = scs_position  # NOTE: Vertex position - from Maya scaled *10 (old & unused in game engine)
                skin_weights = []
                for group in vert.groups:
                    for vg in vg_layers_for_export:
                        if vg.index == group.group:
                            for bone_i, bone in enumerate(bone_list):
                                if vg.name == bone.name:
                                    skin_weights.append((bone_i, group.weight))
                                    skin_weights_cnt += 1
                                    # print('vert: %i - group: %r (%i) - %s' % (vert_i, vg.name, bone_i, str(group.weight)))
                                    break
                            break
                skin_clones = ((piece_index, vert_i), )
                skin_clones_cnt += 1
                skin_list.append((skin_vector, skin_weights, skin_clones))

                # ##
        vertex_streams.append(stream_pos)
        # print('\nstream_pos:\n  %s' % str(stream_pos))
        # vertex_streams.append(stream_nor)
        # print('\nstream_nor:\n  %s' % str(stream_nor))
        for vg_stream in stream_vg_container:
            vertex_stream_count += 1
            vertex_streams.append(vg_stream)
            # print('\nvg_stream:\n  %s' % str(vg_stream))
        # FACES
        # TEMPORAL FACE STREAM DATA FORMAT:
        # faces = [face_data, face_data, ...]
        # face_data = (material, [vertex indices], [face-vertex streams])
        # face_streams = [('_UV0', [(0.0, 0.0), (0.0, 0.0), ...]), ...]
        # example: [(0, [0, 1, 2], [('_UV0', [(0.0, 0.0), (0.0, 0.0)]), ('_UV0', [(0.0, 0.0), (0.0, 0.0)])]), (), ...]

        faces = []
        face_cnt = 0
        uv_layers_exists = 1
        rgb_layers_exists = 1
        # print('used_materials: %s' % str(used_materials))
        for face_i, face in enumerate(mesh.polygons):
            face_cnt += 1
            streams_uv = None
            streams_vcolor = None
            if uv_layers_exists:
                requested_uv_layers, streams_uv = _mesh_utils.get_stream_uvs(mesh, scs_globals.active_uv_only)  # get UV layers (UVs)
                if not streams_uv:
                    uv_layers_exists = 0
            if rgb_layers_exists and scs_globals.export_vertex_color:
                if scs_globals.export_vertex_color_type_7 == 'rgb':
                    rgb_all_layers, streams_vcolor = _mesh_utils.get_stream_rgb(mesh, output_type, False)  # get Vertex Color layers (RGB)
                elif scs_globals.export_vertex_color_type_7 == 'rgbda':
                    rgb_all_layers, streams_vcolor = _mesh_utils.get_stream_rgb(mesh, output_type, True)  # get Vertex Color layers (
                    # RGBdA)
                else:
                    streams_vcolor = None  # TODO: Alpha from another layer
                if not streams_vcolor:
                    rgb_layers_exists = 0
            mat_index = used_materials.index(object_materials[face.material_index])
            # print('face-mat_index: %s; object_materials[f-m_i]: %s; used_materials.index(o_m[f-m_i]): %s' % (face.material_index,
            # object_materials[face.material_index], used_materials.index(object_materials[face.material_index])))
            face_verts = []
            for vert in face.vertices:
                face_verts.append(vert)
            face_verts = face_verts[::-1]  # revert vertex order in face
            # print('face_verts: %s' % str(face_verts))
            face_streams = []
            stream_fv_nor = ("_NORMAL", [])
            stream_fv_uv_container = []
            stream_fv_rgb_container = []
            stream_names = {}
            for loop_index in range(face.loop_start, face.loop_start + face.loop_total):
                # edge_index = mesh.loops[loop_index].edge_index
                vert_index = mesh.loops[loop_index].vertex_index
                # print('face i.: %s\tloop i.: %s\tedge i.: %s\tvert i.: %s' % (face_i, loop_index, edge_index, vert_index))
                # Normals
                stream_fv_nor[1].append(offset_matrix.inverted() * Vector(_mesh_utils.get_vertex_normal(mesh, vert_index)))
                # UV Layers
                if streams_uv:
                    for uv_i, uv_l in enumerate(requested_uv_layers):
                        uv_values = _mesh_utils.get_face_vertex_uv(uv_l.data, loop_index, uv_i)
                        key = str("_UV" + str(uv_i))
                        if uv_i == len(stream_fv_uv_container) and len(stream_fv_uv_container) != len(requested_uv_layers):
                            stream_fv_uv_container.append((key, [uv_values]))
                            stream_names[key] = uv_l.name
                        else:
                            stream_fv_uv_container[uv_i][1].append(uv_values)
                            # Vertex Color Layers (RGB)
                if scs_globals.export_vertex_color:
                    if streams_vcolor:
                        for rgb_i, rgb_l in enumerate(rgb_all_layers):
                            if scs_globals.export_vertex_color_type_7 == 'rgb':
                                rgb_values = _mesh_utils.get_face_vertex_color(rgb_l.data, loop_index, False, rgb_i)
                                key = str("_RGB" + str(rgb_i))
                            elif scs_globals.export_vertex_color_type_7 == 'rgbda':
                                rgb_values = _mesh_utils.get_face_vertex_color(rgb_l.data, loop_index, True, rgb_i)
                                key = str("_RGBA" + str(rgb_i))
                            else:
                                streams_vcolor = None  # TODO: Alpha from another layer
                            if rgb_i == len(stream_fv_rgb_container) and len(stream_fv_rgb_container) != len(rgb_all_layers):
                                stream_fv_rgb_container.append((key, [rgb_values]))
                                stream_names[key] = rgb_l.name
                            else:
                                stream_fv_rgb_container[rgb_i][1].append(rgb_values)
                                # Data Assembling
            face_streams.append(stream_fv_nor)
            for stream in stream_fv_uv_container:
                face_streams.append(stream)
            for stream in stream_fv_rgb_container:
                face_streams.append(stream)
            face_data = (mat_index, face_verts, face_streams)
            faces.append(face_data)

        # SHARP EDGES
        sharp_edges = []
        for edge in mesh.edges:
            if edge.use_edge_sharp:
                sharp_edges.append(edge.vertices[:])

        # BUILD FACE SECTION
        faces_container = _SectionData("Faces")
        faces_container.props.append(("StreamCount", len(faces[0][2])))
        for face_i, face_data in enumerate(faces):
            face_container = _SectionData("Face")
            face_container.props.append(("Index", face_i))
            face_container.props.append(("Material", face_data[0]))
            face_container.props.append(("Indices", face_data[1]))
            for stream in face_data[2]:
                if stream[0] in stream_names:
                    face_container.sections.append(_pix_container.make_vertex_stream(stream, stream_names[stream[0]]))
                else:
                    face_container.sections.append(_pix_container.make_vertex_stream(stream))
            faces_container.sections.append(face_container)

        # BUILD PIECE SECTION
        piece_section = _SectionData("Piece")
        piece_section.props.append(("Index", piece_index))
        global_vertex_count += len(stream_pos[1])
        piece_section.props.append(("VertexCount", len(stream_pos[1])))
        global_face_count += face_cnt
        piece_section.props.append(("FaceCount", face_cnt))
        global_edge_count += len(sharp_edges)
        piece_section.props.append(("EdgeCount", len(sharp_edges)))
        piece_section.props.append(("StreamCount", vertex_stream_count))
        piece_section.props.append(("", ""))
        # vertex streams...
        for stream in vertex_streams:
            if len(stream) == 3:
                # print('\nstream:\n  %s' % str(stream))
                piece_section.sections.append(_pix_container.make_vertex_stream(stream[1:], stream[0]))
            else:
                piece_section.sections.append(_pix_container.make_vertex_stream(stream))
                # faces...
        piece_section.sections.append(faces_container)

        # BUILD AND STORE EDGE SECTION
        if sharp_edges:
            edges_container = _SectionData("Edges")
            for edge in sharp_edges:
                edges_container.data.append(edge)
            piece_section.sections.append(edges_container)

        # STORE PIECE SECTION
        piece_sections.append(piece_section)  # add a piece
    return piece_sections, global_vertex_count, global_face_count, global_edge_count, piece_index_obj, skin_list, skin_weights_cnt, skin_clones_cnt
Пример #3
0
def _fill_piece_sections_5(root_object, object_list, bone_list, scene,
                           used_materials, offset_matrix, scs_globals):
    """Fills up "Piece" sections for file version 5.

    :param root_object: SCS Root Object
    :type root_object: bpy.types.Object
    :param object_list: Object for export
    :type object_list: list of Objects
    :param bone_list: Bones for export
    :type bone_list: list
    :param scene: Blender Scene to operate on
    :type scene: bpy.types.Scene
    :param used_materials: All Materials used in 'SCS Game Object'
    :type used_materials: list
    :param offset_matrix: Matrix for specifying of pivot point
    :type offset_matrix: Matrix
    :param scs_globals: SCS Tools Globals
    :type scs_globals: GlobalSCSProps
    :return: list of parameters
    :rtype: list
    """

    handle_unused_arg(__file__, _fill_piece_sections_5.__name__,
                      "used_materials", used_materials)

    apply_modifiers = scs_globals.apply_modifiers
    exclude_edgesplit = scs_globals.exclude_edgesplit
    include_edgesplit = scs_globals.include_edgesplit
    piece_sections = []
    piece_index_obj = {}
    piece_index = global_vertex_count = global_face_count = 0

    # ----- START: SOLVE THIS!
    skin_list = []
    skin_weights_cnt = skin_clones_cnt = 0
    # -----   END: SOLVE THIS!

    print_info = False

    if print_info:
        print('used_materials: %s\n' % str(used_materials))
        print('object_list: %s\n' % str(object_list))

    # For each object...
    for obj_i, obj in enumerate(object_list):
        piece_index_obj[obj_i] = obj
        # Get all Materials from Object as they're set in material slots...
        object_materials = _object_utils.get_object_materials(obj)
        if print_info:
            print('  object_materials: %s' % str(object_materials))

        # Make Material dictionary (needed for getting rid of Material duplicities)...
        material_dict = _make_material_dict(object_materials)
        if print_info:
            for item in material_dict:
                print('    "%s" = %s' % (str(item), str(material_dict[item])))
            print('')

        # Get Object's Mesh data and list of temporarily disabled "Edge Split" Modifiers...
        mesh = _object_utils.get_mesh(obj)

        # SORT GEOMETRY
        piece_dict, skin_list, skin_weights_cnt, skin_clones_cnt = _get_geometry_dict(
            root_object, obj, mesh, offset_matrix, material_dict,
            used_materials, bone_list, scs_globals)

        # DUMP
        if 0:
            for piece in piece_dict:
                print('Pm: %r' % piece)
                for data in piece_dict[piece]:
                    print('  Da: %r' % str(data))
                    if data == 'hash_dict':
                        if len(piece_dict[piece][data]) > 0:
                            for val in piece_dict[piece][data]:
                                print('    HD: %s:%s' %
                                      (str(val),
                                       str(piece_dict[piece][data][val])))
                        else:
                            print('    NO "hash_dict" Data!')
                    elif data == 'verts':
                        if len(piece_dict[piece][data]) > 0:
                            for val in piece_dict[piece][data]:
                                print('    Ve: %s' % str(val))
                        else:
                            print('    NO "verts" Data!')
                    elif data == 'faces':
                        if len(piece_dict[piece][data]) > 0:
                            for val in piece_dict[piece][data]:
                                print('    Fa: %s' % str(val))
                        else:
                            print('    NO "faces" Data!')
                print('')
            print('')

        # CREATE SECTIONS
        for piece in piece_dict:
            vertex_count = len(piece_dict[piece]['verts'])
            global_vertex_count += vertex_count
            face_count = len(piece_dict[piece]['faces'])
            global_face_count += face_count
            stream_sections = []

            # VERTEX STREAMS
            verts_data = {}
            for val in piece_dict[piece]['verts']:
                facevert_common_data, facevert_unique_data = val
                for data_key in facevert_common_data:
                    # print(' data_key: %s' % str(data_key))
                    if data_key == '_VG':
                        pass
                    else:
                        if data_key not in verts_data:
                            verts_data[data_key] = []
                        verts_data[data_key].append(
                            facevert_common_data[data_key])
                for data_key in facevert_unique_data:
                    # print(' data_key: %s' % str(data_key))
                    if data_key == '_UV':
                        for layer_i, layer in enumerate(
                                facevert_unique_data[data_key]):
                            layer_data_key = str(data_key + str(layer_i))
                            if layer_data_key not in verts_data:
                                verts_data[layer_data_key] = []
                            verts_data[layer_data_key].append(
                                facevert_unique_data[data_key][layer])
                    if data_key == '_RGBA':
                        for layer_i, layer in enumerate(
                                facevert_unique_data[data_key]):
                            if len(facevert_unique_data[data_key]) > 1:
                                layer_data_key = str(data_key + str(layer_i))
                            else:
                                layer_data_key = data_key
                            if layer_data_key not in verts_data:
                                verts_data[layer_data_key] = []
                            verts_data[layer_data_key].append(
                                facevert_unique_data[data_key][layer])
            lprint('S verts_data: %s', (str(verts_data), ))

            data_types = ('_POSITION', '_NORMAL', '_UV', '_UV0', '_UV1',
                          '_UV2', '_UV3', '_UV4', '_UV5', '_UV6', '_UV7',
                          '_UV8', '_UV9', '_RGBA', '_RGBA0', '_RGBA1',
                          '_RGBA2', '_RGBA3', '_RGBA4', '_RGBA5', '_RGBA6',
                          '_RGBA7', '_RGBA8', '_RGBA9', '_VG')
            add_uv = True
            add_rgba = False
            for data_type in data_types:
                if '_RGBA' not in verts_data:
                    add_rgba = True
                if data_type in verts_data:
                    if data_type.startswith('_UV'):
                        add_uv = False
                        stream_sections.append(
                            _pix_container.make_stream_section(
                                verts_data[data_type], data_type,
                                (data_type, )))
                    else:
                        stream_sections.append(
                            _pix_container.make_stream_section(
                                verts_data[data_type], data_type, ()))

            # ADD DEFAULT UV LAYER
            if add_uv:
                lprint('I Adding default UV layer.')
                uv_dummy_data = []
                for vert in range(len(piece_dict[piece]['verts'])):
                    uv_dummy_data.append(Vector((0.0, 0.0)))
                stream_sections.append(
                    _pix_container.make_stream_section(uv_dummy_data, '_UV0',
                                                       ('_UV0', )))

            # ADD DEFAULT RGBA LAYER
            if add_rgba:
                lprint('I Adding default RGBA (vertex color) layer.')
                rgba_dummy_data = []
                for vert in range(len(piece_dict[piece]['verts'])):
                    rgba_dummy_data.append(Vector((1.0, 1.0, 1.0, 1.0)))
                stream_sections.append(
                    _pix_container.make_stream_section(rgba_dummy_data,
                                                       '_RGBA', ()))

            # PIECE PROPERTIES
            piece_section = _SectionData("Piece")
            piece_section.props.append(("Index", piece_index))
            piece_section.props.append(
                ("Material", piece_dict[piece]['material_index']))
            piece_section.props.append(("VertexCount", vertex_count))
            piece_section.props.append(("TriangleCount", face_count))
            piece_section.props.append(("StreamCount", len(stream_sections)))
            piece_section.props.append(("", ""))
            piece_index += 1
            for stream_section in stream_sections:
                piece_section.sections.append(stream_section)

            # FACE STREAM
            stream_section = _SectionData("Triangles")
            stream_section.data = piece_dict[piece]['faces']
            piece_section.sections.append(stream_section)

            piece_sections.append(piece_section)

    return piece_sections, global_vertex_count, global_face_count, piece_index_obj, skin_list, skin_weights_cnt, skin_clones_cnt
Пример #4
0
def _fill_piece_sections_5(root_object, object_list, bone_list, scene, used_materials, offset_matrix, scs_globals):
    """Fills up "Piece" sections for file version 5.

    :param root_object: SCS Root Object
    :type root_object: bpy.types.Object
    :param object_list: Object for export
    :type object_list: list of Objects
    :param bone_list: Bones for export
    :type bone_list: list
    :param scene: Blender Scene to operate on
    :type scene: bpy.types.Scene
    :param used_materials: All Materials used in 'SCS Game Object'
    :type used_materials: list
    :param offset_matrix: Matrix for specifying of pivot point
    :type offset_matrix: Matrix
    :param scs_globals: SCS Tools Globals
    :type scs_globals: GlobalSCSProps
    :return: list of parameters
    :rtype: list
    """

    handle_unused_arg(__file__, _fill_piece_sections_5.__name__, "used_materials", used_materials)

    apply_modifiers = scs_globals.apply_modifiers
    exclude_edgesplit = scs_globals.exclude_edgesplit
    include_edgesplit = scs_globals.include_edgesplit
    piece_sections = []
    piece_index_obj = {}
    piece_index = global_vertex_count = global_face_count = 0

    # ----- START: SOLVE THIS!
    skin_list = []
    skin_weights_cnt = skin_clones_cnt = 0
    # -----   END: SOLVE THIS!

    print_info = False

    if print_info:
        print('used_materials: %s\n' % str(used_materials))
        print('object_list: %s\n' % str(object_list))

    # For each object...
    for obj_i, obj in enumerate(object_list):
        piece_index_obj[obj_i] = obj
        # Get all Materials from Object as they're set in material slots...
        object_materials = _object_utils.get_object_materials(obj)
        if print_info:
            print('  object_materials: %s' % str(object_materials))

        # Make Material dictionary (needed for getting rid of Material duplicities)...
        material_dict = _make_material_dict(object_materials)
        if print_info:
            for item in material_dict:
                print('    "%s" = %s' % (str(item), str(material_dict[item])))
            print('')

        # Get Object's Mesh data and list of temporarily disabled "Edge Split" Modifiers...
        mesh = _object_utils.get_mesh(obj)

        # SORT GEOMETRY
        piece_dict, skin_list, skin_weights_cnt, skin_clones_cnt = _get_geometry_dict(root_object,
                                                                                      obj,
                                                                                      mesh,
                                                                                      offset_matrix,
                                                                                      material_dict,
                                                                                      used_materials,
                                                                                      bone_list,
                                                                                      scs_globals)

        # DUMP
        if 0:
            for piece in piece_dict:
                print('Pm: %r' % piece)
                for data in piece_dict[piece]:
                    print('  Da: %r' % str(data))
                    if data == 'hash_dict':
                        if len(piece_dict[piece][data]) > 0:
                            for val in piece_dict[piece][data]:
                                print('    HD: %s:%s' % (str(val), str(piece_dict[piece][data][val])))
                        else:
                            print('    NO "hash_dict" Data!')
                    elif data == 'verts':
                        if len(piece_dict[piece][data]) > 0:
                            for val in piece_dict[piece][data]:
                                print('    Ve: %s' % str(val))
                        else:
                            print('    NO "verts" Data!')
                    elif data == 'faces':
                        if len(piece_dict[piece][data]) > 0:
                            for val in piece_dict[piece][data]:
                                print('    Fa: %s' % str(val))
                        else:
                            print('    NO "faces" Data!')
                print('')
            print('')

        # CREATE SECTIONS
        for piece in piece_dict:
            vertex_count = len(piece_dict[piece]['verts'])
            global_vertex_count += vertex_count
            face_count = len(piece_dict[piece]['faces'])
            global_face_count += face_count
            stream_sections = []

            # VERTEX STREAMS
            verts_data = {}
            for val in piece_dict[piece]['verts']:
                facevert_common_data, facevert_unique_data = val
                for data_key in facevert_common_data:
                    # print(' data_key: %s' % str(data_key))
                    if data_key == '_VG':
                        pass
                    else:
                        if data_key not in verts_data:
                            verts_data[data_key] = []
                        verts_data[data_key].append(facevert_common_data[data_key])
                for data_key in facevert_unique_data:
                    # print(' data_key: %s' % str(data_key))
                    if data_key == '_UV':
                        for layer_i, layer in enumerate(facevert_unique_data[data_key]):
                            layer_data_key = str(data_key + str(layer_i))
                            if layer_data_key not in verts_data:
                                verts_data[layer_data_key] = []
                            verts_data[layer_data_key].append(facevert_unique_data[data_key][layer])
                    if data_key == '_RGBA':
                        for layer_i, layer in enumerate(facevert_unique_data[data_key]):
                            if len(facevert_unique_data[data_key]) > 1:
                                layer_data_key = str(data_key + str(layer_i))
                            else:
                                layer_data_key = data_key
                            if layer_data_key not in verts_data:
                                verts_data[layer_data_key] = []
                            verts_data[layer_data_key].append(facevert_unique_data[data_key][layer])
            lprint('S verts_data: %s', (str(verts_data),))

            data_types = (
                '_POSITION', '_NORMAL', '_UV', '_UV0', '_UV1', '_UV2', '_UV3', '_UV4', '_UV5', '_UV6', '_UV7', '_UV8', '_UV9', '_RGBA', '_RGBA0',
                '_RGBA1', '_RGBA2', '_RGBA3', '_RGBA4', '_RGBA5', '_RGBA6', '_RGBA7', '_RGBA8', '_RGBA9', '_VG')
            add_uv = True
            add_rgba = False
            for data_type in data_types:
                if '_RGBA' not in verts_data:
                    add_rgba = True
                if data_type in verts_data:
                    if data_type.startswith('_UV'):
                        add_uv = False
                        stream_sections.append(_pix_container.make_stream_section(verts_data[data_type], data_type, (data_type,)))
                    else:
                        stream_sections.append(_pix_container.make_stream_section(verts_data[data_type], data_type, ()))

            # ADD DEFAULT UV LAYER
            if add_uv:
                lprint('I Adding default UV layer.')
                uv_dummy_data = []
                for vert in range(len(piece_dict[piece]['verts'])):
                    uv_dummy_data.append(Vector((0.0, 0.0)))
                stream_sections.append(_pix_container.make_stream_section(uv_dummy_data, '_UV0', ('_UV0',)))

            # ADD DEFAULT RGBA LAYER
            if add_rgba:
                lprint('I Adding default RGBA (vertex color) layer.')
                rgba_dummy_data = []
                for vert in range(len(piece_dict[piece]['verts'])):
                    rgba_dummy_data.append(Vector((1.0, 1.0, 1.0, 1.0)))
                stream_sections.append(_pix_container.make_stream_section(rgba_dummy_data, '_RGBA', ()))

            # PIECE PROPERTIES
            piece_section = _SectionData("Piece")
            piece_section.props.append(("Index", piece_index))
            piece_section.props.append(("Material", piece_dict[piece]['material_index']))
            piece_section.props.append(("VertexCount", vertex_count))
            piece_section.props.append(("TriangleCount", face_count))
            piece_section.props.append(("StreamCount", len(stream_sections)))
            piece_section.props.append(("", ""))
            piece_index += 1
            for stream_section in stream_sections:
                piece_section.sections.append(stream_section)

            # FACE STREAM
            stream_section = _SectionData("Triangles")
            stream_section.data = piece_dict[piece]['faces']
            piece_section.sections.append(stream_section)

            piece_sections.append(piece_section)

    return piece_sections, global_vertex_count, global_face_count, piece_index_obj, skin_list, skin_weights_cnt, skin_clones_cnt