I have to create Blender tools which will open as separate window, and once done with the process, I close them explicitly, I was testing it with a sample tool here which opens as a separate window, the Python script for which is as follows:
import bpy
class MyCustomPanel(bpy.types.Panel):
bl_idname = "OBJECT_PT_custom_panel"
bl_label = "My Custom Panel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'Custom Addon'
def draw(self, context):
layout = self.layout
row = layout.row()
row.operator("object.my_operator")
class MyCustomOperator(bpy.types.Operator):
bl_idname = "object.my_operator"
bl_label = "My Custom Operator"
def execute(self, context):
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self, width=200)
def draw(self, context):
layout = self.layout
layout.label(text="My Custom Operator")
row = layout.row()
row.operator("mesh.primitive_cube_add", text="Add Cube")
row.operator("mesh.primitive_cone_add", text="Add Cone")
def register():
bpy.utils.register_class(MyCustomPanel)
bpy.utils.register_class(MyCustomOperator)
def unregister():
bpy.utils.unregister_class(MyCustomPanel)
bpy.utils.unregister_class(MyCustomOperator)
if name == "main":
register()
But there is one issue with this one, as soon as I am clicking somewhere else the tool window is getting closed. What approach should I take such as I'll get a close button (X) in my tool window and it'll only close when I click on it; otherwise it stays open.
bl_options = {"REGISTER", "UNDO"}to the operator class definition. I think your original design is flawed, it is not supported to call an operator inside another operator's props popup. Even if it is technically possible to do so, it will lead to undefined behavior. You should maybe consider using a subpanel to do this – Gorgious May 09 '23 at 12:15