When we set up lighting in a scene in Blender, we usually set the direction of the light to the desired point manually for each light source. However, this process can be simplified and we can specify the point to which all selected light sources in the scene will be directed at once.
It’s very easy to do.
Select several light sources. Please note that one of them must be an active object in the scene. Now press Shift+ t on the keyboard and move the mouse.
When moving the mouse cursor, all selected light sources now “follow” the cursor with their directions. After clicking with the left mouse button, they will all remain directed to the click point.
This functionality is provided by executing the transform_axis_target() operator. We can also do this using the Blender Python API.
Since this operator is bound to the 3D viewport context, the operator call must be wrapped with temp_override().
First, we need to get pointers to the 3D viewport area and region.
1 2 |
area = [area for area in bpy.context.screen.areas if area.type == "VIEW_3D"][0] region = area.regions[-1] |
Now we can use a temporary override of the context by passing the received values in parameters,
1 |
with bpy.context.temp_override(area=area, region=region): |
and call the operator.
1 |
bpy.ops.object.transform_axis_target("INVOKE_REGION_WIN") |
Please note that the value “INVOKE_REGION_WIN” must be passed in the operator parameters, otherwise the operator will fail with an error.
At the same time, we can get rid of the need to necessarily assign one of the selected light sources to the active object. Since we are already using temp_override(), we can override the active object inside it by passing a pointer to it in the parameters.
1 2 3 4 5 6 7 8 9 |
area = [area for area in bpy.context.screen.areas if area.type == "VIEW_3D"][0] region = area.regions[-1] with bpy.context.temp_override( area=area, region=region, active_object=bpy.context.selected_objects[0] ): bpy.ops.object.transform_axis_target("INVOKE_REGION_WIN") |
By calling it, we can specify the direction point for light sources with the mouse in the 3D viewport working area.