Context switch during SVG import

Hi guys !
I’m writing a small script as an exercise, which is meant by now only to import svg (circles), and to convert them into meshes.
And I’ve got context issues.

I’ve wandered a little bit over the internet, and it seems that context is a typical beginner issue. So glad am I, as a beginner ! Yet I couldn’t figure out this thing…yet.

Here are my very few lines of code :


import bpy
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=True)
        
bpy.ops.import_curve.svg(filepath="C:\\Users\\g.svg")
bpy.ops.import_curve.svg(filepath="C:\\Users\\pg.svg")

for i,j in enumerate(bpy.context.selected_objects):
    j.location.z+=(i)*0.5/len(bpy.context.selected_objects)
    
bpy.ops.object.convert(target='MESH',keep_original=False)
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.join()


My code break is around the bpy.ops.object.convert(target=‘MESH’, keep_original=False).
Console keeps telling me that the related poll function failed, due to an incorrect context. Though I don’t know how to modify this context.
Note as well that if I remove this line, the bpy.ops.object.join() triggers the same error.

Do you have any guidelines or suggestions ?

Operators do certain checks to see if they are allowed to work. (e.g. edit-mode operators check if they are in edit mode, object-mode operators check if they are in object mode, etc). So you could check if you are in object mode.

Alright, so I’ve progressed… by raising new errors !
But it allowed me to understand better what was the underlying problem (I think).
I realize that I didn’t tell you that this script is launched from command-line called blender, aka :

C:\Users\Siskas>"C:\Program Files\Blender Foundation\Blender\blender.exe" -P C:\Users\Siskas\BlenderRun_testjpg.py

After checking for different things, I realized that even switching to object mode (bpy.ops.object.mode_set(mode=‘OBJECT’) ) was raising a poll error.

I ran an override according to the doc (applyed on a select_all function), and it then raised a new error.
Here is the code :

screen = bpy.context.window.screen
for area in screen.areas:
    print('-',area.type)
    if area.type == 'VIEW_3D':
        for region in area.regions:
            print('----',region.type)
            if region.type == 'WINDOW':
                override = {'window': bpy.context.window, 'screen': screen, 'area': area, 'region': region}
                break
bpy.ops.object.select_all(override,action='SELECT')

Here are the errors.

PyContext 'active_object' not found
PyContext 'scene' not found
PyContext 'edit_object' not found
PyContext 'active_object' not found
PyContext 'scene' not found
PyContext 'edit_object' not found
PyContext 'visible_bases' not found
PyContext 'scene' not found
PyContext 'visible_bases' not found
PyContext 'scene' not found
PyContext 'scene' not found

#####
RuntimeError: Operator bpy.ops.object.mode_set.poll() failed, context is incorrect

What I understand is that Blender is simply not ‘initialized’ (context-wise), as the script is running from command line.
Do you know how to make its initialization ?
Do I have to add to the override all the missing contexts ?

Thank you !