Skip to content
Snippets Groups Projects
shading.py 87.3 KiB
Newer Older
  • Learn to ignore specific revisions
  • Maurice Raybaud's avatar
    Maurice Raybaud committed
                    elif texturesDif and texturesDif.startswith("PAT_"):
    
                        tabWrite("[1 %s]\n" %texturesDif)
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                    tabWrite("}\n")
                    tabWrite("}\n")
                    if texturesAlpha and texturesAlpha.startswith("PAT_"):
                        tabWrite("}\n")
    
                else:
                    if texturesDif and texturesDif.startswith("PAT_"):
    
                        tabWrite("pigment{%s}\n" %texturesDif)
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                    else:
                        tabWrite("pigment {uv_mapping image_map {%s \"%s\" %s}%s}\n" % \
                                 (imageFormat(texturesDif), texturesDif,
                                  (imgGamma + imgMap(t_dif)), mappingDif))
    
                if texturesSpec != "":
                    # Level 1 is no specular
                    tabWrite("finish {%s}\n" % (safety(material_finish, Level=1)))
    
                else:
                    # Level 2 is translated specular
                    tabWrite("finish {%s}\n" % (safety(material_finish, Level=2)))
    
                ## scale 1 rotate y*0
                #imageMap = ("{image_map {%s \"%s\" %s }\n" % \
                #            (imageFormat(textures),textures,imgMap(t_dif)))
                #tabWrite("uv_mapping pigment %s} %s finish {%s}\n" % \
                #         (imageMap,mapping,safety(material_finish)))
                #tabWrite("pigment {uv_mapping image_map {%s \"%s\" %s}%s} " \
                #         "finish {%s}\n" % \
                #         (imageFormat(texturesDif), texturesDif, imgMap(t_dif),
                #          mappingDif, safety(material_finish)))
            if texturesNorm != "":
                ## scale 1 rotate y*0
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                mappingNor =imgMapTransforms(t_nor)
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                if texturesNorm and texturesNorm.startswith("PAT_"):
    
                    tabWrite("normal{function{f%s(x,y,z).grey} bump_size %.4g %s}\n" %(texturesNorm, t_nor.normal_factor, mappingNor))
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                else:
                    tabWrite("normal {uv_mapping bump_map " \
                             "{%s \"%s\" %s  bump_size %.4g }%s}\n" % \
                             (imageFormat(texturesNorm), texturesNorm, imgMap(t_nor),
    
                              t_nor.normal_factor, mappingNor))
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            if texturesSpec != "":
                tabWrite("]\n")
            ##################Second index for mapping specular max value###############
                tabWrite("[1 \n")
    
        if texturesDif == "" and mater.pov.replacement_text == "":
            if texturesAlpha != "":
                mappingAlpha = imgMapTransforms(t_alpha)
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                if texturesAlpha and texturesAlpha.startswith("PAT_"):
    
                    tabWrite("function{f%s(x,y,z).transmit %s}\n" %(texturesAlpha, mappingAlpha))
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                else:
                    tabWrite("pigment {pigment_pattern {uv_mapping image_map" \
                             "{%s \"%s\" %s}%s}\n" % \
                             (imageFormat(texturesAlpha), texturesAlpha, imgMap(t_alpha),
                              mappingAlpha))
                tabWrite("pigment_map {\n")
                tabWrite("[0 color rgbft<0,0,0,1,1>]\n")
                tabWrite("[1 color rgbft<%.3g, %.3g, %.3g, %.3g, %.3g>]\n" % \
                         (col[0], col[1], col[2], povFilter, trans))
                tabWrite("}\n")
                tabWrite("}\n")
    
            else:
                tabWrite("pigment {rgbft<%.3g, %.3g, %.3g, %.3g, %.3g>}\n" % \
                         (col[0], col[1], col[2], povFilter, trans))
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            if texturesSpec != "":
                # Level 3 is full specular
                tabWrite("finish {%s}\n" % (safety(material_finish, Level=3)))
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            elif colored_specular_found:
                # Level 1 is no specular
                tabWrite("finish {%s}\n" % (safety(material_finish, Level=1)))
    
            else:
                # Level 2 is translated specular
                tabWrite("finish {%s}\n" % (safety(material_finish, Level=2)))
    
        elif mater.pov.replacement_text == "":
            mappingDif = imgMapTransforms(t_dif)
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            if texturesAlpha != "":
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                mappingAlpha = imgMapTransforms(t_alpha)
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                if texturesAlpha and texturesAlpha.startswith("PAT_"):
    
                    tabWrite("pigment{pigment_pattern {function{f%s(x,y,z).transmit}%s}\n" %(texturesAlpha, mappingAlpha))
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                else:
                    tabWrite("pigment {pigment_pattern {uv_mapping image_map" \
                             "{%s \"%s\" %s}%s}\n" % \
                             (imageFormat(texturesAlpha), texturesAlpha, imgMap(t_alpha),
                              mappingAlpha))
                tabWrite("pigment_map {\n")
                tabWrite("[0 color rgbft<0,0,0,1,1>]\n")
                if texturesAlpha and texturesAlpha.startswith("PAT_"):
    
                    tabWrite("[1 function{f%s(x,y,z).transmit}%s]\n" %(texturesAlpha, mappingAlpha))
                elif texturesDif and not texturesDif.startswith("PAT_"):
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                    tabWrite("[1 uv_mapping image_map {%s \"%s\" %s} %s]\n" % \
                             (imageFormat(texturesDif), texturesDif,
                              (imgMap(t_dif) + imgGamma), mappingDif))
                elif texturesDif and texturesDif.startswith("PAT_"):
    
                    tabWrite("[1 %s %s]\n" %(texturesDif, mappingDif))
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                tabWrite("}\n")
                tabWrite("}\n")
    
            else:
                if texturesDif and texturesDif.startswith("PAT_"):
    
                    tabWrite("pigment{%s %s}\n" %(texturesDif, mappingDif))
                else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                    tabWrite("pigment {\n")
                    tabWrite("uv_mapping image_map {\n")
                    #tabWrite("%s \"%s\" %s}%s\n" % \
                    #         (imageFormat(texturesDif), texturesDif,
                    #         (imgGamma + imgMap(t_dif)),mappingDif))
                    tabWrite("%s \"%s\" \n" % (imageFormat(texturesDif), texturesDif))
                    tabWrite("%s\n" % (imgGamma + imgMap(t_dif)))
                    tabWrite("}\n")
                    tabWrite("%s\n" % mappingDif)
                    tabWrite("}\n")
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            if texturesSpec != "":
                # Level 3 is full specular
    
                tabWrite("finish {%s}\n" % (safety(material_finish, Level=3)))
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            else:
                # Level 2 is translated specular
                tabWrite("finish {%s}\n" % (safety(material_finish, Level=2)))
    
            ## scale 1 rotate y*0
            #imageMap = ("{image_map {%s \"%s\" %s }" % \
            #            (imageFormat(textures), textures,imgMap(t_dif)))
    
            #tabWrite("\n\t\t\tuv_mapping pigment %s} %s finish {%s}" % \
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            #           (imageMap, mapping, safety(material_finish)))
    
            #tabWrite("\n\t\t\tpigment {uv_mapping image_map " \
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            #           "{%s \"%s\" %s}%s} finish {%s}" % \
            #           (imageFormat(texturesDif), texturesDif,imgMap(t_dif),
            #            mappingDif, safety(material_finish)))
        if texturesNorm != "" and mater.pov.replacement_text == "":
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            mappingNor =imgMapTransforms(t_nor)
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            if texturesNorm and texturesNorm.startswith("PAT_"):
    
                tabWrite("normal{function{f%s(x,y,z).grey} bump_size %.4g %s}\n" %(texturesNorm, t_nor.normal_factor, mappingNor))
    
            else:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                tabWrite("normal {uv_mapping bump_map {%s \"%s\" %s  bump_size %.4g }%s}\n" % \
                         (imageFormat(texturesNorm), texturesNorm, imgMap(t_nor),
    
                          t_nor.normal_factor, mappingNor))
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
        if texturesSpec != "" and mater.pov.replacement_text == "":
            tabWrite("]\n")
    
            tabWrite("}\n")
    
        #End of slope/ior texture_map
    
        if mater.pov.diffuse_shader == 'MINNAERT' and mater.pov.replacement_text == "":
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            tabWrite("]\n")
            tabWrite("}\n")
    
        if mater.pov.diffuse_shader == 'FRESNEL' and mater.pov.replacement_text == "":
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            c = 1
            while (c <= lampCount):
                tabWrite("]\n")
                tabWrite("}\n")
                c += 1
    
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
        # Close first layer of POV "texture" (Blender material)
        tabWrite("}\n")
    
        if ((mater.specular_color.s > 0.0) and (mater.pov.diffuse_shader != 'MINNAERT')):
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            colored_specular_found = True
        else:
            colored_specular_found = False
    
    
        # Write another layered texture using invisible diffuse and metallic trick
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
        # to emulate colored specular highlights
        special_texture_found = False
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
        idx = -1
    
        for t in mater.pov_texture_slots:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            idx += 1
            # index = mater.pov.active_texture_index
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            slot = mater.pov_texture_slots[idx] # [index]
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            povtex = slot.name
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            tex = bpy.data.textures[povtex]
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            if(t and t.use and ((tex.type == 'IMAGE' and tex.image) or tex.type != 'IMAGE') and
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
               (t.use_map_specular or t.use_map_raymir)):
                # Specular mapped textures would conflict with colored specular
                # because POV can't layer over or under pigment patterned textures
                special_texture_found = True
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
        if colored_specular_found and not special_texture_found:
            if comments:
    
                tabWrite("  // colored highlights with a stransparent metallic layer\n")
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            else:
                tabWrite("\n")
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            tabWrite("texture {\n")
            tabWrite("pigment {rgbft<%.3g, %.3g, %.3g, 0, 1>}\n" % \
                             (mater.specular_color[0], mater.specular_color[1], mater.specular_color[2]))
            tabWrite("finish {%s}\n" % (safety(material_finish, Level=2))) # Level 2 is translated spec
    
            texturesNorm = ""
    
            for t in mater.pov_texture_slots:
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
    
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                if t and tex.pov.tex_pattern_type != 'emulator':
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                    proceduralFlag=True
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                    image_filename = string_strip_hyphen(bpy.path.clean_name(tex.name))
                if (t and tex.type == 'IMAGE' and
                        t.use and tex.image and
                        tex.pov.tex_pattern_type == 'emulator'):
    
                    proceduralFlag=False
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                    image_filename = path_image(tex.image)
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                    imgGamma = ""
                    if image_filename:
                        if t.use_map_normal:
                            texturesNorm = image_filename
    
                            # colvalue = t.normal_factor/10  # UNUSED
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                            #textNormName=tex.image.name + ".normal"
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                            #was the above used? --MR
                            t_nor = t
                            if proceduralFlag:
                                tabWrite("normal{function" \
                                         "{f%s(x,y,z).grey} bump_size %.4g}\n" % \
                                         (texturesNorm,
    
                                         t_nor.normal_factor))
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                            else:
                                tabWrite("normal {uv_mapping bump_map " \
                                         "{%s \"%s\" %s  bump_size %.4g }%s}\n" % \
                                         (imageFormat(texturesNorm),
                                         texturesNorm, imgMap(t_nor),
    
                                         t_nor.normal_factor,
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
                                         mappingNor))
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            tabWrite("}\n") # THEN IT CAN CLOSE LAST LAYER OF TEXTURE
    
    
    def string_strip_hyphen(name):
        return name.replace("-", "")
    # WARNING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    def write_nodes(scene,povMatName,ntree,file):
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
        """translate Blender node trees to pov and write them to file"""
    
        declareNodes=[]
        scene=bpy.context.scene
        for node in ntree.nodes:
            povNodeName=string_strip_hyphen(bpy.path.clean_name(node.name))+"_%s"%povMatName
            if node.bl_idname == "PovrayFinishNode" and node.outputs["Finish"].is_linked:
                file.write('#declare %s = finish {\n'%povNodeName)
                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 == True:
                                    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))
    
    1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
                        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 == True:
                                    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)
    
                            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==True:
                                    fresnel="fresnel"
    
                            if link.from_node.inputs["Conserve energy"].is_linked:
                                pass
                            else:
                                if link.from_node.inputs["Conserve energy"].default_value==True:
                                    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:
            povNodeName=string_strip_hyphen(bpy.path.clean_name(node.name))+"_%s"%povMatName
            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'%(povNodeName,tx,ty,tz,rx,ry,rz,sx,sy,sz))
    
        for node in ntree.nodes:
            povNodeName=string_strip_hyphen(bpy.path.clean_name(node.name))+"_%s"%povMatName
            if node.bl_idname == "PovrayColorImageNode" and node.outputs["Pigment"].is_linked:
                declareNodes.append(node.name)
                if node.image == "":
                    file.write('#declare %s = pigment { color rgb 0.8}\n'%(povNodeName))
                else:
                    im=bpy.data.images[node.image]
                    if im.filepath and os.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:
                                povTransName=string_strip_hyphen(bpy.path.clean_name(link.from_node.name))+"_%s"%povMatName
                                transform="transform {%s}"%povTransName
                        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'%(povNodeName,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:
            povNodeName=string_strip_hyphen(bpy.path.clean_name(node.name))+"_%s"%povMatName
            if node.bl_idname == "PovrayImagePatternNode" and node.outputs["Pattern"].is_linked:
                declareNodes.append(node.name)
                if node.image != "":
                    im=bpy.data.images[node.image]
                    if im.filepath and os.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:
                                povTransName=string_strip_hyphen(bpy.path.clean_name(link.from_node.name))+"_%s"%povMatName
                                transform="transform {%s}"%povTransName
                        uv=""
                        if node.map_type=="uv_mapping":
                            uv="uv_mapping"
                        filepath=bpy.path.abspath(im.filepath)
                        file.write('#macro %s() %s image_pattern {\n'%(povNodeName,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:
            povNodeName=string_strip_hyphen(bpy.path.clean_name(node.name))+"_%s"%povMatName
            if node.bl_idname == "PovrayBumpMapNode" and node.outputs["Normal"].is_linked:
                if node.image != "":
                    im=bpy.data.images[node.image]
                    if im.filepath and os.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:
                                povTransName=string_strip_hyphen(bpy.path.clean_name(link.from_node.name))+"_%s"%povMatName
                                transform="transform {%s}"%povTransName
                        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'%(povNodeName,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')
                        declareNodes.append(node.name)
    
    
    
        for node in ntree.nodes:
            povNodeName=string_strip_hyphen(bpy.path.clean_name(node.name))+"_%s"%povMatName
            if node.bl_idname == "PovrayPigmentNode" and node.outputs["Pigment"].is_linked:
                declareNodes.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'%(povNodeName,r,g,b,f,t))
    
    
        for node in ntree.nodes:
            povNodeName=string_strip_hyphen(bpy.path.clean_name(node.name))+"_%s"%povMatName
            if node.bl_idname == "PovrayTextureNode" and node.outputs["Texture"].is_linked:
                declareNodes.append(node.name)
                r,g,b=node.inputs["Pigment"].default_value[:]
                povColName="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":
                            povColName=string_strip_hyphen(bpy.path.clean_name(link.from_node.name))+"_%s"%povMatName
                file.write('#declare %s = texture{\n    pigment{%s}\n'%(povNodeName,povColName))
                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 declareNodes:
                            povNorName=string_strip_hyphen(bpy.path.clean_name(link.from_node.name))+"_%s"%povMatName
                            file.write('    normal{%s}\n'%povNorName)
                if node.inputs["Finish"].is_linked:
                    for link in ntree.links:
                        if link.to_node==node and link.to_socket.name=="Finish":
                            povFinName=string_strip_hyphen(bpy.path.clean_name(link.from_node.name))+"_%s"%povMatName
                            file.write('    finish{%s}\n'%povFinName)
                file.write('}\n')
                declareNodes.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 declareNodes):
                            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 declareNodes:
                                        declare=False
                            if declare:
                                povNodeName=string_strip_hyphen(bpy.path.clean_name(node.name))+"_%s"%povMatName
                                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'%(povNodeName,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':
                                            povMacroName=string_strip_hyphen(bpy.path.clean_name(link.from_node.name))+"_%s"%povMatName
                                            pattern = "%s()"%povMacroName
                                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':
                                        povTransName=string_strip_hyphen(bpy.path.clean_name(link.from_node.name))+"_%s"%povMatName
                                        transform="transform {%s}"%povTransName
                                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
                                                povInMatName=string_strip_hyphen(bpy.path.clean_name(link.from_node.name))+"_%s_%s"%(n,povMatName)
                                                default=True
                                                for ilink in ntree.links:
                                                    if ilink.to_node==node and ilink.to_socket.name == str(n):
                                                        default=False
                                                        povInMatName=string_strip_hyphen(bpy.path.clean_name(ilink.from_node.name))+"_%s"%povMatName
                                                if default:
                                                    r,g,b,a=el.color[:]
                                                    file.write('    #declare %s = texture{pigment{color srgbt <%.4g,%.4g,%.4g,%.4g>}};\n'%(povInMatName,r,g,b,1-a))
                                                file.write('    [%s %s]\n'%(el.position,povInMatName))
                                else:
                                    els=[[0,0,0,0],[1,1,1,1]]
                                    for i in range(0,2):
                                        povInMatName=string_strip_hyphen(bpy.path.clean_name(link.from_node.name))+"_%s_%s"%(i,povMatName)
                                        default=True
                                        for ilink in ntree.links:
                                            if ilink.to_node==node and ilink.to_socket.name == str(i):
                                                default=False
                                                povInMatName=string_strip_hyphen(bpy.path.clean_name(ilink.from_node.name))+"_%s"%povMatName
                                        if default:
                                            r,g,b=els[i][1],els[i][2],els[i][3]
                                            if pattern not in {'checker', 'hexagon', 'square', 'triangular', 'brick'}:
                                                file.write('    #declare %s = texture{pigment{color rgb <%.4g,%.4g,%.4g>}};\n'%(povInMatName,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[i][0],povInMatName))
                                        else:
                                            if default==False:
                                                file.write('    texture{%s}\n'%povInMatName)
                                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')
                                declareNodes.append(node.name)
    
        for link in ntree.links:
            if link.to_node.bl_idname == "PovrayOutputNode" and link.from_node.name in declareNodes:
                povMatNodeName=string_strip_hyphen(bpy.path.clean_name(link.from_node.name))+"_%s"%povMatName
                file.write('#declare %s = %s\n'%(povMatName,povMatNodeName))