1
$\begingroup$

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

| improve this question | | | | |
$\endgroup$
  • $\begingroup$ I think that the UI is not yet loaded when you execute the script and as a result you cannot switch to "object" mode. I had this issue also and I resolved it by 1) putting the script in a text editor and 2) use the addon "Animation Nodes" to call the script. the script itselfs was reading a property to validate if it should execute, doing its stuff, set the property when completed, save the file and quit blender. During all these operations, blender UI was running (so it was not running in the background". $\endgroup$ – Kristof Meirlaen Oct 17 '16 at 17:27
  • $\begingroup$ OK so I cannot make it work with blender background? If it's the case there are other method to do so with blender -b? $\endgroup$ – Paranoid Nemo Oct 17 '16 at 17:32
  • $\begingroup$ Doesn't look like the errors prevent your script from running though? $\endgroup$ – kheetor Oct 19 '16 at 13:32
3
$\begingroup$

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…

| improve this answer | | | | |
$\endgroup$
  • $\begingroup$ bpy.ops.mesh.select_mode(type="FACE") $\endgroup$ – Paranoid Nemo Oct 19 '16 at 14:37
  • $\begingroup$ This line select all the faces in the object, or at least, that's what happens when I run it in edit mode. $\endgroup$ – Paranoid Nemo Oct 19 '16 at 14:38
  • $\begingroup$ @ParanoidNemo select_mode(type='FACE') puts blender into face select mode, as opposed to selecting a face. $\endgroup$ – batFINGER Oct 19 '16 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.