3
$\begingroup$

I am newbie to Blender scripting, but I am trying to make very simple Addon with the help of the YT tutorial that looks like this but it is giving me error that something is wrong, tho there is no error message in the console window.

The script should appear as a menu item inside OBJECT -> APPLY, and should just apply all the active modifiers in all selected objects. the script of the Addon looks like this:

bl_info = {
    "name": "Apply Modifiers In Selected",
    "author": "Anonymous",
    "version": (1, 0),
    "blender": (2, 90, 1),
    "location": "View3D > Object > Apply > Modifiers In Selected",
    "description": "Applies all the modifier in selected objects",
    "warning": "",
    "doc_url": "",
    "category": "Object Apply",
}

import bpy
from bpy.types import (
    AddonPreferences,
    Operator,
    Panel,
    PropertyGroup,
)

class OBJECT_OT_applymodifiersinselected(Operator):
    bl_label = "Apply Modifiers In Selected"
    bl_idname = "object.apply.modifiersinselected"
    bl_description = "Applies all the modifier in selected objects"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_options = {'REGISTER', 'UNDO'}
    
    def execute (self, context):

        for ob in bpy.context.selected_objects:
            bpy.context.view_layer.objects.active = ob
            for name in [m.name for m in ob.modifiers]:
                modifier = ob.modifiers[name]
                if modifier.show_viewport:
                    bpy.ops.object.modifier_apply( modifier = name )
        
        return {'FINISHED'}
    
def menu_func (self, context):
    self.layout.operator(OBJECT_OT_applymodifiersinselected.bl_idname)

def register():
    bpy.utils.register_class(OBJECT_OT_applymodifiersinselected)
    bpy.types.VIEW3D_MT_object_apply.append(menu_func)
    
def unregister():
    bpy.utils.unregister_class(OBJECT_OT_applymodifiersinselected)
    bpy.types.VIEW3D_MT_object_apply.remove(menu_func)

if __name__ == "__main__":
    register()

Can anyone, please, tell me what is wrong with the code?

$\endgroup$
2
$\begingroup$

The bl_idname is not allowed to contain more than one dot. The error message is:

Traceback (most recent call last):
  File "\Text", line 52, in <module>
  File "\Text", line 44, in register
RuntimeError: Error: Registering operator class: 'OBJECT_OT_applymodifiersinselected', invalid bl_idname 'object.apply.modifiersinselected', must contain 1 '.' character

This message can be seen when starting Blender from the terminal on Linux, macOS and Windows. On Windows you can also use Window > Toggle System Console.

In order to fix this pick a different bl_idname, for example:

 bl_idname = "object.apply_modifiers_in_selected"
$\endgroup$
2
  • $\begingroup$ great, works now! BTW is there a way how to tell the Blender in what position in the submenu it should appear? Right now it is at the very end at the bottom - can I move it to some more elevated position and even add a separator line? $\endgroup$ – qraqatit Oct 29 '20 at 21:35
  • 1
    $\begingroup$ You can't set the order as far as I'm aware. $\endgroup$ – Robert Gützkow Oct 29 '20 at 21:45

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.