Ejemplo n.º 1
0
    def _write_vertex_colors(self, object_, mesh, root):
        float_colors = []
        alpha_found = False

        if object_.data.tessface_vertex_colors:
            color_layers = object_.data.tessface_vertex_colors
            for color_layer in color_layers:
                for fi, face in enumerate(color_layer.data):
                    colors = [face.color1[:], face.color2[:], face.color3[:]]
                    if len(mesh.tessfaces[fi].vertices) == 4:
                        colors.append(face.color4[:])

                    for color in colors:
                        if color_layer.name.lower() == "alpha":
                            alpha_found = True
                            alpha = (color[0] + color[1] + color[2]) / 3
                            float_colors.extend([1, 1, 1, alpha])
                        else:
                            float_colors.extend(color)

        if float_colors:
            id_ = "{!s}-colors".format(object_.name)
            params = ("RGBA" if alpha_found else "RGB")
            source = utils.write_source(id_, "float", float_colors, params)
            root.appendChild(source)
Ejemplo n.º 2
0
    def _write_normals(self, object_, mesh, root):
        float_normals = []
        float_normals_count = ""

        for face in mesh.tessfaces:
            if face.use_smooth:
                for vert in face.vertices:
                    vertex = mesh.vertices[vert]
                    float_normals.extend(vertex.normal)
            else:
                if self._config.average_planar:
                    count = 1
                    nx = face.normal.x
                    ny = face.normal.y
                    nz = face.normal.z

                    for planar_face in mesh.tessfaces:
                        angle = face.normal.angle(planar_face.normal)
                        if (-.052 < angle and angle < .052):
                            nx += planar_face.normal.x
                            ny += planar_face.normal.y
                            nz += planar_face.normal.z
                            count += 1

                    float_normals.append(nx / count)
                    float_normals.append(ny / count)
                    float_normals.append(nz / count)
                else:
                    float_normals.extend(face.normal)

        id_ = "{!s}-normals".format(object_.name)
        source = utils.write_source(id_, "float", float_normals, "XYZ")
        root.appendChild(source)
Ejemplo n.º 3
0
    def _write_vertex_colors(self, object_, mesh, root):
        float_colors = []
        alpha_found = False

        if object_.data.tessface_vertex_colors:
            color_layers = object_.data.tessface_vertex_colors
            for color_layer in color_layers:
                for fi, face in enumerate(color_layer.data):
                    colors = [face.color1[:], face.color2[:], face.color3[:]]
                    if len(mesh.tessfaces[fi].vertices) == 4:
                        colors.append(face.color4[:])

                    for color in colors:
                        if color_layer.name.lower() == "alpha":
                            alpha_found = True
                            alpha = (color[0] + color[1] + color[2]) / 3
                            float_colors.extend([1, 1, 1, alpha])
                        else:
                            float_colors.extend(color)

        if float_colors:
            id_ = "{!s}-colors".format(object_.name)
            params = ("RGBA" if alpha_found else "RGB")
            source = utils.write_source(id_, "float", float_colors, params)
            root.appendChild(source)
Ejemplo n.º 4
0
    def _write_normals(self, object_, mesh, root):
        float_normals = []
        float_normals_count = ""

        for face in mesh.tessfaces:
            if face.use_smooth:
                for vert in face.vertices:
                    vertex = mesh.vertices[vert]
                    float_normals.extend(vertex.normal)
            else:
                if self._config.average_planar:
                    count = 1
                    nx = face.normal.x
                    ny = face.normal.y
                    nz = face.normal.z

                    for planar_face in mesh.tessfaces:
                        angle = face.normal.angle(planar_face.normal)
                        if (-.052 < angle and angle < .052):
                            nx += planar_face.normal.x
                            ny += planar_face.normal.y
                            nz += planar_face.normal.z
                            count += 1

                    float_normals.append(nx / count)
                    float_normals.append(ny / count)
                    float_normals.append(nz / count)
                else:
                    float_normals.extend(face.normal)

        id_ = "{!s}-normals".format(object_.name)
        source = utils.write_source(id_, "float", float_normals, "XYZ")
        root.appendChild(source)
Ejemplo n.º 5
0
    def _write_positions(self, object_, mesh, root):
        float_positions = []
        for vertex in mesh.vertices:
            float_positions.extend(vertex.co)

        id_ = "{!s}-positions".format(object_.name)
        source = utils.write_source(id_, "float", float_positions, "XYZ")
        root.appendChild(source)
Ejemplo n.º 6
0
    def _write_positions(self, object_, mesh, root):
        float_positions = []
        for vertex in mesh.vertices:
            float_positions.extend(vertex.co)

        id_ = "{!s}-positions".format(object_.name)
        source = utils.write_source(id_, "float", float_positions, "XYZ")
        root.appendChild(source)
Ejemplo n.º 7
0
    def _process_bone_weights(self, object_, armature, skin_node):

        bones = utils.get_bones(armature)
        group_weights = []
        vw = ""
        vertex_groups_lengths = ""
        vertex_count = 0
        bone_list = {}

        for bone_id, bone in enumerate(bones):
            bone_list[bone.name] = bone_id

        for vertex in object_.data.vertices:
            vertex_group_count = 0
            for group in vertex.groups:
                group_name = object_.vertex_groups[group.group].name
                if (group.weight == 0 or
                        group_name not in bone_list):
                    continue
                if vertex_group_count == 8:
                    cbPrint("Too many bone references in {}:{} vertex group"
                            .format(object_.name, group_name))
                    continue
                group_weights.append(group.weight)
                vw = "{}{} {} ".format(vw, bone_list[group_name], vertex_count)
                vertex_count += 1
                vertex_group_count += 1

            vertex_groups_lengths = "{}{} ".format(vertex_groups_lengths,
                                                   vertex_group_count)

        id_ = "{!s}_{!s}-weights".format(armature.name, object_.name)
        source = utils.write_source(id_, "float", group_weights, [])
        skin_node.appendChild(source)

        vertex_weights = self._doc.createElement("vertex_weights")
        vertex_weights.setAttribute("count", str(len(object_.data.vertices)))

        id_ = "{!s}_{!s}".format(armature.name, object_.name)
        input = utils.write_input(id_, 0, "joints", "JOINT")
        vertex_weights.appendChild(input)
        input = utils.write_input(id_, 1, "weights", "WEIGHT")
        vertex_weights.appendChild(input)

        vcount = self._doc.createElement("vcount")
        vcount_text = self._doc.createTextNode(vertex_groups_lengths)
        vcount.appendChild(vcount_text)
        vertex_weights.appendChild(vcount)

        v = self._doc.createElement("v")
        v_text = self._doc.createTextNode(vw)
        v.appendChild(v_text)
        vertex_weights.appendChild(v)

        skin_node.appendChild(vertex_weights)
Ejemplo n.º 8
0
    def _process_bone_weights(self, object_, armature, skin_node):

        bones = utils.get_bones(armature)
        group_weights = []
        vw = ""
        vertex_groups_lengths = ""
        vertex_count = 0
        bone_list = {}

        for bone_id, bone in enumerate(bones):
            bone_list[bone.name] = bone_id

        for vertex in object_.data.vertices:
            vertex_group_count = 0
            for group in vertex.groups:
                group_name = object_.vertex_groups[group.group].name
                if (group.weight == 0 or group_name not in bone_list):
                    continue
                if vertex_group_count == 8:
                    cbPrint("Too many bone references in {}:{} vertex group".
                            format(object_.name, group_name))
                    continue
                group_weights.append(group.weight)
                vw = "{}{} {} ".format(vw, bone_list[group_name], vertex_count)
                vertex_count += 1
                vertex_group_count += 1

            vertex_groups_lengths = "{}{} ".format(vertex_groups_lengths,
                                                   vertex_group_count)

        id_ = "{!s}_{!s}-weights".format(armature.name, object_.name)
        source = utils.write_source(id_, "float", group_weights, [])
        skin_node.appendChild(source)

        vertex_weights = self._doc.createElement("vertex_weights")
        vertex_weights.setAttribute("count", str(len(object_.data.vertices)))

        id_ = "{!s}_{!s}".format(armature.name, object_.name)
        input = utils.write_input(id_, 0, "joints", "JOINT")
        vertex_weights.appendChild(input)
        input = utils.write_input(id_, 1, "weights", "WEIGHT")
        vertex_weights.appendChild(input)

        vcount = self._doc.createElement("vcount")
        vcount_text = self._doc.createTextNode(vertex_groups_lengths)
        vcount.appendChild(vcount_text)
        vertex_weights.appendChild(vcount)

        v = self._doc.createElement("v")
        v_text = self._doc.createTextNode(vw)
        v.appendChild(v_text)
        vertex_weights.appendChild(v)

        skin_node.appendChild(vertex_weights)
Ejemplo n.º 9
0
    def _process_bone_joints(self, object_, armature, skin_node):

        bones = utils.get_bones(armature)
        id_ = "{!s}_{!s}-joints".format(armature.name, object_.name)
        group = utils.get_armature_node(object_)
        bone_names = []
        for bone in bones:
            props_name = self._create_properties_name(bone, group)
            bone_name = "{!s}{!s}".format(bone.name, props_name)
            bone_names.append(bone_name)
        source = utils.write_source(id_, "IDREF", bone_names, [])
        skin_node.appendChild(source)
Ejemplo n.º 10
0
    def _process_bone_joints(self, object_, armature, skin_node):

        bones = utils.get_bones(armature)
        id_ = "{!s}_{!s}-joints".format(armature.name, object_.name)
        group = utils.get_armature_node(object_)
        bone_names = []
        for bone in bones:
            props_name = self._create_properties_name(bone, group)
            bone_name = "{!s}{!s}".format(bone.name, props_name)
            bone_names.append(bone_name)
        source = utils.write_source(id_, "IDREF", bone_names, [])
        skin_node.appendChild(source)
Ejemplo n.º 11
0
    def _create_animation_node(self, type_, data, id_prefix):
        id_ = "{!s}-{!s}".format(id_prefix, type_)
        type_map = {
            "input": ["float", ["TIME"]],
            "output": ["float", ["VALUE"]],
            "intangent": ["float", "XY"],
            "outangent": ["float", "XY"],
            "interpolation": ["name", ["INTERPOLATION"]]
        }

        source = utils.write_source(id_, type_map[type_][0], data,
                                    type_map[type_][1])

        return source
Ejemplo n.º 12
0
    def _create_animation_node(self, type_, data, id_prefix):
        id_ = "{!s}-{!s}".format(id_prefix, type_)
        type_map = {
            "input": ["float", ["TIME"]],
            "output": ["float", ["VALUE"]],
            "intangent": ["float", "XY"],
            "outangent": ["float", "XY"],
            "interpolation": ["name", ["INTERPOLATION"]]
        }

        source = utils.write_source(
            id_, type_map[type_][0], data, type_map[type_][1])

        return source
Ejemplo n.º 13
0
    def _process_bone_matrices(self, object_, armature, skin_node):

        bones = utils.get_bones(armature)
        bone_matrices = []
        for bone in bones:
            fakebone = utils.get_fakebone(bone.name)
            if fakebone is None:
                return
            matrix_local = copy.deepcopy(fakebone.matrix_local)
            utils.negate_z_axis_of_matrix(matrix_local)
            bone_matrices.extend(utils.matrix_to_array(matrix_local))

        id_ = "{!s}_{!s}-matrices".format(armature.name, object_.name)
        source = utils.write_source(id_, "float4x4", bone_matrices, [])
        skin_node.appendChild(source)
Ejemplo n.º 14
0
    def _process_bone_matrices(self, object_, armature, skin_node):

        bones = utils.get_bones(armature)
        bone_matrices = []
        for bone in bones:
            fakebone = utils.get_fakebone(bone.name)
            if fakebone is None:
                return
            matrix_local = copy.deepcopy(fakebone.matrix_local)
            utils.negate_z_axis_of_matrix(matrix_local)
            bone_matrices.extend(utils.matrix_to_array(matrix_local))

        id_ = "{!s}_{!s}-matrices".format(armature.name, object_.name)
        source = utils.write_source(id_, "float4x4", bone_matrices, [])
        skin_node.appendChild(source)
Ejemplo n.º 15
0
    def _write_uvs(self, object_, mesh, root):
        uvdata = object_.data.tessface_uv_textures
        if uvdata is None:
            cbPrint("Your UV map is missing, adding...")
            bpy.ops.mesh.uv_texture_add()
        else:
            cbPrint("Found UV map.")

        float_uvs = []
        for uvindex, uvlayer in enumerate(uvdata):
            mapslot = uvindex
            mapname = uvlayer.name
            uvid = "{!s}-{!s}-{!s}".format(object_.name, mapname, mapslot)

            for uf in uvlayer.data:
                for uv in uf.uv:
                    float_uvs.extend(uv)

        id_ = "{!s}-UVMap-0".format(object_.name)
        source = utils.write_source(id_, "float", float_uvs, "ST")
        root.appendChild(source)
Ejemplo n.º 16
0
    def _write_uvs(self, object_, mesh, root):
        uvdata = object_.data.tessface_uv_textures
        if uvdata is None:
            cbPrint("Your UV map is missing, adding...")
            bpy.ops.mesh.uv_texture_add()
        else:
            cbPrint("Found UV map.")

        float_uvs = []
        for uvindex, uvlayer in enumerate(uvdata):
            mapslot = uvindex
            mapname = uvlayer.name
            uvid = "{!s}-{!s}-{!s}".format(object_.name, mapname, mapslot)

            for uf in uvlayer.data:
                for uv in uf.uv:
                    float_uvs.extend(uv)

        id_ = "{!s}-UVMap-0".format(object_.name)
        source = utils.write_source(id_, "float", float_uvs, "ST")
        root.appendChild(source)