2
$\begingroup$

I want to automatically create a point light source, but I can't get my blender script to work. Im using the latest blender version 2.92. After loading the default scene and loading my my obj model, I first delete the original light and then create a new light source:

# remove default light    
bpy.ops.object.select_by_type(type='LIGHT')
bpy.ops.object.delete(use_global=False)

# Create new light
lamp_data = bpy.data.lights.new(name="Lamp", type='POINT')
lamp_data.energy = 1000
lamp_object = bpy.data.objects.new(name="Lamp", object_data=lamp_data)
bpy.context.collection.objects.link(lamp_object)
lamp_object.location = (0, 0, 2)

In the Cycles viewport I can see that the light is illuminating my object, but when I render with F12 my object is just black, see the screenshot enter image description here What am I missing here?

$\endgroup$
1
  • $\begingroup$ Check the layer, its one $\endgroup$ – Yohello 1 Mar 19 at 18:18
4
$\begingroup$

Recommended way is to use low level API calls. Create a new light data block using BlendDataLights.new(), create a new object using ObjectData.new(), link the light data block and add the object to the collection in context using CollectionObjects.link(object):

import bpy

# Create light datablock
light_data = bpy.data.lights.new(name="my-light-data", type='POINT')
light_data.energy = 100

# Create new object, pass the light data 
light_object = bpy.data.objects.new(name="my-light", object_data=light_data)

# Link object to collection in context
bpy.context.collection.objects.link(light_object)

# Change light position
light_object.location = (0, 0, 3)

enter image description here

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.