admin管理员组

文章数量:1404571

Summary

When an operator is run multiple times successively in blender, report messages (generated by report() API) from previous runs are also showing up in present run.

What is the reason behind it and how to address it properly?

Example code

import bpy

class ReporQueueNotClearedOperator(bpy.types.Operator):
    bl_idname = "object.report_queue_not_cleared"
    bl_label = "Test Report Queue"

    def execute(self, context):
        if len(context.selected_objects) == 0:
            self.report(
                {"ERROR_INVALID_INPUT"},
                "No object is selected. Cancelling!",
            )
            return {"CANCELLED"}
        if len(bpy.data.materials) == 0:
            self.report(
                {"ERROR"},
                "There are no materials in the scene. Not executing!",
            )
            return {"CANCELLED"}
        self.report(
            {"INFO"},
            "There are materials in the scene. Executing!",
        )
        return {'FINISHED'}

# Register the operator
def menu_func(self, _context):
    layout = self.layout
    layout.separator()
    layout.operator(
        ReporQueueNotClearedOperator.bl_idname,
        text=ReporQueueNotClearedOperator.bl_label,
    )

def register():
    bpy.utils.register_class(ReporQueueNotClearedOperator)
    bpy.types.VIEW3D_MT_add.append(menu_func)

def unregister():
    bpy.types.VIEW3D_MT_add.remove(menu_func)
    bpy.utils.unregister_class(ReporQueueNotClearedOperator)

if __name__ == "__main__":
    register()

Reproduction steps

  1. Open Blender (I'm using Blender 4.3.2 in Windows 11).
  2. Copy-paste the above code and execute the operator from Text Editor window.
  3. Come to 3D Viewport window. Make sure no object is selected.
  4. Click Add > Test Report Queue.
  5. Select the Cube object.
  6. Click Add > Test Report Queue.
  7. Remove all the materials using following code in Python Console window.
for mat in bpy.data.materials:
    bpy.data.materials.remove(mat)
  1. Come to 3D Viewport window. Select the Cube object.
  2. Click Add > Test Report Queue.
  3. Repeat step 3.
  4. Click Add > Test Report Queue.

Observation

After step 4

Expected and Actual message - No object is selected. Cancelling!

After step 6

Expected and Actual message - There are materials in the scene. Executing!

After step 9

Expected and Actual message - There are no materials in the scene. Not executing!

After step 11

Expected message - No object is selected. Cancelling!
Actual message - Along with No object is selected. Cancelling!, There are no materials in the scene. Not executing! log also appears on the status bar. Issue image

Inference

It looks like some instance of earlier reports are still present in report queue. I searched for clear/flush APIs/methods for report queue, but could not find any suitable answer.

I am not even sure if they indeed are maintained in a queue fashion. Please help!

本文标签: