Skip to content
Snippets Groups Projects
shading.py 82 KiB
Newer Older
  • Learn to ignore specific revisions
  • Maurice Raybaud's avatar
    Maurice Raybaud committed
                    if pat.tex_pattern_type == "hexagon" and num_color < 4:
                        text_strg += "color rgbf<%.4g,%.4g,%.4g,%.4g> \n" % (col_r, col_g, col_b, col_a)
                    if pat.tex_pattern_type == "square" and num_color < 5:
                        text_strg += "color rgbf<%.4g,%.4g,%.4g,%.4g> \n" % (col_r, col_g, col_b, col_a)
                    if pat.tex_pattern_type == "triangular" and num_color < 7:
                        text_strg += "color rgbf<%.4g,%.4g,%.4g,%.4g> \n" % (col_r, col_g, col_b, col_a)
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                text_strg += "[0 color rgbf<0,0,0,1>]\n"
                text_strg += "[1 color rgbf<1,1,1,0>]\n"
            if pat.tex_pattern_type not in {"checker", "hexagon", "square", "triangular", "brick"}:
                text_strg += "} \n"
            if pat.tex_pattern_type == "brick":
                text_strg += "brick_size <%.4g, %.4g, %.4g> mortar %.4g \n" % (
                    pat.brick_size_x,
                    pat.brick_size_y,
                    pat.brick_size_z,
                    pat.brick_mortar,
                )
            text_strg += "%s \n" % mapping_dif
            text_strg += "rotate <%.4g,%.4g,%.4g> \n" % (pat.tex_rot_x, pat.tex_rot_y, pat.tex_rot_z)
            text_strg += "turbulence <%.4g,%.4g,%.4g> \n" % (
                pat.warp_turbulence_x,
                pat.warp_turbulence_y,
                pat.warp_turbulence_z,
            )
            text_strg += "octaves %s \n" % pat.modifier_octaves
            text_strg += "lambda %.4g \n" % pat.modifier_lambda
            text_strg += "omega %.4g \n" % pat.modifier_omega
            text_strg += "frequency %.4g \n" % pat.modifier_frequency
            text_strg += "phase %.4g \n" % pat.modifier_phase
            text_strg += "}\n\n"
            text_strg += "#declare f%s=\n" % pat_name
            text_strg += "function{pigment{%s}}\n" % pat_name
            text_strg += "\n"
        return text_strg
    
    
    def string_strip_hyphen(name):
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
        """POV naming schemes like to conform to most restrictive charsets."""
    
        return name.replace("-", "")
    
    # WARNING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    
    def write_nodes(pov_mat_name, ntree, file):
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
        """Translate Blender node trees to pov and write them to file."""
        # such function local inlined import are official guidelines
        # of Blender Foundation to lighten addons footprint at startup
        from os import path
    
        declare_nodes = []
        scene = bpy.context.scene
    
        for node in ntree.nodes:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            pov_node_name = string_strip_hyphen(bpy.path.clean_name(node.name)) + "_%s" % pov_mat_name
    
            if node.bl_idname == "PovrayFinishNode" and node.outputs["Finish"].is_linked:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                file.write("#declare %s = finish {\n" % pov_node_name)
                emission = node.inputs["Emission"].default_value
    
                if node.inputs["Emission"].is_linked:
                    pass
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                file.write("    emission %.4g\n" % emission)
    
                for link in ntree.links:
                    if link.to_node == node:
    
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                        if link.from_node.bl_idname == "PovrayDiffuseNode":
                            intensity = 0
                            albedo = ""
                            brilliance = 0
                            crand = 0
    
                            if link.from_node.inputs["Intensity"].is_linked:
                                pass
                            else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                intensity = link.from_node.inputs["Intensity"].default_value
    
                            if link.from_node.inputs["Albedo"].is_linked:
                                pass
                            else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                if link.from_node.inputs["Albedo"].default_value:
    
                                    albedo = "albedo"
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                            file.write("    diffuse %s %.4g\n" % (albedo, intensity))
    
                            if link.from_node.inputs["Brilliance"].is_linked:
                                pass
                            else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                brilliance = link.from_node.inputs["Brilliance"].default_value
                            file.write("    brilliance %.4g\n" % brilliance)
    
                            if link.from_node.inputs["Crand"].is_linked:
                                pass
                            else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                crand = link.from_node.inputs["Crand"].default_value
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                file.write("    crand %.4g\n" % crand)
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                        if link.from_node.bl_idname == "PovraySubsurfaceNode":
    
                            if scene.povray.sslt_enable:
                                energy = 0
                                r = g = b = 0
                                if link.from_node.inputs["Translucency"].is_linked:
                                    pass
                                else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                    r, g, b, a = link.from_node.inputs["Translucency"].default_value[:]
    
                                if link.from_node.inputs["Energy"].is_linked:
                                    pass
                                else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                    energy = link.from_node.inputs["Energy"].default_value
                                file.write(
                                    "    subsurface { translucency <%.4g,%.4g,%.4g>*%s }\n"
                                    % (r, g, b, energy)
                                )
    
                        if link.from_node.bl_idname in {"PovraySpecularNode", "PovrayPhongNode"}:
                            intensity = 0
                            albedo = ""
                            roughness = 0
                            metallic = 0
                            phong_size = 0
                            highlight = "specular"
    
                            if link.from_node.inputs["Intensity"].is_linked:
                                pass
                            else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                intensity = link.from_node.inputs["Intensity"].default_value
    
    
                            if link.from_node.inputs["Albedo"].is_linked:
                                pass
                            else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                if link.from_node.inputs["Albedo"].default_value:
    
                                    albedo = "albedo"
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                            if link.from_node.bl_idname in {"PovrayPhongNode"}:
                                highlight = "phong"
                            file.write("    %s %s %.4g\n" % (highlight, albedo, intensity))
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                            if link.from_node.bl_idname in {"PovraySpecularNode"}:
    
                                if link.from_node.inputs["Roughness"].is_linked:
                                    pass
                                else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                    roughness = link.from_node.inputs["Roughness"].default_value
                                file.write("    roughness %.6g\n" % roughness)
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                            if link.from_node.bl_idname in {"PovrayPhongNode"}:
    
                                if link.from_node.inputs["Size"].is_linked:
                                    pass
                                else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                    phong_size = link.from_node.inputs["Size"].default_value
                                file.write("    phong_size %s\n" % phong_size)
    
    
                            if link.from_node.inputs["Metallic"].is_linked:
                                pass
                            else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                metallic = link.from_node.inputs["Metallic"].default_value
                            file.write("    metallic %.4g\n" % metallic)
    
                        if link.from_node.bl_idname in {"PovrayMirrorNode"}:
                            file.write("    reflection {\n")
                            color = None
                            exponent = 0
                            metallic = 0
                            falloff = 0
                            fresnel = ""
                            conserve = ""
    
                            if link.from_node.inputs["Color"].is_linked:
                                pass
                            else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                color = link.from_node.inputs["Color"].default_value[:]
    
                            file.write(
                                "    <%.4g,%.4g,%.4g>\n"
                                % (color[0], color[1], color[2])
                            )
    
    
                            if link.from_node.inputs["Exponent"].is_linked:
                                pass
                            else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                exponent = link.from_node.inputs["Exponent"].default_value
                            file.write("    exponent %.4g\n" % exponent)
    
    
                            if link.from_node.inputs["Falloff"].is_linked:
                                pass
                            else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                falloff = link.from_node.inputs["Falloff"].default_value
                            file.write("    falloff %.4g\n" % falloff)
    
    
                            if link.from_node.inputs["Metallic"].is_linked:
                                pass
                            else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                metallic = link.from_node.inputs["Metallic"].default_value
                            file.write("    metallic %.4g" % metallic)
    
    
                            if link.from_node.inputs["Fresnel"].is_linked:
                                pass
                            else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                if link.from_node.inputs["Fresnel"].default_value:
                                    fresnel = "fresnel"
    
    
                            if link.from_node.inputs["Conserve energy"].is_linked:
                                pass
                            else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                if link.from_node.inputs["Conserve energy"].default_value:
                                    conserve = "conserve_energy"
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                            file.write("    %s}\n    %s\n" % (fresnel, conserve))
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                        if link.from_node.bl_idname == "PovrayAmbientNode":
                            ambient = (0, 0, 0)
    
                            if link.from_node.inputs["Ambient"].is_linked:
                                pass
                            else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                ambient = link.from_node.inputs["Ambient"].default_value[:]
                            file.write("    ambient <%.4g,%.4g,%.4g>\n" % ambient)
    
                        if link.from_node.bl_idname in {"PovrayIridescenceNode"}:
                            file.write("    irid {\n")
                            amount = 0
                            thickness = 0
                            turbulence = 0
    
                            if link.from_node.inputs["Amount"].is_linked:
                                pass
                            else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                amount = link.from_node.inputs["Amount"].default_value
                            file.write("    %.4g\n" % amount)
    
    
                            if link.from_node.inputs["Thickness"].is_linked:
                                pass
                            else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                exponent = link.from_node.inputs["Thickness"].default_value
                            file.write("    thickness %.4g\n" % thickness)
    
    
                            if link.from_node.inputs["Turbulence"].is_linked:
                                pass
                            else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                falloff = link.from_node.inputs["Turbulence"].default_value
                            file.write("    turbulence %.4g}\n" % turbulence)
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                file.write("}\n")
    
    
        for node in ntree.nodes:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            pov_node_name = string_strip_hyphen(bpy.path.clean_name(node.name)) + "_%s" % pov_mat_name
    
            if node.bl_idname == "PovrayTransformNode" and node.outputs["Transform"].is_linked:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                tx = node.inputs["Translate x"].default_value
                ty = node.inputs["Translate y"].default_value
                tz = node.inputs["Translate z"].default_value
                rx = node.inputs["Rotate x"].default_value
                ry = node.inputs["Rotate y"].default_value
                rz = node.inputs["Rotate z"].default_value
                sx = node.inputs["Scale x"].default_value
                sy = node.inputs["Scale y"].default_value
                sz = node.inputs["Scale z"].default_value
                file.write(
                    "#declare %s = transform {\n"
                    "    translate<%.4g,%.4g,%.4g>\n"
                    "    rotate<%.4g,%.4g,%.4g>\n"
                    "    scale<%.4g,%.4g,%.4g>}\n" % (pov_node_name, tx, ty, tz, rx, ry, rz, sx, sy, sz)
                )
    
    
        for node in ntree.nodes:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            pov_node_name = string_strip_hyphen(bpy.path.clean_name(node.name)) + "_%s" % pov_mat_name
    
            if node.bl_idname == "PovrayColorImageNode" and node.outputs["Pigment"].is_linked:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                declare_nodes.append(node.name)
    
                if node.image == "":
    
                    file.write("#declare %s = pigment { color rgb 0.8}\n" % pov_node_name)
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                    im = bpy.data.images[node.image]
                    if im.filepath and path.exists(bpy.path.abspath(im.filepath)):  # (os.path)
    
                        transform = ""
                        for link in ntree.links:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                            if (
                                link.from_node.bl_idname == "PovrayTransformNode"
                                and link.to_node == node
                            ):
                                pov_trans_name = (
                                    string_strip_hyphen(bpy.path.clean_name(link.from_node.name))
                                    + "_%s" % pov_mat_name
                                )
                                transform = "transform {%s}" % pov_trans_name
                        uv = ""
                        if node.map_type == "uv_mapping":
                            uv = "uv_mapping"
                        filepath = bpy.path.abspath(im.filepath)
                        file.write("#declare %s = pigment {%s image_map {\n" % (pov_node_name, uv))
                        premul = "off"
    
                        if node.premultiplied:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                            premul = "on"
                        once = ""
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                            once = "once"
                        file.write(
                            '    "%s"\n    gamma %.6g\n    premultiplied %s\n'
                            % (filepath, node.inputs["Gamma"].default_value, premul)
                        )
                        file.write("    %s\n" % once)
                        if node.map_type != "uv_mapping":
    
                            file.write("    map_type %s\n" % node.map_type)
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                        file.write(
                            "    interpolate %s\n    filter all %.4g\n    transmit all %.4g\n"
                            % (
                                node.interpolate,
                                node.inputs["Filter"].default_value,
                                node.inputs["Transmit"].default_value,
                            )
                        )
                        file.write("    }\n")
                        file.write("    %s\n" % transform)
                        file.write("    }\n")
    
    
        for node in ntree.nodes:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            pov_node_name = string_strip_hyphen(bpy.path.clean_name(node.name)) + "_%s" % pov_mat_name
    
            if node.bl_idname == "PovrayImagePatternNode" and node.outputs["Pattern"].is_linked:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                declare_nodes.append(node.name)
    
                if node.image != "":
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                    im = bpy.data.images[node.image]
                    if im.filepath and path.exists(bpy.path.abspath(im.filepath)):
    
                        transform = ""
                        for link in ntree.links:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                            if (
                                link.from_node.bl_idname == "PovrayTransformNode"
                                and link.to_node == node
                            ):
                                pov_trans_name = (
                                    string_strip_hyphen(bpy.path.clean_name(link.from_node.name))
                                    + "_%s" % pov_mat_name
                                )
                                transform = "transform {%s}" % pov_trans_name
                        uv = ""
                        if node.map_type == "uv_mapping":
                            uv = "uv_mapping"
                        filepath = bpy.path.abspath(im.filepath)
                        file.write("#macro %s() %s image_pattern {\n" % (pov_node_name, uv))
                        premul = "off"
    
                        if node.premultiplied:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                            premul = "on"
                        once = ""
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                            once = "once"
                        file.write(
                            '    "%s"\n    gamma %.6g\n    premultiplied %s\n'
                            % (filepath, node.inputs["Gamma"].default_value, premul)
                        )
                        file.write("    %s\n" % once)
                        if node.map_type != "uv_mapping":
    
                            file.write("    map_type %s\n" % node.map_type)
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                        file.write("    interpolate %s\n" % node.interpolate)
                        file.write("    }\n")
                        file.write("    %s\n" % transform)
                        file.write("#end\n")
    
    
        for node in ntree.nodes:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            pov_node_name = string_strip_hyphen(bpy.path.clean_name(node.name)) + "_%s" % pov_mat_name
    
            if node.bl_idname == "PovrayBumpMapNode" and node.outputs["Normal"].is_linked:
                if node.image != "":
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                    im = bpy.data.images[node.image]
                    if im.filepath and path.exists(bpy.path.abspath(im.filepath)):
    
                        transform = ""
                        for link in ntree.links:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                            if (
                                link.from_node.bl_idname == "PovrayTransformNode"
                                and link.to_node == node
                            ):
                                pov_trans_name = (
                                    string_strip_hyphen(bpy.path.clean_name(link.from_node.name))
                                    + "_%s" % pov_mat_name
                                )
                                transform = "transform {%s}" % pov_trans_name
                        uv = ""
                        if node.map_type == "uv_mapping":
                            uv = "uv_mapping"
                        filepath = bpy.path.abspath(im.filepath)
                        file.write("#declare %s = normal {%s bump_map {\n" % (pov_node_name, uv))
                        once = ""
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                            once = "once"
                        file.write('    "%s"\n' % filepath)
                        file.write("    %s\n" % once)
                        if node.map_type != "uv_mapping":
    
                            file.write("    map_type %s\n" % node.map_type)
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                        bump_size = node.inputs["Normal"].default_value
    
                        if node.inputs["Normal"].is_linked:
                            pass
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                        file.write(
                            "    interpolate %s\n    bump_size %.4g\n" % (node.interpolate, bump_size)
                        )
                        file.write("    }\n")
                        file.write("    %s\n" % transform)
                        file.write("    }\n")
                        declare_nodes.append(node.name)
    
    
        for node in ntree.nodes:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            pov_node_name = string_strip_hyphen(bpy.path.clean_name(node.name)) + "_%s" % pov_mat_name
    
            if node.bl_idname == "PovrayPigmentNode" and node.outputs["Pigment"].is_linked:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                declare_nodes.append(node.name)
                r, g, b = node.inputs["Color"].default_value[:]
                f = node.inputs["Filter"].default_value
                t = node.inputs["Transmit"].default_value
    
                if node.inputs["Color"].is_linked:
                    pass
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                file.write(
                    "#declare %s = pigment{color srgbft <%.4g,%.4g,%.4g,%.4g,%.4g>}\n"
                    % (pov_node_name, r, g, b, f, t)
                )
    
    
        for node in ntree.nodes:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            pov_node_name = string_strip_hyphen(bpy.path.clean_name(node.name)) + "_%s" % pov_mat_name
    
            if node.bl_idname == "PovrayTextureNode" and node.outputs["Texture"].is_linked:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                declare_nodes.append(node.name)
                r, g, b = node.inputs["Pigment"].default_value[:]
                pov_col_name = "color rgb <%.4g,%.4g,%.4g>" % (r, g, b)
    
                if node.inputs["Pigment"].is_linked:
                    for link in ntree.links:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                        if link.to_node == node and link.to_socket.name == "Pigment":
                            pov_col_name = (
                                string_strip_hyphen(bpy.path.clean_name(link.from_node.name))
                                + "_%s" % pov_mat_name
                            )
                file.write("#declare %s = texture{\n    pigment{%s}\n" % (pov_node_name, pov_col_name))
    
                if node.inputs["Normal"].is_linked:
                    for link in ntree.links:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                        if (
                            link.to_node == node
                            and link.to_socket.name == "Normal"
                            and link.from_node.name in declare_nodes
                        ):
                            pov_nor_name = (
                                string_strip_hyphen(bpy.path.clean_name(link.from_node.name))
                                + "_%s" % pov_mat_name
                            )
                            file.write("    normal{%s}\n" % pov_nor_name)
    
                if node.inputs["Finish"].is_linked:
                    for link in ntree.links:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                        if link.to_node == node and link.to_socket.name == "Finish":
                            pov_fin_name = (
                                string_strip_hyphen(bpy.path.clean_name(link.from_node.name))
                                + "_%s" % pov_mat_name
                            )
                            file.write("    finish{%s}\n" % pov_fin_name)
                file.write("}\n")
                declare_nodes.append(node.name)
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
        for i in range(0, len(ntree.nodes)):
    
            for node in ntree.nodes:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                if node.bl_idname in {"ShaderNodeGroup", "ShaderTextureMapNode"}:
    
                    for output in node.outputs:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                        if (
                            output.name == "Texture"
                            and output.is_linked
                            and (node.name not in declare_nodes)
                        ):
                            declare = True
    
                            for link in ntree.links:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                if link.to_node == node and link.to_socket.name not in {
                                    "",
                                    "Color ramp",
                                    "Mapping",
                                    "Transform",
                                    "Modifier",
                                }:
                                    if link.from_node.name not in declare_nodes:
                                        declare = False
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                pov_node_name = (
                                    string_strip_hyphen(bpy.path.clean_name(node.name))
                                    + "_%s" % pov_mat_name
                                )
                                uv = ""
                                warp = ""
    
                                for link in ntree.links:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                    if (
                                        link.to_node == node
                                        and link.from_node.bl_idname == "PovrayMappingNode"
                                        and link.from_node.warp_type != "NONE"
                                    ):
    
                                        w_type = link.from_node.warp_type
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                        if w_type == "uv_mapping":
                                            uv = "uv_mapping"
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                            tor = ""
                                            if w_type == "toroidal":
                                                tor = (
                                                    "major_radius %.4g"
                                                    % link.from_node.warp_tor_major_radius
                                                )
                                            orient = link.from_node.warp_orientation
                                            exp = link.from_node.warp_dist_exp
                                            warp = "warp{%s orientation %s dist_exp %.4g %s}" % (
                                                w_type,
                                                orient,
                                                exp,
                                                tor,
                                            )
                                            if link.from_node.warp_type == "planar":
                                                warp = "warp{%s %s %.4g}" % (w_type, orient, exp)
                                            if link.from_node.warp_type == "cubic":
                                                warp = "warp{%s}" % w_type
                                file.write("#declare %s = texture {%s\n" % (pov_node_name, uv))
                                pattern = node.inputs[0].default_value
                                advanced = ""
    
                                if node.inputs[0].is_linked:
                                    for link in ntree.links:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                        if (
                                            link.to_node == node
                                            and link.from_node.bl_idname == "ShaderPatternNode"
                                        ):
    
                                            # ------------ advanced ------------------------- #
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                            lfn = link.from_node
                                            pattern = lfn.pattern
                                            if pattern == "agate":
                                                advanced = "agate_turb %.4g" % lfn.agate_turb
                                            if pattern == "crackle":
                                                advanced = "form <%.4g,%.4g,%.4g>" % (
                                                    lfn.crackle_form_x,
                                                    lfn.crackle_form_y,
                                                    lfn.crackle_form_z,
                                                )
                                                advanced += " metric %.4g" % lfn.crackle_metric
    
                                                if lfn.crackle_solid:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                                    advanced += " solid"
                                            if pattern in {"spiral1", "spiral2"}:
                                                advanced = "%.4g" % lfn.spiral_arms
                                            if pattern in {"tiling"}:
                                                advanced = "%.4g" % lfn.tiling_number
                                            if pattern in {"gradient"}:
                                                advanced = "%s" % lfn.gradient_orient
                                        if (
                                            link.to_node == node
                                            and link.from_node.bl_idname == "PovrayImagePatternNode"
                                        ):
                                            pov_macro_name = (
                                                string_strip_hyphen(
                                                    bpy.path.clean_name(link.from_node.name)
                                                )
                                                + "_%s" % pov_mat_name
                                            )
                                            pattern = "%s()" % pov_macro_name
                                file.write("    %s %s %s\n" % (pattern, advanced, warp))
    
                                repeat = ""
    
                                for link in ntree.links:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                    if (
                                        link.to_node == node
                                        and link.from_node.bl_idname == "PovrayMultiplyNode"
                                    ):
    
                                        if link.from_node.amount_x > 1:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                            repeat += "warp{repeat %.4g * x}" % link.from_node.amount_x
    
                                        if link.from_node.amount_y > 1:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                            repeat += " warp{repeat %.4g * y}" % link.from_node.amount_y
    
                                        if link.from_node.amount_z > 1:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                            repeat += " warp{repeat %.4g * z}" % link.from_node.amount_z
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                transform = ""
    
                                for link in ntree.links:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                    if (
                                        link.to_node == node
                                        and link.from_node.bl_idname == "PovrayTransformNode"
                                    ):
                                        pov_trans_name = (
                                            string_strip_hyphen(
                                                bpy.path.clean_name(link.from_node.name)
                                            )
                                            + "_%s" % pov_mat_name
                                        )
                                        transform = "transform {%s}" % pov_trans_name
                                x = 0
                                y = 0
                                z = 0
                                d = 0
                                e = 0
                                f = 0
                                g = 0
                                h = 0
                                modifier = False
    
                                for link in ntree.links:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                    if (
                                        link.to_node == node
                                        and link.from_node.bl_idname == "PovrayModifierNode"
                                    ):
                                        modifier = True
    
                                        if link.from_node.inputs["Turb X"].is_linked:
                                            pass
                                        else:
                                            x = link.from_node.inputs["Turb X"].default_value
    
                                        if link.from_node.inputs["Turb Y"].is_linked:
                                            pass
                                        else:
                                            y = link.from_node.inputs["Turb Y"].default_value
    
                                        if link.from_node.inputs["Turb Z"].is_linked:
                                            pass
                                        else:
                                            z = link.from_node.inputs["Turb Z"].default_value
    
                                        if link.from_node.inputs["Octaves"].is_linked:
                                            pass
                                        else:
                                            d = link.from_node.inputs["Octaves"].default_value
    
                                        if link.from_node.inputs["Lambda"].is_linked:
                                            pass
                                        else:
                                            e = link.from_node.inputs["Lambda"].default_value
    
                                        if link.from_node.inputs["Omega"].is_linked:
                                            pass
                                        else:
                                            f = link.from_node.inputs["Omega"].default_value
    
                                        if link.from_node.inputs["Frequency"].is_linked:
                                            pass
                                        else:
                                            g = link.from_node.inputs["Frequency"].default_value
    
                                        if link.from_node.inputs["Phase"].is_linked:
                                            pass
                                        else:
                                            h = link.from_node.inputs["Phase"].default_value
    
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                turb = "turbulence <%.4g,%.4g,%.4g>" % (x, y, z)
                                octv = "octaves %s" % d
                                lmbd = "lambda %.4g" % e
                                omg = "omega %.4g" % f
                                freq = "frequency %.4g" % g
                                pha = "phase %.4g" % h
    
                                file.write("\n")
                                if pattern not in {
                                    "checker",
                                    "hexagon",
                                    "square",
                                    "triangular",
                                    "brick",
                                }:
                                    file.write("    texture_map {\n")
    
                                if node.inputs["Color ramp"].is_linked:
                                    for link in ntree.links:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                        if (
                                            link.to_node == node
                                            and link.from_node.bl_idname == "ShaderNodeValToRGB"
                                        ):
    
                                            els = link.from_node.color_ramp.elements
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                            n = -1
    
                                            for el in els:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                                n += 1
                                                pov_in_mat_name = string_strip_hyphen(
                                                    bpy.path.clean_name(link.from_node.name)
                                                ) + "_%s_%s" % (n, pov_mat_name)
                                                default = True
    
                                                for ilink in ntree.links:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                                    if (
                                                        ilink.to_node == node
                                                        and ilink.to_socket.name == str(n)
                                                    ):
                                                        default = False
                                                        pov_in_mat_name = (
                                                            string_strip_hyphen(
                                                                bpy.path.clean_name(
                                                                    ilink.from_node.name
                                                                )
                                                            )
                                                            + "_%s" % pov_mat_name
                                                        )
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                                    r, g, b, a = el.color[:]
                                                    file.write(
                                                        "    #declare %s = texture{"
                                                        "pigment{"
                                                        "color srgbt <%.4g,%.4g,%.4g,%.4g>}};\n"
                                                        % (pov_in_mat_name, r, g, b, 1 - a)
                                                    )
                                                file.write(
                                                    "    [%s %s]\n" % (el.position, pov_in_mat_name)
                                                )
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                    els = [[0, 0, 0, 0], [1, 1, 1, 1]]
                                    for t in range(0, 2):
                                        pov_in_mat_name = string_strip_hyphen(
                                            bpy.path.clean_name(link.from_node.name)
                                        ) + "_%s_%s" % (t, pov_mat_name)
                                        default = True
    
                                        for ilink in ntree.links:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                            if ilink.to_node == node and ilink.to_socket.name == str(t):
                                                default = False
                                                pov_in_mat_name = (
                                                    string_strip_hyphen(
                                                        bpy.path.clean_name(ilink.from_node.name)
                                                    )
                                                    + "_%s" % pov_mat_name
                                                )
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                            r, g, b = els[t][1], els[t][2], els[t][3]
                                            if pattern not in {
                                                "checker",
                                                "hexagon",
                                                "square",
                                                "triangular",
                                                "brick",
                                            }:
                                                file.write(
                                                    "    #declare %s = texture{pigment{color rgb <%.4g,%.4g,%.4g>}};\n"
                                                    % (pov_in_mat_name, r, g, b)
                                                )
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                                file.write(
                                                    "    texture{pigment{color rgb <%.4g,%.4g,%.4g>}}\n"
                                                    % (r, g, b)
                                                )
                                        if pattern not in {
                                            "checker",
                                            "hexagon",
                                            "square",
                                            "triangular",
                                            "brick",
                                        }:
                                            file.write("    [%s %s]\n" % (els[t][0], pov_in_mat_name))
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                            if not default:
                                                file.write("    texture{%s}\n" % pov_in_mat_name)
                                if pattern not in {
                                    "checker",
                                    "hexagon",
                                    "square",
                                    "triangular",
                                    "brick",
                                }:
                                    file.write("}\n")
                                if pattern == "brick":
                                    file.write(
                                        "brick_size <%.4g, %.4g, %.4g> mortar %.4g \n"
                                        % (
                                            node.brick_size_x,
                                            node.brick_size_y,
                                            node.brick_size_z,
                                            node.brick_mortar,
                                        )
                                    )
                                file.write("    %s %s" % (repeat, transform))
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                    file.write(
                                        " %s %s %s %s %s %s" % (turb, octv, lmbd, omg, freq, pha)
                                    )
                                file.write("}\n")
                                declare_nodes.append(node.name)
    
    
        for link in ntree.links:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            if link.to_node.bl_idname == "PovrayOutputNode" and link.from_node.name in declare_nodes:
                pov_mat_node_name = (
                    string_strip_hyphen(bpy.path.clean_name(link.from_node.name)) + "_%s" % pov_mat_name
                )
                file.write("#declare %s = %s\n" % (pov_mat_name, pov_mat_node_name))