I'm trying to add a simple triangle to a scene (with Blender demo BMW) using python script. I then want to enter edit mode, select a vertex and then move 3D cursor to that vertex, exit edit mode and then add an area light at the 3D cursor. Repeat to set up very basic three-point lighting. However, when I deselect all objects, and then try to select my "LightingRig" object using the code example, I can see that the triangle is active (not selected, as intended) and I get the following error:
RuntimeError: Operator bpy.ops.object.mode_set.poll() Context missing active object
How can I set the triangle to be selected, and not active?
import bpy
import os
import sys
bpy.ops.wm.append(
filepath=os.path.join(lr_file_path, inner_path, lr_object_name),
directory=os.path.join(lr_file_path, inner_path),
filename=lr_object_name
)
lr = bpy.data.objects.get("LightingRig")
bpy.ops.object.select_all(action='DESELECT')
lr.select_set(state=True)
bpy.ops.object.mode_set(mode="EDIT") # This is where the error occurs
I'm trying this:
bpy.ops.object.vertex_group_select()
bpy.ops.view3d.snap_cursor_to_selected()
This gives similar context error:
RuntimeError: Operator bpy.ops.view3d.snap_cursor_to_selected.poll() failed, context is incorrect
– Chris_S May 17 '22 at 14:49bpy.opsare for interactive use through the UI and should be avoided in scripts because they depend on context. Using them through scripts may yield unexpected results at best, or fail entirely at worst. Manipulate data directly frombpy.datainstead. – Duarte Farrajota Ramos May 18 '22 at 12:06