Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you add a light source in blender using python

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.

like image 396
innov83r Avatar asked Jun 28 '13 00:06

innov83r


People also ask

How do I add a light source in Blender?

Press Shift + A, then mouse over Light, and click on Area. Once you have chosen the Area Light object and inserted it into the scene, you can place it anywhere.

What can Python do in Blender?

Python scripts are a versatile way to extend Blender functionality. Most areas of Blender can be scripted, including animation, rendering, import and export, object creation and automating repetitive tasks. To interact with Blender, scripts can make use of the tightly integrated API .

Can Blender be used with Python?

Python in BlenderBlender has an embedded Python interpreter which is loaded when Blender is started and stays active while Blender is running. This interpreter runs scripts to draw the user interface and is used for some of Blender's internal tools as well.


2 Answers

Blender 2.80 broke the old API, most steps changed. Updated code below.

import bpy

# create light datablock, set attributes
light_data = bpy.data.lights.new(name="light_2.80", type='POINT')
light_data.energy = 30

# create new object with our light datablock
light_object = bpy.data.objects.new(name="light_2.80", object_data=light_data)

# link light object
bpy.context.collection.objects.link(light_object)

# make it active 
bpy.context.view_layer.objects.active = light_object

#change location
light_object.location = (5, 5, 5)

# update scene, if needed
dg = bpy.context.evaluated_depsgraph_get() 
dg.update()
like image 139
onorabil Avatar answered Oct 21 '22 05:10

onorabil


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
like image 37
Gauthier Boaglio Avatar answered Oct 21 '22 06:10

Gauthier Boaglio