Paste Code
Paste Blends
Paste Images
import bpy

class VRAY_OT_set_kelvin_color(bpy.types.Operator):
bl_idname = "vray.kelvin_color"
bl_label = "Kelvin color"
bl_description = "Set color temperature."

data_path= bpy.props.StringProperty(
name= "Data",
description= "Data path.",
maxlen= 1024,
default= ""
)

d_color= bpy.props.EnumProperty(
name= "Illuminant series D",
description= "Illuminant series D.",
items= (
('D75', "D75", "North sky Daylight."),
('D65', "D65", "Noon Daylight."),
('D55', "D55", "Mid-morning / Mid-afternoon Daylight."),
('D50', "D50", "Horizon Light."),
),
default= 'D50'
)

use_temperature= bpy.props.BoolProperty(
name= "Use temperature",
description= "Use temperature.",
default= False
)

temperature= bpy.props.IntProperty(
name= "Temperature",
description= "Kelvin temperature.",
min= 1000,
max= 40000,
step= 100,
default= 5000
)

dialog_width= 150

def draw(self, context):
layout= self.layout

row= layout.split().row(align= True)
row.prop(self, 'use_temperature', text= "")
if self.use_temperature:
row.prop(self, 'temperature', text= "K")
else:
row.prop(self, 'd_color', text= "Type")

layout.separator()

def invoke(self, context, event):
wm= context.window_manager
return wm.invoke_props_dialog(self, self.dialog_width)

def execute(self, context):
D_COLOR= {
'D75': 7500,
'D65': 6500,
'D55': 5500,
'D50': 5000,
}

def recoursive_attr(data, attrs):
if not attrs:
return data
attr= attrs.pop()
return recoursive_attr(getattr(data, attr), attrs)

if self.data_path:
attrs= self.data_path.split('.')
attr= attrs.pop() # Attribute to set
attrs.reverse()

data_pointer= recoursive_attr(context, attrs)

temperature= D_COLOR[self.d_color]

if self.use_temperature:
temperature= self.temperature

setattr(data_pointer, attr, tuple(kelvin_to_rgb(temperature)))

return {'FINISHED'}


class OBJECT_PT_hello(bpy.types.Panel):
bl_label = "Hello World Panel"
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
bl_context = "object"

def draw(self, context):
layout = self.layout

layout.operator("vray.kelvin_color")


def register():
bpy.utils.register_class(VRAY_OT_set_kelvin_color)
bpy.utils.register_class(OBJECT_PT_hello)


def unregister():
bpy.utils.unregister_class(VRAY_OT_set_kelvin_color)
bpy.utils.unregister_class(OBJECT_PT_hello)


if __name__ == "__main__":
register()
  1. import bpy
  2.  
  3. class VRAY_OT_set_kelvin_color(bpy.types.Operator):
  4.         bl_idname         = "vray.kelvin_color"
  5.         bl_label           = "Kelvin color"
  6.         bl_description = "Set color temperature."
  7.  
  8.         data_path= bpy.props.StringProperty(
  9.                 name= "Data",
  10.                 description= "Data path.",
  11.                 maxlen= 1024,
  12.                 default= ""
  13.         )
  14.  
  15.         d_color= bpy.props.EnumProperty(
  16.                 name= "Illuminant series D",
  17.                 description= "Illuminant series D.",
  18.                 items= (
  19.                         ('D75',  "D75",  "North sky Daylight."),
  20.                         ('D65',  "D65",  "Noon Daylight."),
  21.                         ('D55',  "D55",  "Mid-morning / Mid-afternoon Daylight."),
  22.                         ('D50',  "D50",  "Horizon Light."),
  23.                 ),
  24.                 default= 'D50'
  25.         )
  26.  
  27.         use_temperature= bpy.props.BoolProperty(
  28.                 name= "Use temperature",
  29.                 description= "Use temperature.",
  30.                 default= False
  31.         )
  32.  
  33.         temperature= bpy.props.IntProperty(
  34.                 name= "Temperature",
  35.                 description= "Kelvin temperature.",
  36.                 min= 1000,
  37.                 max= 40000,
  38.                 step= 100,
  39.                 default= 5000
  40.         )
  41.  
  42.         dialog_width= 150
  43.  
  44.         def draw(self, context):
  45.                 layout= self.layout
  46.  
  47.                 row= layout.split().row(align= True)
  48.                 row.prop(self, 'use_temperature', text= "")
  49.                 if self.use_temperature:
  50.                         row.prop(self, 'temperature', text= "K")
  51.                 else:
  52.                         row.prop(self, 'd_color', text= "Type")
  53.  
  54.                 layout.separator()
  55.                
  56.         def invoke(self, context, event):
  57.                 wm= context.window_manager
  58.                 return wm.invoke_props_dialog(self, self.dialog_width)
  59.  
  60.         def execute(self, context):
  61.                 D_COLOR= {
  62.                         'D75': 7500,
  63.                         'D65': 6500,
  64.                         'D55': 5500,
  65.                         'D50': 5000,
  66.                 }
  67.                
  68.                 def recoursive_attr(data, attrs):
  69.                         if not attrs:
  70.                                 return data
  71.                         attr= attrs.pop()
  72.                         return recoursive_attr(getattr(data, attr), attrs)
  73.  
  74.                 if self.data_path:
  75.                         attrs= self.data_path.split('.')
  76.                         attr= attrs.pop() # Attribute to set
  77.                         attrs.reverse()
  78.  
  79.                         data_pointer= recoursive_attr(context, attrs)
  80.  
  81.                         temperature= D_COLOR[self.d_color]
  82.  
  83.                         if self.use_temperature:
  84.                                 temperature= self.temperature
  85.  
  86.                         setattr(data_pointer, attr, tuple(kelvin_to_rgb(temperature)))
  87.  
  88.                 return {'FINISHED'}
  89.  
  90.  
  91. class OBJECT_PT_hello(bpy.types.Panel):
  92.         bl_label = "Hello World Panel"
  93.         bl_space_type = "PROPERTIES"
  94.         bl_region_type = "WINDOW"
  95.         bl_context = "object"
  96.  
  97.         def draw(self, context):
  98.                 layout = self.layout
  99.  
  100.                 layout.operator("vray.kelvin_color")
  101.  
  102.  
  103. def register():
  104.         bpy.utils.register_class(VRAY_OT_set_kelvin_color)
  105.         bpy.utils.register_class(OBJECT_PT_hello)
  106.  
  107.  
  108. def unregister():
  109.         bpy.utils.unregister_class(VRAY_OT_set_kelvin_color)
  110.         bpy.utils.unregister_class(OBJECT_PT_hello)
  111.  
  112.  
  113. if __name__ == "__main__":
  114.         register()
go to heaven