2
$\begingroup$

I would like to specify the color of a light that I add using a python script launched from the command line:

blender --python script.py

The script:

import bpy

bpy.ops.object.light_add(type='AREA')
light_obj             = bpy.data.objects['Area']
light_obj.data.energy = 500
light_obj.color       = [1.0, 0.0, 0.0, 1.0]

The light power is correctly set to 500, but the light color remains at its default white color, instead of being red.

Should I do something more to set the color of the light?

Note that I am using Blender 2.80 with Debian 10.

I noted a few weird thing: The current online documentation of blender python api says here that the color of a light is a "float array of 3 items", but I get en error if I don't give 4 values. This post also talk about 3 values. Anyway, it proposes something similar to what I am trying to do: l.color = [r, g, b].

Is it en error in the documentation? Is their a bug?

$\endgroup$
1
  • 2
    $\begingroup$ Try light_obj.data.color = (1, 0, 0) $\endgroup$ Feb 8, 2020 at 22:19

1 Answer 1

5
$\begingroup$

Test code out in the console.

Even if you are going to use a script from command line always (IMO) a good idea to test it out in the python console. (D = bpy.data, C = bpy.context)

When adding an object via operator, the new object added is the context.object Notice the name below is not "Area" as there are already other area lights in the scene. Using light_ob = bpy.data.objects["Area"] will only work as expected if there are no other lights of that name in the blend file.

>>> bpy.ops.object.light_add(type='AREA')
{'FINISHED'}

>>> C.object #  the new object
bpy.data.objects['Area.002']

At any time can hit the autocomplete to see the available options. The light is the data part of the light object. (In the UI Properties panel it is under the Data tab.)

>>> light_ob = C.object
>>> light = light_ob.data
>>> light
bpy.data.lights['Area.002']

It's of type AreaLight which per documentation link posted in question is a subclass which inherits properties of bpy.types.Light

>> type(light)
<class 'bpy.types.AreaLight'>

>>> light.energy = 500

All objects have an rgba color property. For a lamp as commented above to set the lamp color set it on the data part.

>>> light.color = (1, 0, 0)
>>> 

The script.

import bpy


bpy.ops.object.light_add(type='AREA')
light_ob = bpy.context.object
light = light_ob.data
light.energy = 500
light.color = (1, 0, 0)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .