Using object.mode_set() Without GUI

Can someone explain the use of “object.mode_set()” when using Blender without the GUI? The poll() for this function seems to require the GUI to be loaded, but I don’t understand why this would be the case.

I am using Blender 2.54.

The following script creates a monkey, and then triangulates it.

import bpy

# Add a monkey

# Triangulate

When I run the script from within blender, it works as expected.
When I run the script from the command line, but loading the GUI, it works as expected:

# Works correctly
blender -P

But when I run the script from the command line, without loading the GUI, it fails:

# Fails
blender -b -P

The return is: “Operator bpy.ops.object.mode_set.poll() failed, context is incorrect.”

Perhaps there’s a good reason for the mode_set() function to require the GUI to be loaded. But is there really no way to change the mode then?

Thanks so much.


Hello, I’m sorry to up this old thread, but I have the same problem.
I would like create a script launched from a windows terminal
I used option 'blender.exe -P" and I have the fallowing error:

RuntimeError: Operator bpy.ops.mesh.extrude_vertices_move.poll() failed, context
is incorrect

The error occurs in this function

def creationPoutre_cone(a):
	Poucone_mesh = bpy.ops.mesh.primitive_circle_add(vertices=32, radius=0.95, location=(-0.947816, -1.9005, -0.915294), rotation=(0, math.pi/2, 0)) = bpy.context.object

	bpy.ops.mesh.extrude_vertices_move(MESH_OT_extrude={"type":'VERTS'}, TRANSFORM_OT_translate={"value":(5, 0, 0), "constraint_axis":(True, False, False)})									
	bpy.ops.transform.resize(value=(1.0, 1.0, 1.0), constraint_axis=(False, False, False))

I tried many solutions but nothing works as expected…

Thanks for your help :slight_smile:

No idea ?
Thanks :o