<commit_before>import bpy
import json
from shutil import copyfile
from .common import *


def copy_linked_map(input_name, directory, blender_material, node):
    node_input = node.inputs.get(input_name)
    if not node_input:
        return None
    filepath = None
    if node_input.is_linked:
        image = node_input.links[0].from_node.image
        filename = image.name
        source_filepath = bpy.path.abspath(image.filepath, library=image.library)
        filepath = library_path(blender_material) + "textures/" + filename
        full_filepath = directory + '/' + filepath
        os.makedirs(os.path.dirname(full_filepath), exist_ok=True)
        copyfile(source_filepath, full_filepath)
    return filepath


def material_path(blender_material: bpy.types.Material):
    path = library_path(blender_material) + "materials/" + blender_material.name + ".material"
    return path.strip('/')


def write(report, directory):
    blender_materials = bpy.data.materials

    for blender_material in blender_materials:
        print("WRITING " + str(blender_material.name))
        report({'INFO'}, "Writing: " + str(blender_material.name))
        try:
            node = next(n for n in blender_material.node_tree.nodes.values() if n.bl_idname == "ShaderNodeOutputMaterial").inputs[0].links[0].from_node

            if node.bl_idname == "ShaderNodeBsdfPrincipled":
                albedo_input = node.inputs.get("Base Color")
                albedo_map = copy_linked_map("Base Color", directory, blender_material, node)
                albedo = (0.0, 0.0, 0.0) if not albedo_input.default_value[:3] else albedo_input.default_value[:3]

                normal_input = node.inputs.get("Normal")
                normal_map_node = normal_input.links[0].from_node if normal_input.is_linked else None
                normal_map = copy_linked_map("Color", directory, blender_material, normal_map_node) if normal_map_node else None

                metallic_map = copy_linked_map("Metallic", directory, blender_material, node)
                roughness_map = copy_linked_map("Roughness", directory, blender_material, node)
                ambient_occlusion_map = copy_linked_map("Ambient occlusion", directory, blender_material, node)

                roughness = node.inputs.get("Roughness").default_value
                metallic = node.inputs.get("Metallic").default_value

                opacity_node = node.inputs.get("Opacity")
                opacity = opacity_node.default_value if opacity_node else 1.0

                emission_node = node.inputs.get("Emission")
                emission_rgb = emission_node.default_value[:3] if emission_node else [0, 0, 0]
                emission = 0.2126 * emission_rgb[0] + 0.7152 * emission_rgb[1] + 0.0722 * emission_rgb[2]

                transmission = node.inputs.get("Transmission").default_value

                ambient_occlusion_input = node.inputs.get("Ambient occlusion")
                ambient_occlusion = ambient_occlusion_input.default_value if ambient_occlusion_input else 1.0
            else:
                albedo_input = node.inputs.get("Base Color")
                albedo_map = copy_linked_map("Base Color", directory, blender_material, node)
                albedo = (0.0, 0.0, 0.0) if not albedo_input.default_value[:3] else albedo_input.default_value[:3]

                normal_map = copy_linked_map("Normal", directory, blender_material, node)
                metallic_map = copy_linked_map("Metallic", directory, blender_material, node)
                roughness_map = copy_linked_map("Roughness", directory, blender_material, node)
                ambient_occlusion_map = copy_linked_map("Ambient occlusion", directory, blender_material, node)

                roughness = node.inputs.get("Roughness").default_value
                metallic = node.inputs.get("Metallic").default_value

                opacity_node = node.inputs.get("Opacity")
                opacity = opacity_node.default_value if opacity_node else 1.0

                emission_node = node.inputs.get("Emission")
                emission = emission_node.default_value if emission_node else 0.0

                transmission = node.inputs.get("Transmission").default_value

                ambient_occlusion_input = node.inputs.get("Ambient occlusion")
                ambient_occlusion = ambient_occlusion_input.default_value if ambient_occlusion_input else 1.0

            material = {"albedo": tuple(albedo),
                        "opacity": opacity,
                        "transmission": transmission,
                        "roughness": float(roughness),
                        "metallic": float(metallic),
                        "emission": float(emission),<commit_msg>Only support Principled shader.<commit_after>import bpy
import json
from shutil import copyfile
from .common import *


def copy_linked_map(input_name, directory, blender_material, node):
    node_input = node.inputs.get(input_name)
    if not node_input:
        return None
    filepath = None
    if node_input.is_linked:
        image = node_input.links[0].from_node.image
        filename = image.name
        source_filepath = bpy.path.abspath(image.filepath, library=image.library)
        filepath = library_path(blender_material) + "textures/" + filename
        full_filepath = directory + '/' + filepath
        os.makedirs(os.path.dirname(full_filepath), exist_ok=True)
        copyfile(source_filepath, full_filepath)
    return filepath


def material_path(blender_material: bpy.types.Material):
    path = library_path(blender_material) + "materials/" + blender_material.name + ".material"
    return path.strip('/')


def write(report, directory):
    blender_materials = bpy.data.materials

    for blender_material in blender_materials:
        print("WRITING " + str(blender_material.name))
        report({'INFO'}, "Writing: " + str(blender_material.name))
        try:
            node = next(n for n in blender_material.node_tree.nodes.values() if n.bl_idname == "ShaderNodeOutputMaterial").inputs[0].links[0].from_node

            if node.bl_idname != "ShaderNodeBsdfPrincipled":
                raise Exception("Material node must be Principled.")

            albedo_input = node.inputs.get("Base Color")
            albedo_map = copy_linked_map("Base Color", directory, blender_material, node)
            albedo = (0.0, 0.0, 0.0) if not albedo_input.default_value[:3] else albedo_input.default_value[:3]

            normal_input = node.inputs.get("Normal")
            normal_map_node = normal_input.links[0].from_node if normal_input.is_linked else None
            normal_map = copy_linked_map("Color", directory, blender_material, normal_map_node) if normal_map_node else None

            metallic_map = copy_linked_map("Metallic", directory, blender_material, node)
            roughness_map = copy_linked_map("Roughness", directory, blender_material, node)
            ambient_occlusion_map = copy_linked_map("Ambient occlusion", directory, blender_material, node)

            roughness = node.inputs.get("Roughness").default_value
            metallic = node.inputs.get("Metallic").default_value
            index_of_refraction = node.inputs.get("IOR").default_value

            emission_node = node.inputs.get("Emission")
            emission_rgb = emission_node.default_value[:3] if emission_node else [0, 0, 0]
            emission = 0.2126 * emission_rgb[0] + 0.7152 * emission_rgb[1] + 0.0722 * emission_rgb[2]

            transmission = node.inputs.get("Transmission").default_value

            ambient_occlusion_input = node.inputs.get("Ambient occlusion")
            ambient_occlusion = ambient_occlusion_input.default_value if ambient_occlusion_input else 1.0

            material = {"albedo": tuple(albedo),
                        "index_of_refraction": index_of_refraction,
                        "transmission": transmission,
                        "roughness": float(roughness),
                        "metallic": float(metallic),
                        "emission": float(emission),
{'fraction_non_alphanumeric': 0.0968421052631579, 'fraction_numerical': 0.010897832817337461, 'mean_word_length': 2.779129620963968, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '300314', 'n_tokens_mistral': 2437, 'n_tokens_neox': 2254, 'n_words': 449}