def preview_lightmap(self, context):
    preview_lightmap = context.scene.texture_settings.preview_lightmap
    vis_mats = material_functions.get_all_visible_materials()
    for material in vis_mats:
        if (not material.node_tree):
            continue
        nodes = material.node_tree.nodes
        lightmap_node = nodes.get('Lightmap')
        if (lightmap_node is None):
            continue
        pbr_node = node_functions.get_pbr_node(material)
        if (pbr_node is None):
            print((('\n ' + material.name) + ' has no PBR Node \n'))
            continue
        base_color_input = node_functions.get_pbr_inputs(pbr_node)['base_color_input']
        emission_input = node_functions.get_pbr_inputs(pbr_node)['emission_input']
        lightmap_output = lightmap_node.outputs['Color']
        if preview_lightmap:
            mix_node_name = 'Mulitply Lightmap'
            mix_node = node_functions.add_node(material, constants.Shader_Node_Types.mix, mix_node_name)
            mix_node.blend_type = 'MULTIPLY'
            mix_node.inputs[0].default_value = 1
            pos_offset = mathutils.Vector(((- 200), 200))
            mix_node.location = (pbr_node.location + pos_offset)
            mix_node_input1 = mix_node.inputs['Color1']
            mix_node_input2 = mix_node.inputs['Color2']
            mix_node_output = mix_node.outputs['Color']
            if base_color_input.is_linked:
                node_before_base_color = base_color_input.links[0].from_node
                if (not (node_before_base_color.name == mix_node_name)):
                    node_functions.make_link(material, node_before_base_color.outputs['Color'], mix_node_input1)
                    node_functions.make_link(material, lightmap_output, mix_node_input2)
                    node_functions.make_link(material, mix_node_output, base_color_input)
            else:
                mix_node_input1.default_value = base_color_input.default_value
                node_functions.make_link(material, lightmap_output, mix_node_input2)
                node_functions.make_link(material, mix_node_output, base_color_input)
            node_functions.remove_link(material, lightmap_output, emission_input)
        if (not preview_lightmap):
            mix_node = nodes.get('Mulitply Lightmap')
            if (mix_node is not None):
                color_input_connections = len(mix_node.inputs['Color1'].links)
                if (color_input_connections == 0):
                    node_functions.remove_node(material, mix_node.name)
                else:
                    node_functions.remove_reconnect_node(material, mix_node.name)
            node_functions.link_pbr_to_output(material, pbr_node)
def preview_lightmap(self, context):
    preview_lightmap = context.scene.texture_settings.preview_lightmap
    vis_mats = material_functions.get_all_visible_materials()
    for material in vis_mats:
        if (not material.node_tree):
            continue
        nodes = material.node_tree.nodes
        lightmap_node = nodes.get('Lightmap')
        if (lightmap_node is None):
            continue
        pbr_node = node_functions.get_pbr_node(material)
        if (pbr_node is None):
            print((('\n ' + material.name) + ' has no PBR Node \n'))
            continue
        base_color_input = node_functions.get_pbr_inputs(pbr_node)['base_color_input']
        emission_input = node_functions.get_pbr_inputs(pbr_node)['emission_input']
        lightmap_output = lightmap_node.outputs['Color']
        if preview_lightmap:
            mix_node_name = 'Mulitply Lightmap'
            mix_node = node_functions.add_node(material, constants.Shader_Node_Types.mix, mix_node_name)
            mix_node.blend_type = 'MULTIPLY'
            mix_node.inputs[0].default_value = 1
            pos_offset = mathutils.Vector(((- 200), 200))
            mix_node.location = (pbr_node.location + pos_offset)
            mix_node_input1 = mix_node.inputs['Color1']
            mix_node_input2 = mix_node.inputs['Color2']
            mix_node_output = mix_node.outputs['Color']
            if base_color_input.is_linked:
                node_before_base_color = base_color_input.links[0].from_node
                if (not (node_before_base_color.name == mix_node_name)):
                    node_functions.make_link(material, node_before_base_color.outputs['Color'], mix_node_input1)
                    node_functions.make_link(material, lightmap_output, mix_node_input2)
                    node_functions.make_link(material, mix_node_output, base_color_input)
            else:
                mix_node_input1.default_value = base_color_input.default_value
                node_functions.make_link(material, lightmap_output, mix_node_input2)
                node_functions.make_link(material, mix_node_output, base_color_input)
            node_functions.remove_link(material, lightmap_output, emission_input)
        if (not preview_lightmap):
            mix_node = nodes.get('Mulitply Lightmap')
            if (mix_node is not None):
                color_input_connections = len(mix_node.inputs['Color1'].links)
                if (color_input_connections == 0):
                    node_functions.remove_node(material, mix_node.name)
                else:
                    node_functions.remove_reconnect_node(material, mix_node.name)
            node_functions.link_pbr_to_output(material, pbr_node)