-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
303 lines (254 loc) · 10.7 KB
/
__init__.py
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
# <pep8 compliant>
# Light Temperature - Professional light temperature control for Blender
# Copyright (C) 2024 softyoda
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# CHANGELOG
# v 2.8 February 2024 - Light Temperature
# ☒ Added temperature sync via Ctrl+L menu
# ☒ Improved state management and error handling
# ☒ Added power toggle with energy preservation
# ☒ Initial release with temperature control and presets
bl_info = {
"name": "Light Temperature Color",
"author": "softyoda",
"version": (1, 0),
"blender": (4, 3, 2),
"location": "Properties > Light > Light Temperature, Object > Link/Transfer Data",
"description": "Professional light temperature control with physical accuracy",
"warning": "Requires Cycles render engine",
"doc_url": "", # Add your documentation URL if available
"category": "Lighting",
}
import bpy
from bpy.props import FloatProperty, BoolProperty
from bpy.types import Panel, Operator, PropertyGroup
from bpy.app.handlers import persistent
# ---------------------------
# Operators
# ---------------------------
class LIGHT_OT_preset_temperature(Operator):
"""Apply predefined temperature preset to the selected light"""
bl_idname = "light.apply_preset"
bl_label = "Apply Temperature Preset"
bl_options = {'REGISTER', 'UNDO'}
temperature: FloatProperty(
name="Temperature",
description="Light temperature in Kelvin",
min=800,
max=12000,
default=6500
)
def execute(self, context):
light = context.active_object.data
if not light.temperature_setup:
setup_temperature_nodes(light)
light.temperature = self.temperature
return {'FINISHED'}
class LIGHT_OT_setup_temperature(Operator):
"""Initialize the temperature control system for this light"""
bl_idname = "light.setup_temperature"
bl_label = "Enable Temperature Control"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
light = context.active_object.data
setup_temperature_nodes(light)
return {'FINISHED'}
class LIGHT_OT_copy_temperature(Operator):
"""Copy temperature settings from active light to selected lights"""
bl_idname = "light.copy_temperature"
bl_label = "Copy Temperature Settings"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
active = context.active_object
if not active or active.type != 'LIGHT':
self.report({'ERROR'}, "Active object must be a light")
return {'CANCELLED'}
active_light = active.data
if not active_light.temperature_setup:
self.report({'ERROR'}, "Active light must have temperature control enabled")
return {'CANCELLED'}
# Copy settings to all selected lights
for obj in context.selected_objects:
if obj != active and obj.type == 'LIGHT':
light = obj.data
if not light.temperature_setup:
setup_temperature_nodes(light)
light.temperature = active_light.temperature
light.light_enabled = active_light.light_enabled
if light.light_enabled:
light.energy = active_light.energy
return {'FINISHED'}
# ---------------------------
# Core Functionality
# ---------------------------
def setup_temperature_nodes(light):
"""Create and configure the node setup for temperature control"""
try:
light.use_nodes = True
tree = light.node_tree
tree.nodes.clear()
# Create basic node setup
output = tree.nodes.new('ShaderNodeOutputLight')
emission = tree.nodes.new('ShaderNodeEmission')
blackbody = tree.nodes.new('ShaderNodeBlackbody')
# Position nodes for clean layout
output.location = (400, 0)
emission.location = (200, 0)
blackbody.location = (0, 0)
# Connect nodes
tree.links.new(blackbody.outputs['Color'], emission.inputs['Color'])
tree.links.new(emission.outputs['Emission'], output.inputs['Surface'])
# Initialize values
blackbody.inputs['Temperature'].default_value = light.temperature
light.color = (1, 1, 1)
light.temperature_setup = True
light.prev_energy = light.energy
light.light_enabled = light.energy > 0
except Exception as e:
print(f"Node setup failed: {str(e)}")
light.temperature_setup = False
def update_light_temperature(self, context):
"""Update the blackbody node when temperature value changes"""
if self.temperature_setup:
if self.color[:3] != (1, 1, 1):
self.temperature_setup = False
return
if tree := self.node_tree:
if blackbody := next((n for n in tree.nodes if n.type == 'BLACKBODY'), None):
blackbody.inputs['Temperature'].default_value = self.temperature
def update_light_state(self, context):
"""Handle power state changes and energy management"""
if self.light_enabled and self.energy == 0:
self.energy = self.prev_energy
elif not self.light_enabled and self.energy > 0:
self.prev_energy = self.energy
self.energy = 0.0
@persistent
def check_light_changes(scene):
"""Monitor light changes to maintain proper state synchronization"""
for obj in scene.objects:
if obj.type == 'LIGHT':
light = obj.data
if light.temperature_setup:
# Detect manual color changes
if light.color[:3] != (1, 1, 1):
light.temperature_setup = False
# Sync power state with energy
if light.energy > 0 and not light.light_enabled:
light.light_enabled = True
elif light.energy == 0 and light.light_enabled:
light.light_enabled = False
# ---------------------------
# UI
# ---------------------------
class LIGHT_PT_temperature(Panel):
"""Light Temperature Control Panel"""
bl_label = "Light Temperature"
bl_idname = "LIGHT_PT_temperature"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "data"
bl_order = 1
@classmethod
def poll(cls, context):
return (context.scene.render.engine == 'CYCLES'
and context.active_object
and context.active_object.type == 'LIGHT')
def draw(self, context):
layout = self.layout
light = context.active_object.data
if not light.temperature_setup:
layout.operator("light.setup_temperature",
icon='LIGHT',
text="Enable Temperature Control")
return
main_col = layout.column(align=True)
# Temperature control
main_col.prop(light, "temperature", slider=True)
# Common temperature presets
presets = main_col.grid_flow(row_major=True, columns=4, align=True)
for label, temp in [("Candle", 1500), ("Tungsten", 3200),
("Daylight", 6500), ("Overcast", 7500)]:
props = presets.operator("light.apply_preset", text=label)
props.temperature = temp
# Power control
power_row = main_col.row(align=True)
power_row.prop(light, "light_enabled",
text="Power: On" if light.light_enabled else "Power: Off",
toggle=True,
icon='CHECKBOX_HLT' if light.light_enabled else 'CHECKBOX_DEHLT')
def light_temperature_menu(self, context):
"""Add temperature copying to the Link menu"""
self.layout.operator(LIGHT_OT_copy_temperature.bl_idname,
text="Light Temperature Settings")
# ---------------------------
# Registration
# ---------------------------
def register():
# Register classes
bpy.utils.register_class(LIGHT_OT_preset_temperature)
bpy.utils.register_class(LIGHT_OT_setup_temperature)
bpy.utils.register_class(LIGHT_OT_copy_temperature)
bpy.utils.register_class(LIGHT_PT_temperature)
# Register properties
bpy.types.Light.temperature = FloatProperty(
name="Temperature",
description="Color temperature in Kelvin",
min=800,
max=12000,
default=6500,
update=update_light_temperature,
options={'ANIMATABLE'}
)
bpy.types.Light.temperature_setup = BoolProperty(
name="Temperature Setup",
description="Whether temperature control is enabled for this light",
default=False
)
bpy.types.Light.light_enabled = BoolProperty(
name="Light Enabled",
description="Toggle light power while preserving energy value",
default=True,
update=update_light_state
)
bpy.types.Light.prev_energy = FloatProperty(
name="Previous Energy",
description="Stores previous energy value when light is disabled",
default=10.0
)
# Register update handler
if check_light_changes not in bpy.app.handlers.depsgraph_update_post:
bpy.app.handlers.depsgraph_update_post.append(check_light_changes)
# Add menu item
bpy.types.VIEW3D_MT_make_links.append(light_temperature_menu)
def unregister():
# Unregister classes
bpy.utils.unregister_class(LIGHT_OT_preset_temperature)
bpy.utils.unregister_class(LIGHT_OT_setup_temperature)
bpy.utils.unregister_class(LIGHT_OT_copy_temperature)
bpy.utils.unregister_class(LIGHT_PT_temperature)
# Remove menu item
bpy.types.VIEW3D_MT_make_links.remove(light_temperature_menu)
# Remove update handler
if check_light_changes in bpy.app.handlers.depsgraph_update_post:
bpy.app.handlers.depsgraph_update_post.remove(check_light_changes)
# Remove properties
del bpy.types.Light.temperature
del bpy.types.Light.temperature_setup
del bpy.types.Light.light_enabled
del bpy.types.Light.prev_energy
if __name__ == "__main__":
register()