1

I'm creating a physically accurate light on Blender to recreate the Sunlight for a spatial scene. I'm using python Blender and i cannot understand how to connect shader Nodes, in particular the blackbody one, to light model. I'm using the code reported below, but the result is a total white light, without any input of color due to the shader Node. Thanks for your help!

bpy.ops.object.light_add(type='SUN', align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
bpy.context.active_object.name = SunLight["name"]

bpy.context.active_object.data.energy = SunLight["irradiance"]

bpy.context.active_object.data.angle = SunLight["angle"] / 57.2957795

bpy.context.active_object.rotation_euler = SunLight["rotation"]

bpy.context.active_object.data.use_nodes = True light = bpy.context.active_object.data nodes = light.node_tree.nodes lights_output = nodes.get('Light Output') lights_output.location = 0, 0 lights_output.width = 180 node_ox = nodes.get('Emission') node_ox.location = -200, 0 node_ox.width = 180 links = light.node_tree.links node_bb = nodes.new(type="ShaderNodeBlackbody") node_bb.inputs[0].default_value = SunLight['temp'] node_bb.location = -400, 0 node_bb.width = 180 link = links.new(node_bb.outputs[0], node_ox.inputs[0])

I've seen that some others had obtained the wanted result with a similar code, but i can't understand the mistake.

1 Answers1

5

Have a look into: Control Cycles/Eevee material nodes and material properties using python?

The emission node is created by default. All you really have to do is adding the blackbody node, set the properties and connect it using NodeLinks.new(): https://docs.blender.org/api/current/bpy.types.NodeTree.html?highlight=links#bpy.types.NodeTree.links

enter image description here

import bpy
import math

bpy.ops.object.light_add(type='SUN') obj = bpy.context.active_object

Sun properties

obj.data.energy = 1 obj.data.angle = math.radians(50) obj.rotation_euler.x = math.radians(45.0) obj.location.z = 2

Nodes

obj.data.use_nodes = True node_tree = obj.data.node_tree nodes = node_tree.nodes links = node_tree.links

Default emission node

emit_node = nodes.get("Emission") if emit_node: # Add blackbody node blackbody_node = nodes.new(type="ShaderNodeBlackbody") blackbody_node.location = (emit_node.location.x -200, emit_node.location.y) blackbody_node.inputs['Temperature'].default_value = 2300 links.new(blackbody_node.outputs['Color'], emit_node.inputs['Color'])

enter image description here

pyCod3R
  • 1,926
  • 4
  • 15