'blocking' Yes/No Dialog box in Addon

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
AlphaNow
Posts: 23
Joined: Mon Sep 30, 2013 7:13 pm

'blocking' Yes/No Dialog box in Addon

Post by AlphaNow » Fri Oct 11, 2013 11:25 pm

I am using Blender 2.68a.

I have managed to use some code I found written for 2.5x that works somewhat to display a window with a message and an OK button, but it seems less than obvious how I might setup an event.

I've got an operator which I want to, before it begins it's real work, show a message warning the user that it may take the script several minutes to complete its' work, but to be patient even though the UI seems unresponsive. I want the user to be able to say "OK" or "Cancel".

OK->addon script continues to run and returns {'FINISHED'}
Cancel->addon script aborts and returns {'CANCELED'}

Code: Select all

class MessageOperator(bpy.types.Operator):
	bl_idname = "dialog.message"
	bl_label = "Message"
	type = StringProperty()
	message = StringProperty()
	
	def execute(self, context):
		self.report({'INFO'}, self.message)
		print(self.message)
		return {'FINISHED'}
		
	def invoke(self, context, event):
		wm = context.window_manager
		return wm.invoke_popup(self, width=800, height=300)
		
	def draw(self, context):
		self.layout.label("Preparing to export...")
		row=self.layout.split(0.25)
		row.prop(self, "message")
		row = self.layout.split(0.80)
		row.label("")
		row.operator("dialog.ok")
		
class OkOperator(bpy.types.Operator):
	bl_idname="dialog.ok"
	bl_label="OK"
	def execute(self, context):
		return{'FINISHED'}

class CancelOperator(bpy.types.Operator):
	bl_idname="dialog.cancel"
	bl_label="Cancel"
	def execute(self,context):
		return{'CANCELED'}
		
bpy.utils.register_class(CancelOperator)
bpy.utils.register_class(OkOperator)
bpy.utils.register_class(MessageOperator)
How can I fire events?

How can I make the dialog block until a button is pressed?

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX » Sat Oct 12, 2013 4:10 am

The Blender user interface is mostly non-modal and non-overlapping. Non-modal means you do not have popup windows that block you from doing other things while the window is open. If the interface is non-modal it means you change some settings, and see their effect or test them out without having to confirm and reopen the popup window each time. Non-overlapping means you generally do not have overlapping windows. You have editors side-by-side and can edit in both without one blocking the other, and you spend less time moving things out of the way.
http://wiki.blender.org/index.php/User:Brecht/UI

In other words: it's not designed to do such a thing. You can use invoke_props_dialog however, which is pretty close. If user confirms, execute() is called.
I'm sitting, waiting, wishing, building Blender in superstition...

AlphaNow
Posts: 23
Joined: Mon Sep 30, 2013 7:13 pm

Post by AlphaNow » Mon Oct 14, 2013 9:32 pm

CoDEmanX wrote: http://wiki.blender.org/index.php/User:Brecht/UI

In other words: it's not designed to do such a thing. You can use invoke_props_dialog however, which is pretty close. If user confirms, execute() is called.
I understand that Blender isn't designed to do such things, and I appreciate the reasons why. That given, I'll still want to try and achieve this functionality for the sake of my users.

So if I was to, in the execute function of my addon(the one you helped me work on in that other thread) , define it as something like:

Code: Select all

	def execute(self, context):
		if(bpy.ops.invoke_props_dialog(<some_arguments_here>)== {'FINISHED'}):
			if main(bpy.context):
				return {'FINISHED'}
		return {'CANCELLED'}
it would invoke a property dialog(that i'll have to sort out how to call) and then when the user clicked the OK button it would return {'FINISHED'} and move on? And assumably i could design the properties dialog it's going to open, so it would just have a relevant message?

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX » Mon Oct 14, 2013 10:37 pm

hm no, look here:

Code: Select all

import bpy

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"
    bl_options = {'REGISTER', 'UNDO'}
    
    prop = bpy.props.StringProperty(name="Material", maxlen=63)
    mats = bpy.props.CollectionProperty(type=bpy.types.PropertyGroup)

    @classmethod
    def poll(cls, context):
        return (context.active_object is not None and
                context.active_object.type == 'MESH')    

    def execute(self, context):
        self.report({'INFO'}, self.prop)
        print(self.prop)
        return {'FINISHED'}
    
    def draw(self, context):
        layout = self.layout
        layout.prop_search(self, "prop", self, "mats", icon='MATERIAL')
        layout.separator()
        col = layout.column(align=True)
        col.label("Pick a material above, or click outside")
        col.label("of this dialog box to cancel.")
        
    def invoke(self, context, event):
        self.mats.clear()
        for i in range(1, 7):
            self.mats.add().name = "Material %i" % i
        return context.window_manager.invoke_props_dialog(self)


def register():
    bpy.utils.register_class(SimpleOperator)


def unregister():
    bpy.utils.unregister_class(SimpleOperator)


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.object.simple_operator('INVOKE_DEFAULT')
I'm sitting, waiting, wishing, building Blender in superstition...

AlphaNow
Posts: 23
Joined: Mon Sep 30, 2013 7:13 pm

Post by AlphaNow » Thu Oct 17, 2013 7:59 pm

So the answer is, I can duplicate the functionality i'm after by writing an operator that draws a dialog box, with a button on it which, if clicked, executes my "workhorse" operator, and if not, just cancels.

I just have the menu item execute the dialog box, and the user either can continue by using the button in the box(having been giving a warning that the UI will be blocked until the script finishes) or cancel.

I found this superior to using the window_manager.invoke_confirm(self, event).

rasamohamed
Posts: 8
Joined: Sat Oct 05, 2013 11:44 am
Location: INDIA

Post by rasamohamed » Wed Oct 23, 2013 2:39 pm

I have to create dialog box this should display while closing the Blender. This dialog box have to ask from user that Save Quit Cancel.

I have created pop window while closing Blender but I didn't create two button horizontally
How it's possible. I'm trying from last one week but no improvement.

Where i have to edit and how the coding should be.....?????
Any Help....................?
Regards
Rasa [India]
http://projectvismaya.com/

AlphaNow
Posts: 23
Joined: Mon Sep 30, 2013 7:13 pm

Post by AlphaNow » Mon Nov 04, 2013 5:50 pm

rasamohamed wrote:I have to create dialog box this should display while closing the Blender. This dialog box have to ask from user that Save Quit Cancel.

I have created pop window while closing Blender but I didn't create two button horizontally
How it's possible. I'm trying from last one week but no improvement.

Where i have to edit and how the coding should be.....?????
Any Help....................?
While this does talk about quite a number of things, I would start by looking at the "Panel layout and several arguments" example here, which they've also provided a photo of. It is for blender 2.5 but I believe most of the layout syntax is the same.

http://wiki.blender.org/index.php/Dev:2 ... /Interface

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests