Ejemplo n.º 1
0
    def _adjust_material_nodes(mat: Material, adjustments: Dict[str, str]):
        """ Adjust the material node of the given material according to the given adjustments.

        Textures or diffuse colors will be changed according to the given material_adjustments.

        :param mat: The blender material.
        :param adjustments: A dict containing a new "diffuse" color or a new "texture" path
        """

        if "diffuse" in adjustments:
            principle_node = mat.get_the_one_node_with_type("BsdfPrincipled")
            principle_node.inputs[
                'Base Color'].default_value = Utility.hex_to_rgba(
                    adjustments["diffuse"])

        if "texture" in adjustments:
            image_path = os.path.join(SuncgLoader._suncg_dir, "texture",
                                      adjustments["texture"])
            image_path = resolve_path(image_path)

            if os.path.exists(image_path + ".png"):
                image_path += ".png"
            else:
                image_path += ".jpg"

            image_node = mat.get_the_one_node_with_type("ShaderNodeTexImage")
            if os.path.exists(image_path):
                image_node.image = bpy.data.images.load(image_path,
                                                        check_existing=True)
            else:
                print(
                    "Warning: Cannot load texture, path does not exist: {}, remove image node again"
                    .format(image_path))
                mat.remove_node(image_node)
Ejemplo n.º 2
0
    def _correct_materials(objects: List[MeshObject]):
        """ If the used material contains an alpha texture, the alpha texture has to be flipped to be correct

        :param objects: Mesh objects where the material might be wrong.
        """
        for obj in objects:
            for material in obj.get_materials():
                if material is None:
                    continue
                # Create a principled node and set the default color
                principled_bsdf = material.get_the_one_node_with_type(
                    "BsdfPrincipled")
                # Pick random skin color value
                skin_tone_hex = np.random.choice(AMASSLoader.human_skin_colors)
                skin_tone_rgb = Utility.hex_to_rgba(skin_tone_hex)[:3]

                # this is done to make the chance higher that the representation of skin tones is more diverse
                skin_tone_fac = random.uniform(0.0, 1)
                skin_tone_rgb = [
                    value * skin_tone_fac for value in skin_tone_rgb
                ]
                principled_bsdf.inputs[
                    "Base Color"].default_value = mathutils.Vector(
                        [*skin_tone_rgb, 1.0])
                principled_bsdf.inputs["Subsurface"].default_value = 0.2
                principled_bsdf.inputs[
                    "Subsurface Color"].default_value = mathutils.Vector(
                        [*skin_tone_rgb, 1.0])

                # darker skin looks better when made less specular
                principled_bsdf.inputs["Specular"].default_value = np.mean(
                    skin_tone_rgb) / 255.0

                texture_nodes = material.get_nodes_with_type(
                    "ShaderNodeTexImage")
                if texture_nodes and len(texture_nodes) > 1:
                    # find the image texture node which is connect to alpha
                    node_connected_to_the_alpha = None
                    for node_links in principled_bsdf.inputs["Alpha"].links:
                        if "ShaderNodeTexImage" in node_links.from_node.bl_idname:
                            node_connected_to_the_alpha = node_links.from_node
                    # if a node was found which is connected to the alpha node, add an invert between the two
                    if node_connected_to_the_alpha is not None:
                        invert_node = material.new_node("ShaderNodeInvert")
                        invert_node.inputs["Fac"].default_value = 1.0
                        material.insert_node_instead_existing_link(
                            node_connected_to_the_alpha.outputs["Color"],
                            invert_node.inputs["Color"],
                            invert_node.outputs["Color"],
                            principled_bsdf.inputs["Alpha"])