Skip to content
Snippets Groups Projects
nodes_fn.py 36.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
    # SPDX-License-Identifier: GPL-2.0-or-later
    
    """Translate complex shaders to exported POV textures."""
    
    import bpy
    
    # WARNING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    def write_nodes(pov_mat_name, ntree, file):
        """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
        from .render import string_strip_hyphen
    
        declare_nodes = []
        scene = bpy.context.scene
        for node in ntree.nodes:
            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:
                file.write("#declare %s = finish {\n" % pov_node_name)
                emission = node.inputs["Emission"].default_value
                if node.inputs["Emission"].is_linked:
                    pass
                file.write("    emission %.4g\n" % emission)
                for link in ntree.links:
                    if link.to_node == node:
    
                        if link.from_node.bl_idname == "PovrayDiffuseNode":
                            intensity = 0
                            albedo = ""
                            brilliance = 0
                            crand = 0
                            if link.from_node.inputs["Intensity"].is_linked:
                                pass
                            else:
                                intensity = link.from_node.inputs["Intensity"].default_value
                            if link.from_node.inputs["Albedo"].is_linked:
                                pass
                            else:
                                if link.from_node.inputs["Albedo"].default_value:
                                    albedo = "albedo"
                            file.write("    diffuse %s %.4g\n" % (albedo, intensity))
                            if link.from_node.inputs["Brilliance"].is_linked:
                                pass
                            else:
                                brilliance = link.from_node.inputs["Brilliance"].default_value
                            file.write("    brilliance %.4g\n" % brilliance)
                            if link.from_node.inputs["Crand"].is_linked:
                                pass
                            else:
                                crand = link.from_node.inputs["Crand"].default_value
                            if crand > 0:
                                file.write("    crand %.4g\n" % crand)
    
                        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:
                                    r, g, b, a = link.from_node.inputs["Translucency"].default_value[:]
                                if link.from_node.inputs["Energy"].is_linked:
                                    pass
                                else:
                                    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:
                                intensity = link.from_node.inputs["Intensity"].default_value
    
                            if link.from_node.inputs["Albedo"].is_linked:
                                pass
                            else:
                                if link.from_node.inputs["Albedo"].default_value:
                                    albedo = "albedo"
                            if link.from_node.bl_idname in {"PovrayPhongNode"}:
                                highlight = "phong"
                            file.write("    %s %s %.4g\n" % (highlight, albedo, intensity))
    
                            if link.from_node.bl_idname in {"PovraySpecularNode"}:
                                if link.from_node.inputs["Roughness"].is_linked:
                                    pass
                                else:
                                    roughness = link.from_node.inputs["Roughness"].default_value
                                file.write("    roughness %.6g\n" % roughness)
    
                            if link.from_node.bl_idname in {"PovrayPhongNode"}:
                                if link.from_node.inputs["Size"].is_linked:
                                    pass
                                else:
                                    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:
                                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:
                                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:
                                exponent = link.from_node.inputs["Exponent"].default_value
                            file.write("    exponent %.4g\n" % exponent)
    
                            if link.from_node.inputs["Falloff"].is_linked:
                                pass
                            else:
                                falloff = link.from_node.inputs["Falloff"].default_value
                            file.write("    falloff %.4g\n" % falloff)
    
                            if link.from_node.inputs["Metallic"].is_linked:
                                pass
                            else:
                                metallic = link.from_node.inputs["Metallic"].default_value
                            file.write("    metallic %.4g" % metallic)
    
                            if link.from_node.inputs["Fresnel"].is_linked:
                                pass
                            else:
                                if link.from_node.inputs["Fresnel"].default_value:
                                    fresnel = "fresnel"
    
                            if link.from_node.inputs["Conserve energy"].is_linked:
                                pass
                            else:
                                if link.from_node.inputs["Conserve energy"].default_value:
                                    conserve = "conserve_energy"
    
                            file.write("    %s}\n    %s\n" % (fresnel, conserve))
    
                        if link.from_node.bl_idname == "PovrayAmbientNode":
                            ambient = (0, 0, 0)
                            if link.from_node.inputs["Ambient"].is_linked:
                                pass
                            else:
                                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:
                                amount = link.from_node.inputs["Amount"].default_value
                            file.write("    %.4g\n" % amount)
    
                            if link.from_node.inputs["Thickness"].is_linked:
                                pass
                            else:
                                exponent = link.from_node.inputs["Thickness"].default_value
                            file.write("    thickness %.4g\n" % thickness)
    
                            if link.from_node.inputs["Turbulence"].is_linked:
                                pass
                            else:
                                falloff = link.from_node.inputs["Turbulence"].default_value
                            file.write("    turbulence %.4g}\n" % turbulence)
    
                file.write("}\n")
    
        for node in ntree.nodes:
            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:
                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:
            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:
                declare_nodes.append(node.name)
                if node.image == "":
                    file.write("#declare %s = pigment { color rgb 0.8}\n" % pov_node_name)
                else:
                    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:
                            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:
                            premul = "on"
                        once = ""
                        if node.once:
                            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)
                        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:
            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:
                declare_nodes.append(node.name)
                if node.image != "":
                    im = bpy.data.images[node.image]
                    if im.filepath and path.exists(bpy.path.abspath(im.filepath)):
                        transform = ""
                        for link in ntree.links:
                            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:
                            premul = "on"
                        once = ""
                        if node.once:
                            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)
                        file.write("    interpolate %s\n" % node.interpolate)
                        file.write("    }\n")
                        file.write("    %s\n" % transform)
                        file.write("#end\n")
    
        for node in ntree.nodes:
            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 != "":
                    im = bpy.data.images[node.image]
                    if im.filepath and path.exists(bpy.path.abspath(im.filepath)):
                        transform = ""
                        for link in ntree.links:
                            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 = ""
                        if node.once:
                            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)
                        bump_size = node.inputs["Normal"].default_value
                        if node.inputs["Normal"].is_linked:
                            pass
                        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:
            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:
                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
                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:
            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:
                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:
                        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:
                        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:
                        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)
    
        for i in range(0, len(ntree.nodes)):
            for node in ntree.nodes:
                if node.bl_idname in {"ShaderNodeGroup", "ShaderTextureMapNode"}:
                    for output in node.outputs:
                        if (
                            output.name == "Texture"
                            and output.is_linked
                            and (node.name not in declare_nodes)
                        ):
                            declare = True
                            for link in ntree.links:
                                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
                            if declare:
                                pov_node_name = (
                                    string_strip_hyphen(bpy.path.clean_name(node.name))
                                    + "_%s" % pov_mat_name
                                )
                                uv = ""
                                warp = ""
                                for link in ntree.links:
                                    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
                                        if w_type == "uv_mapping":
                                            uv = "uv_mapping"
                                        else:
                                            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:
                                        if (
                                            link.to_node == node
                                            and link.from_node.bl_idname == "ShaderPatternNode"
                                        ):
                                            # ------------ advanced ------------------------- #
                                            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:
                                                    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:
                                    if (
                                        link.to_node == node
                                        and link.from_node.bl_idname == "PovrayMultiplyNode"
                                    ):
                                        if link.from_node.amount_x > 1:
                                            repeat += "warp{repeat %.4g * x}" % link.from_node.amount_x
                                        if link.from_node.amount_y > 1:
                                            repeat += " warp{repeat %.4g * y}" % link.from_node.amount_y
                                        if link.from_node.amount_z > 1:
                                            repeat += " warp{repeat %.4g * z}" % link.from_node.amount_z
    
                                transform = ""
                                for link in ntree.links:
                                    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:
                                    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
    
                                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:
                                        if (
                                            link.to_node == node
                                            and link.from_node.bl_idname == "ShaderNodeValToRGB"
                                        ):
                                            els = link.from_node.color_ramp.elements
                                            n = -1
                                            for el in els:
                                                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:
                                                    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
                                                        )
                                                if default:
                                                    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)
                                                )
                                else:
                                    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:
                                            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
                                                )
                                        if default:
                                            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)
                                                )
                                            else:
                                                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))
                                        else:
                                            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))
                                if modifier:
                                    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:
            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))