Advertisement
Guest User

srbglincon

a guest
Apr 12th, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.91 KB | None | 0 0
  1. # ##### BEGIN GPL LICENSE BLOCK #####
  2. #
  3. #  This program is free software; you can redistribute it and/or
  4. #  modify it under the terms of the GNU General Public License
  5. #  as published by the Free Software Foundation; either version 2
  6. #  of the License, or (at your option) any later version.
  7. #
  8. #  This program is distributed in the hope that it will be useful,
  9. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. #  GNU General Public License for more details.
  12. #
  13. #  You should have received a copy of the GNU General Public License
  14. #  along with this program; if not, write to the Free Software Foundation,
  15. #  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  16. #
  17. # ##### END GPL LICENSE BLOCK #####
  18.  
  19. """
  20. by  Rickyblender, updated for 2.8 by Brockmann
  21. on blendernaton forum
  22. feedback from Padone
  23.  
  24. Thread  in pythpn forum
  25. http://blenderartists.org/forum/showthread.php?360140-help-please-is-there-a-fast-way-to-convert-srgb-values-to-linear&p=2802920#post2802920
  26.  
  27. and stackexchange
  28. # https://blender.stackexchange.com/q/136908
  29.  
  30. """
  31.  
  32. import bpy,bmesh
  33. from bpy.props import *
  34. from mathutils import *
  35. from math import *
  36.  
  37. from time import time
  38.  
  39. last_menu1 = 'None'
  40.  
  41. class PanelSimple(bpy.types.Panel):
  42.     bl_label = "Remove Doubles / Merge"
  43.     bl_space_type = "VIEW_3D"          
  44.     bl_region_type = "TOOLS"
  45.     #bl_region_type = "TOOL_PROPS"  
  46.     bl_show_header=True
  47.  
  48.     def draw(self, context):
  49.         global last_menu1  # Access global Var to find the last operation
  50.  
  51.         layout = self.layout
  52.         scene = context.scene
  53.  
  54.         col = layout.column()
  55.         #col.label(text=' menu color shade:', 'LAMP_DATA')
  56.        
  57.         # First  Operator
  58.         #col.operator_menu_enum('view_3d.my_color1', 'colormenu1', 'Menu Selection')
  59.  
  60.         # Here the operator menu is displayed
  61.         # 'view_3d.my_color1' is the bl_idname of the operator called
  62.  
  63.         col.label(text=' Selection : ' + last_menu1)
  64.         #  print ('in draw   ies panel  colormenu1 =',last_menu1,' Val =',eval(last_menu1))
  65.  
  66.         colorselect=""
  67.  
  68.         if eval(last_menu1)==1:
  69.  
  70.             colorselect="sRGB"
  71.             #col.label(text='First point', 'FCURVE')
  72.  
  73.             print ('selected sRGB')
  74.  
  75.         elif eval(last_menu1)==2:
  76.  
  77.             colorselect="Linear"
  78.             #col.label(text='Middle Point', 'PARTICLE_DATA')
  79.  
  80.         #print (' Color selection =',colorselect)
  81.         col.separator()
  82.         Txcolor=colorselect+ ' Color selected'
  83.         col.label(text=Txcolor)
  84.         subtype ='PERCENTAGE'
  85.         col.separator()
  86.  
  87.         layout.operator("custom.button1")
  88.         layout.operator("custom.button2")
  89.  
  90.  
  91. class FIRSTOperator(bpy.types.Operator): # When an option in the operator menu is clicked, this is called
  92.     '''Operator'''
  93.     bl_idname = 'view_3d.my_color1'
  94.     bl_label = 'Operator'
  95.  
  96.     # Define possible operations
  97.  
  98.     colormenu1 = EnumProperty(items=(
  99.         ('1', 'sRGB-Linear', 'The first item'),
  100.         ('2', 'Linear-sRGB', 'The second item')
  101.     ))
  102.  
  103.     @classmethod
  104.     def poll(cls, context):
  105.         return context.object is not None
  106.  
  107.  
  108.     def execute(self, context):
  109.         global last_menu1 # Access global Var so we can later draw it in the panel
  110.         last_menu1 = self.properties.colormenu1 # Store the choosen operation / Selection
  111.         print ('selection   shade=',self.properties.colormenu1[0])
  112.  
  113.         if last_menu1=="1":
  114.             print ('operator  BL  Diffuse  SRGB  to Linear  1')
  115.  
  116.         elif last_menu1=="2":
  117.             print (' operator  BL  Diffuse  Linear to   SRGB 2')
  118.  
  119.         return {'FINISHED'}
  120.  
  121.  
  122. # http://entropymine.com/imageworsener/srgbformula/
  123. def s2lin1(x):
  124.  
  125.     a = 0.055
  126.     if x <= 0.04045 :
  127.         y = x * (1.0 / 12.92)  
  128.     elif  0.04045 < x <= 1 :
  129.         y =  ((x+0.055)/1.055)**2.4
  130.  
  131.     return y
  132.  
  133. #x = 0.1
  134.  
  135. #z = s2lin1(x)
  136. #print ('Linear RGB  to   S RGB')
  137. #print ()
  138.  
  139.  
  140. def lin2s1(x):
  141.  
  142.     a = 0.055
  143.     if x <=0.0031308:
  144.         y = x * 12.92
  145.     elif 0.0031308 < x <= 1 :
  146.         y = 1.055*x**(1/2.4) - 0.055
  147.  
  148.     return y
  149.  
  150.  
  151. #Custom Button
  152. class CustomButton1(bpy.types.Operator):
  153.     bl_idname = "custom.button1"
  154.     bl_label = "convert sRGB colors"
  155.     __doc__ = "Simple convert sRGB colors"
  156.  
  157.     def invoke(self, context, event):
  158.  
  159.         # when the button is press it prints this to the log / Console
  160.         print("  ####################  ")
  161.         print("convert sRGB colors1")
  162.         print (' $$$$$$$$$$$$$ convert sRGB colors    **************************')
  163.         print("  ####################  ")
  164.  
  165.         global  last_menu1
  166.         context = bpy.context
  167.         mesh_objs = [ob for ob in context.selected_objects if ob.type == 'MESH']
  168.  
  169.         begin = time()
  170.         #print('Start Time in seconds =', begin)
  171.         print (' operator  BL  Diffuse  SRGB  to Linear  1')
  172.  
  173.         for mesh in mesh_objs:
  174.  
  175.             print('repr =',repr(mesh))
  176.             print("active_slot:", mesh.active_material_index)
  177.  
  178.             if mesh.active_material is not None:
  179.                 print("active_material:", mesh.active_material.name)
  180.  
  181.             for i, mat in enumerate(mesh.material_slots):
  182.                 # print (' dir mat =', dir(mat ))
  183.                 if not i:
  184.                     print("material_slots")
  185.                 if mat is not None:
  186.                     print("\t[%d] = %s" % (i, mat.name))
  187.                     # print ('material = ', mat.material)
  188.  
  189.                     # print (' dir mat =', dir(mat ))
  190.                     # print("\t[%d] = %s Diff color = %f" % (i, mat.name,mat.))
  191.  
  192.                     for item in bpy.data.materials:
  193.                         if item.name == mat.name:
  194.                             nred1 = s2lin1(item.diffuse_color[0])
  195.                             ngreen1 = s2lin1(item.diffuse_color[1])
  196.                             nblue1 = s2lin1(item.diffuse_color[2])
  197.                             newcol = (( nred1 ,ngreen1 , nblue1, 1.0 ))
  198.                             print ('old Red =',item.diffuse_color[0],' New Red =', nred1 ,'name =',item.name)
  199.                             item.diffuse_color = newcol
  200.                 else:
  201.                     print("\t[%d] is None" % i)
  202.  
  203.         # P 41  53 65
  204.         end = time()
  205.         print('Execution took : ', time()-begin, 'seconds')
  206.         return{'FINISHED'}    
  207.  
  208.  
  209. #Custom Button2
  210. class CustomButton2(bpy.types.Operator):
  211.     bl_idname = "custom.button2"
  212.     bl_label = "convert Lin colors"
  213.     __doc__ = "Simple convert Lin colors"
  214.  
  215.     def invoke(self, context, event):
  216.  
  217.                                                     # when the button is press it prints this to the log / Console
  218.         print("  ####################  ")
  219.         print("convert lin colors 2")
  220.         print (' $$$$$$$$$$$$$ convert sRGB colors **************************')
  221.         print("  ####################  ")
  222.  
  223.         global  last_menu1
  224.  
  225.         context = bpy.context
  226.  
  227.         mesh_objs = [ob for ob in context.selected_objects if ob.type == 'MESH']
  228.  
  229.  
  230.  
  231.         # P 41  53 65
  232.         begin = time()
  233.         #print('Start Time in seconds =', begin)
  234.  
  235.         for mesh in mesh_objs:
  236.  
  237.             print('repr =',repr(mesh))
  238.  
  239.             print("active_slot:", mesh.active_material_index)
  240.             print ()
  241.  
  242.             if mesh.active_material is not None:
  243.                 print("active_material:", mesh.active_material.name)
  244.  
  245.             for i, mat in enumerate(mesh.material_slots):
  246.                 # print (' dir mat =', dir(mat ))
  247.                 if not i:
  248.                     print("material_slots")
  249.                 if mat is not None:
  250.                     print("\t[%d] = %s" % (i, mat.name))
  251.  
  252.                     for item in bpy.data.materials:
  253.  
  254.                         if item.name == mat.name:
  255.  
  256.                             print (item.name,' RGB =',item.diffuse_color)
  257.                             nred1 = lin2s1(item.diffuse_color[0])
  258.                             ngreen1 = lin2s1(item.diffuse_color[1])
  259.                             nblue1 = lin2s1(item.diffuse_color[2])
  260.                             newcol = (( nred1 ,ngreen1 , nblue1, 1.0 ))
  261.                             #print ('old Red =',item.diffuse_color[0],' New Red =', nred1 )
  262.                             print ('New SRGB =',newcol )
  263.  
  264.                             item.diffuse_color = newcol
  265.  
  266.  
  267.         end = time()
  268.         print('Execution took : ', time()-begin, 'seconds')
  269.         return{'FINISHED'}    
  270.  
  271.  
  272. def register():
  273.     bpy.utils.register_class(PanelSimple)
  274.     bpy.utils.register_class(FIRSTOperator)
  275.     bpy.utils.register_class(CustomButton1)
  276.     bpy.utils.register_class(CustomButton2)
  277.  
  278.  
  279. def unregister():
  280.     bpy.utils.unregister_class(PanelSimple)
  281.     bpy.utils.unregister_class(FIRSTOperator)
  282.     bpy.utils.unregister_class(CustomButton1)
  283.     bpy.utils.unregister_class(CustomButton2)
  284.  
  285.  
  286. if __name__ == "__main__":
  287.     register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement