0
$\begingroup$

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 ?

$\endgroup$

2 Answers 2

3
$\begingroup$

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
$\endgroup$
5
  • $\begingroup$ 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 $\endgroup$
    – user824624
    Commented Oct 16, 2017 at 0:13
  • $\begingroup$ 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". $\endgroup$
    – batFINGER
    Commented Oct 16, 2017 at 5:01
  • $\begingroup$ 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']. $\endgroup$
    – user824624
    Commented Oct 16, 2017 at 5:49
  • $\begingroup$ 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 $\endgroup$
    – mcExchange
    Commented Aug 17, 2018 at 10:07
  • $\begingroup$ This helped me: blender.stackexchange.com/a/39533/61307 $\endgroup$
    – mcExchange
    Commented Aug 17, 2018 at 10:20
1
$\begingroup$

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
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .