Calling operator from handler that saves bmesh freezes blender forever #108865

Open
opened 2023-06-11 20:35:01 +02:00 by Andrej · 1 comment
Contributor

Blender version: 3.5.1

I setup some shader to run on top of blender viewport and connected it’s update to SpaceView3D.draw_handler_add. The idea was that shader will only work in EDIT mode and if user exists it then it will automatically trigger some operator.
Line that freezes Blender is bm.to_mesh(context.object.data) although it doens’t freeze right away.

What I’ve found that if I call operator that edits mesh from bmesh then it freezes Blender entirely.
Currently as a workaround I moved operator’s code to some function and call it instead of because bm.to_mesh(context.object.data) doesn’t seem to freeze Blender when it’s called from handler without operator.

Easy way to reproduce (create default scene, make sure cube is selected, run the script):

import bpy
from bpy.types import SpaceView3D
import bmesh

class TestHandler:
    installed = None

    @classmethod
    def install(cls, context):
        handler = cls()
        cls.installed = SpaceView3D.draw_handler_add(
            handler, (context, ), "WINDOW", "POST_VIEW"
        )

    @classmethod
    def uninstall(cls):
        try:
            SpaceView3D.draw_handler_remove(cls.installed, "WINDOW")
        except ValueError:
            pass
        cls.installed = None

    def __call__(self, context):
        obj = context.object
        if obj.mode != "EDIT":
            TestHandler.uninstall()
            bpy.ops.test.test()
            print('HANDLER CALL FINISHED') # never printed
            return
        
        print('HANDLER FINISHED')

class TestOperator(bpy.types.Operator):
    bl_idname = "test.test"
    bl_label = "test"

    def execute(self, context):
        print('TEST OPERATOR CALLED')
        bm = bmesh.new()

        # WITHOUT THIS LINE IT DOESN'T FREEZE
        bm.to_mesh(context.object.data)

        # still printed
        print('TEST OPERATOR FINISHED')
        return {"FINISHED"}
    
class TestSetupOperator(bpy.types.Operator):
    bl_idname = "test.test_setup"
    bl_label = "test"

    def execute(self, context):
        print('TEST SETUP OPERATOR CALLED')
        TestHandler.install(context)
        return {"FINISHED"}


bpy.utils.register_class(TestOperator)
bpy.utils.register_class(TestSetupOperator)
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.test.test_setup()
# freezes on the next step
bpy.ops.object.mode_set(mode="OBJECT")
Blender version: 3.5.1 I setup some shader to run on top of blender viewport and connected it’s update to `SpaceView3D.draw_handler_add`. The idea was that shader will only work in EDIT mode and if user exists it then it will automatically trigger some operator. Line that freezes Blender is `bm.to_mesh(context.object.data)` although it doens’t freeze right away. What I’ve found that if I call operator that edits mesh from bmesh then it freezes Blender entirely. Currently as a workaround I moved operator’s code to some function and call it instead of because `bm.to_mesh(context.object.data)` doesn’t seem to freeze Blender when it’s called from handler without operator. Easy way to reproduce (create default scene, make sure cube is selected, run the script): ```python import bpy from bpy.types import SpaceView3D import bmesh class TestHandler: installed = None @classmethod def install(cls, context): handler = cls() cls.installed = SpaceView3D.draw_handler_add( handler, (context, ), "WINDOW", "POST_VIEW" ) @classmethod def uninstall(cls): try: SpaceView3D.draw_handler_remove(cls.installed, "WINDOW") except ValueError: pass cls.installed = None def __call__(self, context): obj = context.object if obj.mode != "EDIT": TestHandler.uninstall() bpy.ops.test.test() print('HANDLER CALL FINISHED') # never printed return print('HANDLER FINISHED') class TestOperator(bpy.types.Operator): bl_idname = "test.test" bl_label = "test" def execute(self, context): print('TEST OPERATOR CALLED') bm = bmesh.new() # WITHOUT THIS LINE IT DOESN'T FREEZE bm.to_mesh(context.object.data) # still printed print('TEST OPERATOR FINISHED') return {"FINISHED"} class TestSetupOperator(bpy.types.Operator): bl_idname = "test.test_setup" bl_label = "test" def execute(self, context): print('TEST SETUP OPERATOR CALLED') TestHandler.install(context) return {"FINISHED"} bpy.utils.register_class(TestOperator) bpy.utils.register_class(TestSetupOperator) bpy.ops.object.mode_set(mode="EDIT") bpy.ops.test.test_setup() # freezes on the next step bpy.ops.object.mode_set(mode="OBJECT") ```
Andrej added the
Type
Report
Status
Needs Triage
Priority
Normal
labels 2023-06-11 20:35:02 +02:00
Member

It's probably not expected to call operator "POST_VIEW", since it will always be called when there's a viewport update, but need more investigation.

When Blender is frozen, the CPU usage here shows zero so it might not be a stuck loop, looks more likely a dead lock situation.

For now try not to call operator within a handler, will poke developers about this.

It's probably not expected to call operator "POST_VIEW", since it will always be called when there's a viewport update, but need more investigation. When Blender is frozen, the CPU usage here shows zero so it might not be a stuck loop, looks more likely a dead lock situation. For now try not to call operator within a handler, will poke developers about this.
YimingWu added the
Interest
Python API
label 2023-06-12 05:49:33 +02:00
YimingWu added
Status
Needs Info from Developers
Module
Python API
and removed
Status
Needs Triage
labels 2023-06-12 06:01:16 +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
2 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#108865
No description provided.