2
$\begingroup$

I'm trying to use Python to create a sun lamp (in Cycles mode) and then change it's strength. I began by performing the operations manually and then copying the Python code that was produced in the top output area. When I then run the script, the sun lamp is created, but I get an error before the strength can be set. Here's the code:

import bpy

bpy.context.scene.render.engine = 'CYCLES'
bpy.ops.object.lamp_add(type='SUN', view_align=False, location=(0, 0, 0))
bpy.context.space_data.context = 'DATA'
bpy.data.node_groups["Shader Nodetree"].nodes["Emission"].inputs[1].default_value = 2

Which produces this error:

File "/Text", line 5, in <module>
AttributeError: 'SpaceTextEditor' object has no attribute 'context'

It seems as though the problem is that I'm not properly telling Blender what object I want to change?


The answer (thanks to poor):

import bpy   
bpy.context.scene.render.engine = 'CYCLES'
bpy.ops.object.lamp_add(type='SUN', view_align=False, location=(0, 0, 0))
bpy.context.object.data.node_tree.nodes['Emission'].inputs['Strength'].default_value=3
$\endgroup$
5
  • $\begingroup$ Welcome gwint :) If the duplicate above don't solve your issue feel free to comment here. $\endgroup$
    – p2or
    Jan 28, 2015 at 21:09
  • $\begingroup$ Hi poor! Still no luck. I tried the code from the answer you linked to ("node = bpy.context.active_object.active_material.node_tree.nodes['Emission']") but got the error "AttributeError: 'NoneType' object has no attribute 'node_tree'". It feels like I just need to know how to access the node data of the sun lamp I just created but I don't know how to. $\endgroup$
    – gwint
    Jan 28, 2015 at 21:27
  • $\begingroup$ (As an aside, I feel like part of the problem is I can't find a good intro guide to Blender 2.7x scripting for someone who's an experienced programmer but not an experienced Blenderer-- suggestions welcome :) $\endgroup$
    – gwint
    Jan 28, 2015 at 21:31
  • $\begingroup$ You can try: bpy.context.object.data.node_tree.nodes['Emission'].inputs[1].keyframe_insert(data_path="default_value",frame=120) The only difference is that lamps don't have a "active_material". You can use the console inside blender and the autocompletion there (CTRL+Space). Good resource is e.g. this: blenderscripting.blogspot.de $\endgroup$
    – p2or
    Jan 28, 2015 at 22:26
  • $\begingroup$ Yes! That's the special sauce: "bpy.context.object.data.node_tree..." Thank you poor! $\endgroup$
    – gwint
    Jan 28, 2015 at 22:33

0

Browse other questions tagged .