Context missing active object

Hi there, I’m having some problems changing the mode to “EDIT” when using Blender as a Python module, it basically when running:

bpy.ops.object.mode_set(mode=‘EDIT’)

it returns:

RuntimeError: Operator bpy.ops.object.mode_set.poll() Context missing active object

Even though I have set an active object:

context.view_layer.objects.active = central_curve_obj

Any help is much appreciated!! Thank u in advanced

Not enough code shown to be sure but a couple guesses of possible problems.

  • the variable central_curve_obj is not being set properly
  • the variable central_curve_obj is set in a different function but not referenced as a global variable both when set and referenced
    Either of the above could cause central_curve_obj to be a nonetype object

Hi there, thank you for the reply!

Here is how I’m setting central_curve_obj:

curve = bpy.data.curves.new(“curve1”, “CURVE”)
central_curve_obj = bpy.data.objects.new(“CentralCurve”, curve)
context.collection.objects.link(central_curve_obj)
context.view_layer.objects.active = central_curve_obj

context.view_layer.objects.active.rotation_euler = rot
context.view_layer.objects.active.location = loc
context.view_layer.objects.active.scale = sca

bpy.ops.object.mode_set(mode=‘EDIT’)
bpy.ops.curve.select_all(action=‘SELECT’)
bpy.ops.curve.subdivide(number_cuts=cen_subdivi)
bpy.ops.curve.select_all(action=‘DESELECT’)
bpy.ops.object.mode_set(mode=‘OBJECT’)

The thing is, when I do print of context.view_layer.objects.active, it prints the curve. Everything works fine when doing this with Blender running, but when I try to work with its python module only, this happens.

Did you try a viewlayer update?

I’m quite new to this. I have used it to set the object to active and change some of its properties, is there something else I should do with the viewlayer?

bpy.context.view_layer.update()

Just a guess, if its working fine with blenders ui, that might be the difference.

Sadly the error still persists

Strange. Hard to say, testwise try an additional selection ( obj.select_set(True) ) or the bpy.context directly

Nop, still nothing :pensive:

What does it say if you print the bpy.context.active_object

If I don’t use active_layer it returns:

AttributeError: ‘Context’ object has no attribute ‘active_object’

You mean bpy.context.view_layer.objects.active ?

print(bpy.context.view_layer.objects.active)

returns:
<bpy_struct, Object(“CentralCurve”) at 0x7fb15c115408>

but print(bpy.context.active_object)

returns the error:
AttributeError: ‘Context’ object has no attribute ‘active_object’

Hmm, somethings wrong with your context there, have a read here. you might try overriding it.

Sorry I’m not sure, whats causing this to work in one scenario but not in the other.
Maybe someone else has an idea.

That’s alright, thank u very much!

I don’t use blender as a python module so any suggestions I provide will be limited.

BlenderAsPyModule
info_advanced_blender_as_bpy

Your code above is still has many issues to consider:

  • When constructing a curve like this curve = bpy.data.curves.new(“curve1”, “CURVE”) only a single point is added to the mesh data block. How to specify Nurbs path vertices in python? - Blender Stack Exchange is an old post I answered on BSE that creates a curve from given points. It would need to be updated for linking to collections instead of scene but should help with creating a curve.
  • context.view_layer.objects.active.rotation_euler = rot
    ** if you are not setting the active object and only want to get the active object use context.object.rotation_euler = rot
    ** variables rot, loc, sca are not defined in your code while readers can just plug in numbers every additional error that someone has to fix detracts time from attempting to help with your original issue

Ideally when you post code you want it to be easy for others to copy and paste as such code blocks should be pasted as code use the </> button this also corrects issues with single quote and double quote ascii codes which otherwise have to be replaced manually by the user copying your code.

print(bpy.context.object)

I have managed to set the mode to “EDIT” by overriding the context:

with bpy.context.temp_override(
                    window=window,
                    area=area,
                    region=region,
                    active_object=central_curve_obj):
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.curve.select_all(action='SELECT')
    bpy.ops.curve.subdivide(number_cuts=cen_subdivi)
    bpy.ops.curve.select_all(action='DESELECT')
    bpy.ops.object.mode_set(mode='OBJECT')

The problem now is trying to select the points of the curve, I get the following error:

RuntimeError: Operator bpy.ops.curve.select_all.poll() failed, context is incorrect

And if I do print:

print("----- Values after overriding -----")
print("Active Object:", bpy.context.active_object)
print("Window:", bpy.context.window)
print("Area:", bpy.context.area)
print("Region:", bpy.context.region)
print("==================================")

I get:

----- Values after overriding -----
Active Object: <bpy_struct, Object("CentralCurve") at 0x7fcf7fc5c408>
Window: None
Area: None
Region: None
==================================