Don S
Don S

Reputation: 21

Blender > Adding Nodes to a Light Using Python

I'm new to Python and scripting for Blender. The "copy from console" method has been working for me so far, but I've hit a wall.

I've made an add-on for Blender 2.8 that adds and positions three area lights to a scene. I'd like to add a Blackbody converter to each light, set the value to 5000, and connect it to the Emission Shader, but I keep getting errors along the lines of

AttributeError: 'AreaLight' object has no attribute 'add_node'

with every path variation I've tried.

Here's the basic script for each light I'm using:

import bpy

bpy.ops.object.light_add(type='AREA', radius=10, location=(5, 1.5, 5))
bpy.context.active_object.data.use_nodes = True
bpy.context.active_object.data.node.add_node(type="ShaderNodeBlackbody", use_transform=True)
bpy.context.active_object.name = "Area_Right"
bpy.context.active_object.data.shape = 'RECTANGLE'
bpy.context.active_object.data.energy = 300
bpy.context.active_object.data.size = 1
bpy.context.active_object.data.size_y = 3

bpy.context.active_object.data.node.add_node(type="ShaderNodeBlackbody", use_transform=True)

is the line that's been blocking me for the last few weeks.

Anyone have any suggestions?

Upvotes: 1

Views: 1461

Answers (1)

Don S
Don S

Reputation: 21

So I found the solution, and OF! COURSE! it took me less than 20 mins of googling. 🤦🏻‍♀️

Here's the solution:

light   = bpy.context.active_object.data
nodes   = light.node_tree.nodes
node_bb = nodes.new(type="ShaderNodeBlackbody")
node_bb.inputs[0].default_value = 5000
node_bb.location = -200,300
node_ox = nodes.get('Emission')
links   = light.node_tree.links
link    = links.new(node_bb.outputs[0], node_ox.inputs[0])`

I tweaked code from these two posts:

Control Cycles material nodes and material properties in Python

Add Nodes to Material with Python

If you'd like to use the script, or review it and suggest improvements, I've posted version 1.2 of the Addon to Github:

Blender QLE

Upvotes: 1

Related Questions