Can you add a light source in blender using python

innov83r picture innov83r · Jun 28, 2013 · Viewed 9k times · Source

Alright, I'm totally new to Blender and am just looking for some good tutorials on how to use python to control it. I want to be able to add/remove/edit light sources via python methods... can this be done? Thanks for any advice.

Answer

Gauthier Boaglio picture Gauthier Boaglio · Jun 28, 2013

Answer is Yes!

Look at the recent Python API.

The example below creates a new Lamp object and puts it at the default location (5, 5, 5) in the current scene:

(Blender 2.63)

The script should look like this:

import bpy

scene = bpy.context.scene

# Create new lamp datablock
lamp_data = bpy.data.lamps.new(name="New Lamp", type='POINT')

# Create new object with our lamp datablock
lamp_object = bpy.data.objects.new(name="New Lamp", object_data=lamp_data)

# Link lamp object to the scene so it'll appear in this scene
scene.objects.link(lamp_object)

# Place lamp to a specified location
lamp_object.location = (5.0, 5.0, 5.0)

# And finally select it make active
lamp_object.select = True
scene.objects.active = lamp_object