Advertisement
Guest User

Untitled

a guest
Jul 15th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.06 KB | None | 0 0
  1. '''
  2. Copyright (C) 2018 Legigan Jeremy AKA Pistiwique
  3.  
  4.  
  5. Created by Legigan jeremy AKA Pistiwique
  6.  
  7.    This program is free software: you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation, either version 3 of the License, or
  10.    (at your option) any later version.
  11.  
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19. '''
  20.  
  21. bl_info = {
  22.     "name": "RAL to RGB",
  23.     "description": "",
  24.     "author": "Legigan Jeremy AKA Pistiwique",
  25.     "version": (0, 0, 1),
  26.     "blender": (2, 79, 0),
  27.     "location": "Node Editor",
  28.     "category": "Node"
  29.     }
  30.    
  31. import bpy
  32. from bpy.types import NodeTree, Node, NodeSocket
  33.  
  34. RAL_values = {
  35.     1000: (205, 186, 136),
  36.     1001: (208, 176, 132),
  37.     1002: (210, 170, 109),
  38.     1003: (249, 168, 0),
  39.     1004: (228, 158, 0),
  40.     1005: (203, 142, 30),
  41.     1006: (226, 144, 0),
  42.     1007: (232, 140, 0),
  43.     1011: (175, 128, 79),
  44.     1012: (221, 175, 39),
  45.     1013: (227, 217, 198),
  46.     1014: (221, 196, 154),
  47.     1015: (230, 210, 181),
  48.     1016: (241, 221, 56),
  49.     1017: (246, 169, 80),
  50.     1018: (250, 202, 48),
  51.     1019: (164, 143, 122),
  52.     1020: (160, 143, 101),
  53.     1021: (246, 182, 0),
  54.     1023: (247, 181, 0),
  55.     1024: (186, 143, 76),
  56.     1026: (255, 255, 0),
  57.     1027: (167, 127, 14),
  58.     1028: (255, 155, 0),
  59.     1032: (226, 163, 0),
  60.     1033: (249, 154, 28),
  61.     1034: (235, 156, 82),
  62.     1035: (144, 131, 112),
  63.     1036: (128, 100, 63),
  64.     1037: (240, 146, 0),
  65.     2000: (218, 110, 0),
  66.     2001: (186, 72, 27),
  67.     2002: (191, 57, 34),
  68.     2003: (246, 120, 40),
  69.     2004: (226, 83, 3),
  70.     2005: (255, 77, 6),
  71.     2007: (255, 178, 0),
  72.     2008: (237, 107, 33),
  73.     2009: (222, 83, 7),
  74.     2010: (208, 93, 40),
  75.     2011: (226, 110, 14),
  76.     2012: (213, 101, 77),
  77.     2013: (146, 62, 37),
  78.     3000: (167, 41, 32),
  79.     3001: (155, 36, 35),
  80.     3002: (155, 35, 33),
  81.     3003: (134, 26, 34),
  82.     3004: (107, 28, 35),
  83.     3005: (89, 25, 31),
  84.     3007: (62, 32, 34),
  85.     3009: (109, 52, 45),
  86.     3011: (121, 36, 35),
  87.     3012: (198, 132, 109),
  88.     3013: (151, 46, 37),
  89.     3014: (203, 115, 117),
  90.     3015: (216, 160, 166),
  91.     3016: (166, 61, 47),
  92.     3017: (203, 85, 93),
  93.     3018: (199, 63, 74),
  94.     3020: (187, 30, 16),
  95.     3022: (207, 105, 85),
  96.     3024: (255, 45, 33),
  97.     3026: (255, 42, 27),
  98.     3027: (171, 39, 60),
  99.     3028: (204, 44, 36),
  100.     3031: (166, 52, 55),
  101.     3032: (112, 29, 35),
  102.     3033: (165, 58, 45),
  103.     4001: (129, 97, 131),
  104.     4002: (141, 60, 75),
  105.     4003: (196, 97, 140),
  106.     4004: (101, 30, 56),
  107.     4005: (118, 104, 154),
  108.     4006: (144, 50, 115),
  109.     4007: (71, 36, 60),
  110.     4008: (132, 76, 130),
  111.     4009: (157, 134, 146),
  112.     4010: (188, 64, 119),
  113.     4011: (110, 99, 135),
  114.     4012: (107, 107, 127),
  115.     5000: (49, 79, 111),
  116.     5001: (15, 76, 100),
  117.     5002: (0, 56, 123),
  118.     5003: (31, 56, 85),
  119.     5004: (25, 30, 40),
  120.     5005: (0, 83, 135),
  121.     5007: (55, 107, 140),
  122.     5008: (43, 58, 68),
  123.     5009: (34, 95, 120),
  124.     5010: (0, 79, 124),
  125.     5011: (26, 43, 60),
  126.     5012: (0, 137, 182),
  127.     5013: (25, 49, 83),
  128.     5014: (99, 125, 150),
  129.     5015: (0, 124, 176),
  130.     5017: (0, 91, 140),
  131.     5018: (5, 139, 140),
  132.     5019: (0, 94, 131),
  133.     5020: (0, 65, 75),
  134.     5021: (0, 117, 119),
  135.     5022: (34, 45, 90),
  136.     5023: (66, 105, 140),
  137.     5024: (96, 147, 172),
  138.     5025: (33, 105, 124),
  139.     5026: (15, 48, 82),
  140.     6000: (60, 116, 96),
  141.     6001: (54, 103, 53),
  142.     6002: (50, 89, 40),
  143.     6003: (80, 83, 60),
  144.     6004: (2, 68, 66),
  145.     6005: (17, 66, 50),
  146.     6006: (60, 57, 46),
  147.     6007: (44, 50, 34),
  148.     6008: (55, 52, 42),
  149.     6009: (39, 53, 42),
  150.     6010: (77, 111, 57),
  151.     6011: (108, 124, 89),
  152.     6012: (48, 61, 58),
  153.     6013: (125, 118, 90),
  154.     6014: (71, 65, 53),
  155.     6015: (61, 61, 54),
  156.     6016: (0, 105, 76),
  157.     6017: (88, 127, 64),
  158.     6018: (97, 153, 59),
  159.     6019: (185, 206, 172),
  160.     6020: (55, 66, 47),
  161.     6021: (138, 153, 119),
  162.     6022: (58, 51, 39),
  163.     6024: (0, 131, 81),
  164.     6025: (94, 110, 59),
  165.     6026: (0, 95, 78),
  166.     6027: (126, 186, 181),
  167.     6028: (49, 84, 66),
  168.     6029: (0, 111, 61),
  169.     6032: (35, 127, 82),
  170.     6033: (70, 135, 127),
  171.     6034: (122, 172, 172),
  172.     6035: (25, 77, 37),
  173.     6036: (4, 87, 75),
  174.     6037: (0, 139, 41),
  175.     6038: (0, 181, 26),
  176.     7000: (122, 136, 142),
  177.     7001: (140, 150, 157),
  178.     7002: (129, 120, 99),
  179.     7003: (122, 118, 105),
  180.     7004: (155, 155, 155),
  181.     7005: (108, 110, 107),
  182.     7006: (118, 106, 94),
  183.     7008: (116, 94, 61),
  184.     7009: (93, 96, 88),
  185.     7010: (88, 92, 86),
  186.     7011: (82, 89, 93),
  187.     7012: (87, 93, 94),
  188.     7013: (87, 80, 68),
  189.     7015: (79, 83, 88),
  190.     7016: (56, 62, 66),
  191.     7021: (47, 50, 52),
  192.     7022: (76, 74, 68),
  193.     7023: (128, 128, 118),
  194.     7024: (69, 73, 78),
  195.     7026: (55, 67, 69),
  196.     7030: (146, 142, 133),
  197.     7031: (91, 104, 109),
  198.     7032: (181, 176, 161),
  199.     7033: (127, 130, 116),
  200.     7034: (146, 136, 111),
  201.     7035: (197, 199, 196),
  202.     7036: (151, 147, 146),
  203.     7037: (122, 123, 122),
  204.     7038: (176, 176, 169),
  205.     7039: (107, 102, 94),
  206.     7040: (152, 158, 161),
  207.     7042: (142, 146, 145),
  208.     7043: (79, 82, 80),
  209.     7044: (183, 179, 168),
  210.     7045: (141, 146, 149),
  211.     7046: (127, 134, 138),
  212.     7047: (200, 200, 199),
  213.     7048: (129, 123, 115),
  214.     8000: (137, 105, 62),
  215.     8001: (157, 98, 43),
  216.     8002: (121, 77, 62),
  217.     8003: (126, 75, 38),
  218.     8004: (141, 73, 49),
  219.     8007: (112, 69, 42),
  220.     8008: (114, 74, 37),
  221.     8011: (90, 56, 38),
  222.     8012: (102, 51, 43),
  223.     8014: (74, 53, 38),
  224.     8015: (94, 47, 38),
  225.     8016: (76, 43, 32),
  226.     8017: (68, 47, 41),
  227.     8019: (61, 54, 53),
  228.     8022: (26, 23, 24),
  229.     8023: (164, 87, 41),
  230.     8024: (121, 80, 56),
  231.     8025: (117, 88, 71),
  232.     8028: (81, 58, 42),
  233.     8029: (127, 64, 49),
  234.     9001: (233, 224, 210),
  235.     9002: (215, 213, 203),
  236.     9003: (236, 236, 231),
  237.     9004: (43, 43, 44),
  238.     9005: (14, 14, 16),
  239.     9006: (161, 161, 160),
  240.     9007: (135, 133, 129),
  241.     9010: (241, 236, 225),
  242.     9011: (39, 41, 43),
  243.     9016: (241, 240, 234),
  244.     9017: (42, 41, 42),
  245.     9018: (200, 203, 196),
  246.     9022: (133, 133, 131),
  247.     9023: (121, 123, 122),
  248.     }
  249.  
  250.  
  251. class MyCustomTreeNode :
  252.     @classmethod
  253.     def poll(cls, ntree):
  254.         return ntree.bl_idname == 'ShaderNodeTree'
  255.  
  256.  
  257. class MyCustomNode(Node, MyCustomTreeNode):
  258.     '''A custom node'''
  259.     bl_idname = 'CustomNodeType'
  260.     bl_label = 'RAL to RGB'
  261.     bl_icon = 'INFO'
  262.  
  263.     def update_value(self, context):
  264.         self.outputs["RGB"].default_value = (int(self.RAL), int(self.RAL), int(self.RAL), 1)
  265.         self.update ()
  266.    
  267.     RAL = bpy.props.StringProperty(default="1000", update = update_value)
  268.    
  269.     def srgb_to_linear(self, r, g, b):
  270.         def srgb(c):
  271.             a = .055
  272.             if c <= .04045:
  273.                 return c / 12.92
  274.             else:
  275.                 return ((c+a) / (1+a)) ** 2.4
  276.         return tuple(srgb(c/255) for c in (r, g, b))
  277.    
  278.     def init(self, context):
  279.         self.outputs.new('NodeSocketColor', "RGB")
  280.         col = self.srgb_to_linear(*RAL_values[int(self.RAL)])
  281.         COLOR = (col[0], col[1], col[2], 1)
  282.        
  283.         self.outputs["RGB"].default_value = COLOR
  284.        
  285.    
  286.     def update(self):
  287.         try:
  288.             out = self.outputs["RGB"]
  289.             can_continue = True
  290.         except:
  291.             can_continue = False
  292.        
  293.         if can_continue and RAL_values.get(int(self.RAL)):
  294.             if out.is_linked:
  295.                 col = self.srgb_to_linear(*RAL_values[int(self.RAL)])
  296.                 COLOR = (col[0], col[1], col[2], 1)
  297.                 for o in out.links:
  298.                     if o.is_valid:
  299.                         o.to_socket.node.inputs[o.to_socket.name].default_value = COLOR
  300.  
  301.     def draw_buttons(self, context, layout):
  302.         layout.prop(self, "RAL",text = 'RAL')
  303.         if not RAL_values.get(int(self.RAL)):
  304.             layout.label("RAL {} not in database".format(self.RAL))
  305.  
  306.     def draw_label(self):
  307.         return "RAL to RGB"
  308.  
  309. import nodeitems_utils
  310. from nodeitems_utils import NodeCategory, NodeItem
  311.  
  312. class MyNodeCategory(NodeCategory):
  313.     @classmethod
  314.     def poll(cls, context):
  315.         return context.space_data.tree_type == 'ShaderNodeTree'
  316.  
  317. node_categories = [
  318.     MyNodeCategory("SOMENODES", "RAL to RGB", items=[
  319.         NodeItem("CustomNodeType"),
  320.         ]),
  321.     ]
  322.  
  323. def register():
  324.     bpy.utils.register_class(MyCustomNode)
  325.     nodeitems_utils.register_node_categories("CUSTOM_NODES", node_categories)
  326.  
  327. def unregister():
  328.     nodeitems_utils.unregister_node_categories("CUSTOM_NODES")
  329.     bpy.utils.unregister_class(MyCustomNode)
  330.    
  331.  
  332. if __name__ == "__main__":
  333.     register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement