0

I need to programatically modify the value of sun lightenter image description here

From the picture, it seems we can code it from

bpy.data.node_groups["Shader Nodetree"].nodes["Emission"].inputs[1].default_value = 1.2

however, the code produce an error that

>>> bpy.data.node_groups["Shader Nodetree"].nodes["Emission"].inputs[1].default_value = 2
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
KeyError: 'bpy_prop_collection[key]: key "Shader Nodetree" not found'

I found a similar post it is related to changing the material, but mine question is related to the sunlight, how would i do it ?

user824624
  • 269
  • 1
  • 7
  • 20

2 Answers2

3

Yes the tooltip can be a little misleading, look for the node_tree property on the lamp data object.

With the lamp as context object

>> C.object
bpy.data.objects['Lamp']

>>> C.object.data.node_tree.nodes['Emission'].inputs['Strength'].default_value = 2

# or via bpy.data (D)

>>> D.objects['Lamp'] == C.object
True

>>> D.objects['Lamp'].data
bpy.data.lamps['Lamp']

>>> lamp = D.lamps['Lamp']

>>> lamp.node_tree.nodes['Emission'].inputs['Strength'].default_value = 3
batFINGER
  • 84,216
  • 10
  • 108
  • 233
  • I tried bpy.data.lamps['Lamp'].node_tree.nodes['Emission'].inputs[1].default_value = 100, but got error : KeyError : 'bpy_prop_collection[key]: key 'Lamp' not found – user824624 Oct 16 '17 at 00:13
  • Select your lamp object and type C.object.data into python console. If it prints bpy.data.lamps["ItsCalledSomethingElse"] it won't respond the the name "Lamp". – batFINGER Oct 16 '17 at 05:01
  • oh, I understand. i tried it in python console, by typing C.object, it responds with bpy.data.objects['Sun']; by typing D.objects['Sun'].data, it responds with bpy.data.lamps['Sun.001']. – user824624 Oct 16 '17 at 05:49
  • This is not working for me, C and D don't exist if you call blender from terminal with a python script ./blender -b -P ./myScript.py – mcExchange Aug 17 '18 at 10:07
  • This helped me: https://blender.stackexchange.com/a/39533/61307 – mcExchange Aug 17 '18 at 10:20
1

1. Select lamp

lamp_objects = [o for o in bpy.data.objects
                if o.type == 'LAMP']
lamp = lamp_objects[0] # assuming you only have one lamp

2. Set use_nodes to True

lamp.data.use_nodes = True

3. Set value

lamp.data.node_tree.nodes['Emission'].inputs['Strength'].default_value = 7