Script working only inside blender I'm writing a script to limited delete all coplanar faces into one but I'm running into a little problem. As the title say the script work only if I open it into blender using the text block window and run it from there. If I run it using blender -b file.blend -P scritp.py it gave me an error and doesn't work at all.
Here the script (is inside a class because I'm planning to use it in a much bigger software)

import bpy


class Normalization:

    def __init__(self):
        self.scene = bpy.context.scene

    @staticmethod
    def retrieve_mesh():
        mesh_list = []

        for o in bpy.data.objects:
            if o.type == "MESH":
                mesh_list.append(o)

        return mesh_list

    def merge_coplanar(self, obj_list):
        for o in obj_list:
            try:
                self.scene.objects.active = o
                bpy.ops.object.mode_set(mode="EDIT")
                bpy.ops.mesh.select_mode(type="FACE")
                bpy.ops.mesh.dissolve_faces()
                bpy.ops.object.mode_set(mode="OBJECT")
            except Exception:
                bpy.ops.object.mode_set(mode="OBJECT")


# Start DEBUG section --------------------------------------------------------------------------------------------------
if __name__ == "__main__":
    n = Normalization()
    ml = n.retrieve_mesh()
    n.merge_coplanar(ml)
    print("Coplanar faces merged.\nDone!")


And here the error

Read new prefs: C:\Users\Andrea\AppData\Roaming\Blender Foundation\Blender\2.78\config\userpref.blend
AL lib: (EE) UpdateDeviceParams: Failed to set 48000hz, got 44100hz instead
found bundled python: C:\Program Files\Blender Foundation\Blender\2.78\python
version 3 imported
archimesh: Imported multifiles
Exception in module register(): 'C:\\Program Files\\Blender Foundation\\Blender\\2.78\\scripts\\addons\\object_boolean_tools.py'
Traceback (most recent call last):
  File "C:\Program Files\Blender Foundation\Blender\2.78\scripts\modules\addon_utils.py", line 349, in enable
    mod.register()
  File "C:\Program Files\Blender Foundation\Blender\2.78\scripts\addons\object_boolean_tools.py", line 1380, in register
    km = wm.keyconfigs.addon.keymaps.new(name='Object Mode')
AttributeError: 'NoneType' object has no attribute 'keymaps'
Exception in module register(): 'C:\\Users\\Andrea\\AppData\\Roaming\\Blender Foundation\\Blender\\2.78\\scripts\\addons\\uv_squares.py'
Traceback (most recent call last):
  File "C:\Program Files\Blender Foundation\Blender\2.78\scripts\modules\addon_utils.py", line 349, in enable
    mod.register()
  File "C:\Users\Andrea\AppData\Roaming\Blender Foundation\Blender\2.78\scripts\addons\uv_squares.py", line 957, in register
    km = wm.keyconfigs.addon.keymaps.new(name='UV Editor', space_type='EMPTY')
AttributeError: 'NoneType' object has no attribute 'keymaps'
read blend: C:\Users\Andrea\Desktop\t.blend
Error: Could not create merged face
Error: Could not create merged face
Error: Could not create merged face
Error: Could not create merged face
Coplanar faces merged.
Done!

Blender quit


Blender version: 2.78,
Python version: 3.5,
OS: Windows 10
Object/edit mode is not related to UI.

I think one issue here is that some add-ons do not handle their init code correctly (assuming keyconfigs are always present in wm, which iirc is not the case when Blender is started in background mode, a lot of things get disabled then).

So a first thing to try is to start blender with --factory-startup option, which only enables official add-ons (which are supposed to behave correctly).

Second point is that, afaics, you do not select anything in your mesh before calling the dissolve operator? So if no faces are selected in existing objects, dissolve cannot merge anything…