bpy.utils.register_class silently overrides existing classes with the same bl_idname #116370

Open
opened 2023-12-20 08:14:49 +01:00 by Andrej · 5 comments
Contributor

Any Blender version

Calling bpy.utils.register_class for two operators A and B with the same bl_idname causes operator B to be overridden by A without any warnings or errors -- we now have warnings since c5e2ec8a1a

Additionally A.unregister() is never called, so it's not really unregistered.

Which can be unsafe as two addons may have the same names for operators and in case of name collision the addon registered first will break silently / start behave unexpectedly. In case if override is intended then previous operator can be unregistered with bpy.utils.unregister_class.

import bpy

bl_info = {
    "name": "Example Addon",
    "author": "Your Name",
    "version": (1, 0),
    "blender": (2, 90, 0),
    "location": "Properties > Modifiers",
    "description": "",
    "warning": "",
    "category": "Modifiers",
}

class TestOperator1(bpy.types.Operator):
    bl_idname = "object.test_operator"
    bl_label = "Test Operator 1"
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        self.report({"INFO"}, self.bl_label)
        return {"FINISHED"}

    @classmethod
    def register(self):
        print(f'register {self.bl_label}')

    @classmethod
    def unregister(self):
        print(f'unregister {self.bl_label}')

class TestOperator2(bpy.types.Operator):
    bl_idname = "object.test_operator"
    bl_label = "Test Operator 2"
    bl_options = {"REGISTER", "UNDO"}

    def execute(self, context):
        self.report({"INFO"}, "2")
        return {"FINISHED"}

    @classmethod
    def register(self):
        print(f'register {self.bl_label}')

    @classmethod
    def unregister(self):
        print(f'unregister {self.bl_label}')

def register():
    bpy.utils.register_class(TestOperator1)
    bpy.utils.register_class(TestOperator2)

if __name__ == "__main__":
    register()
    bpy.ops.object.test_operator() # Info: 2

Any Blender version Calling `bpy.utils.register_class` for two operators A and B with the same `bl_idname` causes operator B to be overridden by A without any warnings or errors -- we now have warnings since c5e2ec8a1af5ca2b8f1a3a35692f5b034c38b357 Additionally `A.unregister()` is never called, so it's not really unregistered. Which can be unsafe as two addons may have the same names for operators and in case of name collision the addon registered first will break silently / start behave unexpectedly. In case if override is intended then previous operator can be unregistered with `bpy.utils.unregister_class`. ```python import bpy bl_info = { "name": "Example Addon", "author": "Your Name", "version": (1, 0), "blender": (2, 90, 0), "location": "Properties > Modifiers", "description": "", "warning": "", "category": "Modifiers", } class TestOperator1(bpy.types.Operator): bl_idname = "object.test_operator" bl_label = "Test Operator 1" bl_options = {"REGISTER", "UNDO"} def execute(self, context): self.report({"INFO"}, self.bl_label) return {"FINISHED"} @classmethod def register(self): print(f'register {self.bl_label}') @classmethod def unregister(self): print(f'unregister {self.bl_label}') class TestOperator2(bpy.types.Operator): bl_idname = "object.test_operator" bl_label = "Test Operator 2" bl_options = {"REGISTER", "UNDO"} def execute(self, context): self.report({"INFO"}, "2") return {"FINISHED"} @classmethod def register(self): print(f'register {self.bl_label}') @classmethod def unregister(self): print(f'unregister {self.bl_label}') def register(): bpy.utils.register_class(TestOperator1) bpy.utils.register_class(TestOperator2) if __name__ == "__main__": register() bpy.ops.object.test_operator() # Info: 2 ```
Andrej added the
Status
Needs Triage
Priority
Normal
Type
Report
labels 2023-12-20 08:14:49 +01:00
Member

Can confirm the behavior.

Not sure though if this can be considered a bug, will quickly check if we could/should at least spit out a warning/error

Can confirm the behavior. Not sure though if this can be considered a bug, will quickly check if we could/should at least spit out a warning/error
Philipp Oeser added
Module
Python API
Status
Confirmed
and removed
Status
Needs Triage
labels 2023-12-20 09:40:52 +01:00
Member

OK, checked a bit and the behavior of unregistering previous found operators (keyconf-prefs, gizmos, add-on preferences, ... many types affected...) is intended behavior.

(for affected types, check /* Check if we have registered this in code)

However I think it makes sense to spit out a warning in these cases, will put up a PR for this shortly.

OK, checked a bit and the behavior of unregistering previous found operators (keyconf-prefs, gizmos, add-on preferences, ... many types affected...) is intended behavior. (for affected types, check `/* Check if we have registered this` in code) However I think it makes sense to spit out a warning in these cases, will put up a PR for this shortly.
Philipp Oeser self-assigned this 2023-12-20 10:42:23 +01:00
Member

and A.unregister() is never called, so it's not really unregistered

Ah, still have to check on this one, looking at code this should actually happen I think

>and A.unregister() is never called, so it's not really unregistered Ah, still have to check on this one, looking at code this should actually happen I think
Member

Will unassign (since it is not something I am planning to work on), however the unregister() issue still needs to be looked at

Will unassign (since it is not something I am planning to work on), however the `unregister()` issue still needs to be looked at
Philipp Oeser removed their assignment 2024-03-27 14:35:52 +01:00
Philipp Oeser added
Status
Needs Triage
and removed
Status
Confirmed
labels 2024-03-27 14:35:59 +01:00
Member

Additionally A.unregister() is never called, so it's not really unregistered...Which can be unsafe as two addons may have the same names for operators

Hi, rna_Operator_unregister is called for the first operator. So two operators of same names will never exists: https://projects.blender.org/blender/blender/src/branch/main/source/blender/makesrna/intern/rna_wm.cc#L1569-L1578

Guess you're expecting the execution of unregister() fn defined in python, right? Not sure how this is handled, forwarding to devs for more info.

> Additionally A.unregister() is never called, so it's not really unregistered...Which can be unsafe as two addons may have the same names for operators Hi, `rna_Operator_unregister` is called for the first operator. So two operators of same names will never exists: https://projects.blender.org/blender/blender/src/branch/main/source/blender/makesrna/intern/rna_wm.cc#L1569-L1578 Guess you're expecting the execution of unregister() fn defined in python, right? Not sure how this is handled, forwarding to devs for more info.
Pratik Borhade added
Status
Needs Info from Developers
and removed
Status
Needs Triage
labels 2024-04-11 09:09:57 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#116370
No description provided.