Setting blender context for uv editing issue I'm trying to run this command, I crash after print 1, which I guess has something to do with setting another context but no idea how to bite it to get it to work... Any hints?
        print(&quot;Update graph&quot;)
        bpy.context.evaluated_depsgraph_get()
        for window in bpy.context.window_manager.windows:
            screen = window.screen
            for area in screen.areas:
                if area.type == 'VIEW_3D':
                    #set context to 3dview
                    cont = bpy.context.copy()
                    cont['area'] = area
                    cont['region'] = area.regions[-1]
                    bpy.ops.view3d.view_selected(cont)
                    break

        if len(objects) &gt; 0 and cont is not None:
            # Deselect all
            for obj in objects:
                if obj.type == 'MESH':
                    obj.select_set(state=False)

            #Select 1 by 1, and apply the uv system.
            for obj in objects:
                if obj.type == &quot;MESH&quot;:
                    obj.select_set(state=True)
                    print(&quot;1&quot;,cont)
                    bpy.ops.object.editmode_toggle(cont)
                    print(&quot;2&quot;,cont)
                    bpy.ops.mesh.select_all(action='SELECT')
                    print(&quot;3&quot;,cont)
                    bpy.ops.uv.smart_project(cont)
                    print(&quot;4&quot;,cont)
                    bpy.ops.object.editmode_toggle(cont)
                    obj.select_set(state=False)

TIA
I have missed some explanations.
This function is called by a persistent handler
@persistent
def load_handler(dummy):
    bpy.app.timers.register(actionQueue.runInternalFastTimer)

def register()
      bpy.app.handlers.load_post.append(load_handler)

Its done this way as I'm sending command to it from a threadX that is not blender thread. So the idea is to push job function ptr to a queue that then  gets called/executed by blender in its own correct thread.
However at that stage I have wrong context and I get this error &gt;
RuntimeError: Operator bpy.ops.object.editmode_toggle.poll() failed, context is incorrect


EDIT A more comprehensive example of the issue can be found here (second post) &gt; https://devtalk.blender.org/t/creating-a-valid-context-for-uv-editing/15670
Running this script from the cli, I can unwrap an object in the background (no gui window).
I think the key point is setting the active object not just selecting it. I also create a uvmap layer if one doesn't exist and use mode_set rather that editmode_toggle.
import bpy

# Deselect all
bpy.ops.object.select_all(action='DESELECT')

for obj in bpy.context.scene.objects:
    if obj.type == 'MESH':
        obj.select_set(state=True)
        bpy.context.view_layer.objects.active = obj
        bpy.ops.object.mode_set(mode='EDIT')
        bpy.ops.mesh.select_all(action='SELECT')
        if len(obj.data.uv_layers) == 0:
            obj.data.uv_layers.new()
        bpy.ops.uv.smart_project()
        bpy.ops.object.mode_set(mode='OBJECT')
        bpy.ops.wm.save_mainfile()

I saved it as uvproject.py and used the cli command
blender -b test.blend -P uvproject.py