def __gather_orm_texture(blender_material, export_settings):
    # Check for the presence of Occlusion, Roughness, Metallic sharing a single image.
    # If not fully shared, return None, so the images will be cached and processed separately.

    occlusion = gltf2_blender_get.get_socket(blender_material, "Occlusion")
    if occlusion is None or not __has_image_node_from_socket(occlusion):
        occlusion = gltf2_blender_get.get_socket_old(blender_material,
                                                     "Occlusion")
        if occlusion is None or not __has_image_node_from_socket(occlusion):
            return None

    metallic_socket = gltf2_blender_get.get_socket(blender_material,
                                                   "Metallic")
    roughness_socket = gltf2_blender_get.get_socket(blender_material,
                                                    "Roughness")

    hasMetal = metallic_socket is not None and __has_image_node_from_socket(
        metallic_socket)
    hasRough = roughness_socket is not None and __has_image_node_from_socket(
        roughness_socket)

    if not hasMetal and not hasRough:
        metallic_roughness = gltf2_blender_get.get_socket_old(
            blender_material, "MetallicRoughness")
        if metallic_roughness is None or not __has_image_node_from_socket(
                metallic_roughness):
            return None
        result = (occlusion, metallic_roughness)
    elif not hasMetal:
        result = (occlusion, roughness_socket)
    elif not hasRough:
        result = (occlusion, metallic_socket)
    else:
        result = (occlusion, roughness_socket, metallic_socket)

    if not gltf2_blender_gather_texture_info.check_same_size_images(result):
        print_console(
            "INFO",
            "Occlusion and metal-roughness texture will be exported separately "
            "(use same-sized images if you want them combined)")
        return None

    # Double-check this will past the filter in texture_info
    info = gltf2_blender_gather_texture_info.gather_texture_info(
        result[0], result, export_settings)
    if info is None:
        return None

    return result
def __gather_occlusion_texture(blender_material, orm_texture, export_settings):
    occlusion = gltf2_blender_get.get_socket(blender_material, "Occlusion")
    if occlusion is None:
        occlusion = gltf2_blender_get.get_socket_old(blender_material,
                                                     "Occlusion")
    return gltf2_blender_gather_texture_info.gather_material_occlusion_texture_info_class(
        occlusion, orm_texture or (occlusion, ), export_settings)
def __gather_base_color_factor(blender_material, export_settings):
    rgb, alpha = None, None

    alpha_socket = gltf2_blender_get.get_socket(blender_material, "Alpha")
    if isinstance(alpha_socket, bpy.types.NodeSocket):
        alpha = gltf2_blender_get.get_factor_from_socket(alpha_socket,
                                                         kind='VALUE')

    base_color_socket = gltf2_blender_get.get_socket(blender_material,
                                                     "Base Color")
    if base_color_socket is None:
        base_color_socket = gltf2_blender_get.get_socket(
            blender_material, "BaseColor")
    if base_color_socket is None:
        base_color_socket = gltf2_blender_get.get_socket_old(
            blender_material, "BaseColorFactor")
    if base_color_socket is None:
        base_color_socket = gltf2_blender_get.get_socket(
            blender_material, "Background")
    if isinstance(base_color_socket, bpy.types.NodeSocket):
        rgb = gltf2_blender_get.get_factor_from_socket(base_color_socket,
                                                       kind='RGB')

    if rgb is None: rgb = [1.0, 1.0, 1.0]
    if alpha is None: alpha = 1.0

    rgba = [*rgb, alpha]

    if rgba == [1, 1, 1, 1]: return None
    return rgba
Exemplo n.º 4
0
def __gather_roughness_factor(blender_material, export_settings):
    roughness_socket = gltf2_blender_get.get_socket(blender_material, "Roughness")
    if roughness_socket is None:
        roughness_socket = gltf2_blender_get.get_socket_old(blender_material, "RoughnessFactor")
    if isinstance(roughness_socket, bpy.types.NodeSocket) and not roughness_socket.is_linked:
        return roughness_socket.default_value
    return None
def __gather_emissive_texture(blender_material, export_settings):
    emissive = gltf2_blender_get.get_socket(blender_material, "Emissive")
    if emissive is None:
        emissive = gltf2_blender_get.get_socket_old(blender_material,
                                                    "Emissive")
    return gltf2_blender_gather_texture_info.gather_texture_info(
        emissive, (emissive, ), export_settings)
Exemplo n.º 6
0
def __gather_metallic_roughness_texture(blender_material, orm_texture,
                                        export_settings):
    if orm_texture is not None:
        texture_input = orm_texture
    else:
        metallic_socket = gltf2_blender_get.get_socket(blender_material,
                                                       "Metallic")
        roughness_socket = gltf2_blender_get.get_socket(
            blender_material, "Roughness")

        hasMetal = metallic_socket is not None and __has_image_node_from_socket(
            metallic_socket)
        hasRough = roughness_socket is not None and __has_image_node_from_socket(
            roughness_socket)

        if not hasMetal and not hasRough:
            metallic_roughness = gltf2_blender_get.get_socket_old(
                blender_material, "MetallicRoughness")
            if metallic_roughness is None or not __has_image_node_from_socket(
                    metallic_roughness):
                return None
            texture_input = (metallic_roughness, )
        elif not hasMetal:
            texture_input = (roughness_socket, )
        elif not hasRough:
            texture_input = (metallic_socket, )
        else:
            texture_input = (metallic_socket, roughness_socket)

    return gltf2_blender_gather_texture_info.gather_texture_info(
        texture_input, export_settings)
def __gather_base_color_factor(blender_material, export_settings):
    if not blender_material.use_nodes:
        return [*blender_material.diffuse_color[:3], 1.0]

    rgb, alpha = None, None

    alpha_socket = gltf2_blender_get.get_socket(blender_material, "Alpha")
    if isinstance(alpha_socket, bpy.types.NodeSocket):
        if export_settings['gltf_image_format'] != "NONE":
            alpha = gltf2_blender_get.get_factor_from_socket(alpha_socket, kind='VALUE')
        else:
            alpha = gltf2_blender_get.get_const_from_default_value_socket(alpha_socket, kind='VALUE')

    base_color_socket = gltf2_blender_get.get_socket(blender_material, "Base Color")
    if base_color_socket is None:
        base_color_socket = gltf2_blender_get.get_socket(blender_material, "BaseColor")
    if base_color_socket is None:
        base_color_socket = gltf2_blender_get.get_socket_old(blender_material, "BaseColorFactor")
    if isinstance(base_color_socket, bpy.types.NodeSocket):
        if export_settings['gltf_image_format'] != "NONE":
            rgb = gltf2_blender_get.get_factor_from_socket(base_color_socket, kind='RGB')
        else:
            rgb = gltf2_blender_get.get_const_from_default_value_socket(base_color_socket, kind='RGB')

    if rgb is None: rgb = [1.0, 1.0, 1.0]
    if alpha is None: alpha = 1.0

    rgba = [*rgb, alpha]

    if rgba == [1, 1, 1, 1]: return None
    return rgba
Exemplo n.º 8
0
def __gather_metallic_factor(blender_material, export_settings):
    metallic_socket = gltf2_blender_get.get_socket(blender_material, "Metallic")
    if metallic_socket is None:
        metallic_socket = gltf2_blender_get.get_socket_old(blender_material, "MetallicFactor")
    if isinstance(metallic_socket, bpy.types.NodeSocket) and not metallic_socket.is_linked:
        return metallic_socket.default_value
    return None
Exemplo n.º 9
0
def __gather_normal_texture(blender_material, export_settings):
    normal = gltf2_blender_get.get_socket(blender_material, "Normal")
    if normal is None:
        normal = gltf2_blender_get.get_socket_old(blender_material, "Normal")
    return gltf2_blender_gather_texture_info.gather_material_normal_texture_info_class(
        (normal,),
        export_settings)
def __gather_normal_texture(blender_material, export_settings):
    normal = gltf2_blender_get.get_socket(blender_material, "Normal")
    if normal is None:
        normal = gltf2_blender_get.get_socket_old(blender_material, "Normal")
    normal_texture, use_active_uvmap_normal = gltf2_blender_gather_texture_info.gather_material_normal_texture_info_class(
        normal, (normal, ), export_settings)
    return normal_texture, ["normalTexture"
                            ] if use_active_uvmap_normal else None
def __gather_emissive_texture(blender_material, export_settings):
    emissive = gltf2_blender_get.get_socket(blender_material, "Emissive")
    if emissive is None:
        emissive = gltf2_blender_get.get_socket_old(blender_material,
                                                    "Emissive")
    emissive_texture, use_actives_uvmap_emissive = gltf2_blender_gather_texture_info.gather_texture_info(
        emissive, (emissive, ), export_settings)
    return emissive_texture, ["emissiveTexture"
                              ] if use_actives_uvmap_emissive else None
def __gather_occlusion_texture(blender_material, orm_texture, export_settings):
    occlusion = gltf2_blender_get.get_socket(blender_material, "Occlusion")
    if occlusion is None:
        occlusion = gltf2_blender_get.get_socket_old(blender_material,
                                                     "Occlusion")
    occlusion_texture, use_active_uvmap_occlusion = gltf2_blender_gather_texture_info.gather_material_occlusion_texture_info_class(
        occlusion, orm_texture or (occlusion, ), export_settings)
    return occlusion_texture, ["occlusionTexture"
                               ] if use_active_uvmap_occlusion else None
Exemplo n.º 13
0
def __gather_double_sided(blender_material, mesh_double_sided, export_settings):
    if mesh_double_sided:
        return True

    old_double_sided_socket = gltf2_blender_get.get_socket_old(blender_material, "DoubleSided")
    if old_double_sided_socket is not None and\
            not old_double_sided_socket.is_linked and\
            old_double_sided_socket.default_value > 0.5:
        return True
    return None
def __gather_roughness_factor(blender_material, export_settings):
    if not blender_material.use_nodes:
        return blender_material.roughness

    roughness_socket = gltf2_blender_get.get_socket(blender_material, "Roughness")
    if roughness_socket is None:
        roughness_socket = gltf2_blender_get.get_socket_old(blender_material, "RoughnessFactor")
    if isinstance(roughness_socket, bpy.types.NodeSocket):
        fac = gltf2_blender_get.get_factor_from_socket(roughness_socket, kind='VALUE')
        return fac if fac != 1 else None
    return None
def __gather_metallic_factor(blender_material, export_settings):
    if not blender_material.use_nodes:
        return blender_material.metallic

    metallic_socket = gltf2_blender_get.get_socket(blender_material, "Metallic")
    if metallic_socket is None:
        metallic_socket = gltf2_blender_get.get_socket_old(blender_material, "MetallicFactor")
    if isinstance(metallic_socket, bpy.types.NodeSocket):
        fac = gltf2_blender_get.get_factor_from_socket(metallic_socket, kind='VALUE')
        return fac if fac != 1 else None
    return None
Exemplo n.º 16
0
def __gather_emissive_factor(blender_material, export_settings):
    emissive_socket = gltf2_blender_get.get_socket(blender_material, "Emissive")
    if emissive_socket is None:
        emissive_socket = gltf2_blender_get.get_socket_old(blender_material, "EmissiveFactor")
    if isinstance(emissive_socket, bpy.types.NodeSocket):
        if emissive_socket.is_linked:
            # In glTF, the default emissiveFactor is all zeros, so if an emission texture is connected,
            # we have to manually set it to all ones.
            return [1.0, 1.0, 1.0]
        else:
            return list(emissive_socket.default_value)[0:3]
    return None
Exemplo n.º 17
0
def __gather_base_color_factor(blender_material, export_settings):
    alpha_socket = gltf2_blender_get.get_socket(blender_material, "Alpha")
    alpha = alpha_socket.default_value if alpha_socket is not None and not alpha_socket.is_linked else 1.0

    base_color_socket = gltf2_blender_get.get_socket(blender_material,
                                                     "Base Color")
    if base_color_socket is None:
        base_color_socket = gltf2_blender_get.get_socket(
            blender_material, "BaseColor")
    if base_color_socket is None:
        base_color_socket = gltf2_blender_get.get_socket_old(
            blender_material, "BaseColorFactor")
    if base_color_socket is None:
        base_color_socket = gltf2_blender_get.get_socket(
            blender_material, "Background")
    if not isinstance(base_color_socket, bpy.types.NodeSocket):
        return None
    if not base_color_socket.is_linked:
        return list(base_color_socket.default_value)[:3] + [alpha]

    texture_node = __get_tex_from_socket(base_color_socket)
    if texture_node is None:
        return None

    def is_valid_multiply_node(node):
        return isinstance(node, bpy.types.ShaderNodeMixRGB) and \
               node.blend_type == "MULTIPLY" and \
               len(node.inputs) == 3

    multiply_node = next((link.from_node for link in texture_node.path
                          if is_valid_multiply_node(link.from_node)), None)
    if multiply_node is None:
        return None

    def is_factor_socket(socket):
        return isinstance(socket, bpy.types.NodeSocketColor) and \
               (not socket.is_linked or socket.links[0] not in texture_node.path)

    factor_socket = next(
        (socket
         for socket in multiply_node.inputs if is_factor_socket(socket)), None)
    if factor_socket is None:
        return None

    if factor_socket.is_linked:
        print_console(
            "WARNING",
            "BaseColorFactor only supports sockets without links (in Node '{}')."
            .format(multiply_node.name))
        return None

    return list(factor_socket.default_value)[:3] + [alpha]
Exemplo n.º 18
0
def __gather_emissive_factor(blender_material, export_settings):
    emissive_socket = gltf2_blender_get.get_socket(blender_material, "Emissive")
    if emissive_socket is None:
        emissive_socket = gltf2_blender_get.get_socket_old(blender_material, "EmissiveFactor")
    if isinstance(emissive_socket, bpy.types.NodeSocket):
        fac = gltf2_blender_get.get_factor_from_socket(emissive_socket, kind='RGB')
        if fac is None and emissive_socket.is_linked:
            # In glTF, the default emissiveFactor is all zeros, so if an emission texture is connected,
            # we have to manually set it to all ones.
            fac = [1.0, 1.0, 1.0]
        if fac == [0, 0, 0]: fac = None
        return fac
    return None
Exemplo n.º 19
0
def __gather_base_color_texture(blender_material, export_settings):
    base_color_socket = gltf2_blender_get.get_socket(blender_material, "Base Color")
    if base_color_socket is None:
        base_color_socket = gltf2_blender_get.get_socket(blender_material, "BaseColor")
    if base_color_socket is None:
        base_color_socket = gltf2_blender_get.get_socket_old(blender_material, "BaseColor")
    if base_color_socket is None:
        base_color_socket = gltf2_blender_get.get_socket(blender_material, "Background")

    alpha_socket = gltf2_blender_get.get_socket(blender_material, "Alpha")
    if alpha_socket is not None and alpha_socket.is_linked:
        inputs = (base_color_socket, alpha_socket, )
    else:
        inputs = (base_color_socket,)

    return gltf2_blender_gather_texture_info.gather_texture_info(inputs, export_settings)
def __gather_base_color_texture(blender_material, export_settings):
    base_color_socket = gltf2_blender_get.get_socket(blender_material, "Base Color")
    if base_color_socket is None:
        base_color_socket = gltf2_blender_get.get_socket(blender_material, "BaseColor")
    if base_color_socket is None:
        base_color_socket = gltf2_blender_get.get_socket_old(blender_material, "BaseColor")

    alpha_socket = gltf2_blender_get.get_socket(blender_material, "Alpha")

    # keep sockets that have some texture : color and/or alpha
    inputs = tuple(
        socket for socket in [base_color_socket, alpha_socket]
        if socket is not None and __has_image_node_from_socket(socket)
    )
    if not inputs:
        return None, None

    return gltf2_blender_gather_texture_info.gather_texture_info(inputs[0], inputs, export_settings)
def __gather_emissive_factor(blender_material, export_settings):
    emissive_socket = gltf2_blender_get.get_socket(blender_material,
                                                   "Emissive")
    if emissive_socket is None:
        emissive_socket = gltf2_blender_get.get_socket_old(
            blender_material, "EmissiveFactor")
    if isinstance(emissive_socket, bpy.types.NodeSocket):
        if export_settings['gltf_image_format'] != "NONE":
            factor = gltf2_blender_get.get_factor_from_socket(emissive_socket,
                                                              kind='RGB')
        else:
            factor = gltf2_blender_get.get_const_from_default_value_socket(
                emissive_socket, kind='RGB')

        if factor is None and emissive_socket.is_linked:
            # In glTF, the default emissiveFactor is all zeros, so if an emission texture is connected,
            # we have to manually set it to all ones.
            factor = [1.0, 1.0, 1.0]

        if factor is None: factor = [0.0, 0.0, 0.0]

        # Handle Emission Strength
        strength_socket = None
        if emissive_socket.node.type == 'EMISSION':
            strength_socket = emissive_socket.node.inputs['Strength']
        elif 'Emission Strength' in emissive_socket.node.inputs:
            strength_socket = emissive_socket.node.inputs['Emission Strength']
        strength = (gltf2_blender_get.get_const_from_socket(strength_socket,
                                                            kind='VALUE')
                    if strength_socket is not None else None)
        if strength is not None:
            factor = [f * strength for f in factor]

        # Clamp to range [0,1]
        factor = [min(1.0, f) for f in factor]

        if factor == [0, 0, 0]: factor = None

        return factor

    return None