How to put a tool setting into a popup menu?

How would I put a tool setting into a popup menu?

I have been following a CG Cookie tut on YouTube here which Wes Burke refers to here.

Here’s my code:

import bpy

class CustomMenu(bpy.types.Menu):
    bl_label = "Custom Menu"
    bl_idname = "view3D.custom_menu"
    
    
    def draw(self, context):
        
        self.layout("ToolSettings.use_mesh_automerge")
        # self.layout.operator("mesh.primitive_cube_add")
        
def register():
    bpy.utils.register_class(CustomMenu)
    bpy.ops.wm.call_menu(name=CustomMenu.bl_idname)
    
def unregister():
    bpy.utils.unregister_class(CustomMenu)
    
if __name__ == "__main__":
    register()

What are you specifically wanting in the pop up menu? It already has an example there of an add object operator.

The operator you see commented out was part of the tutorial.

I would like to create a popup and put more than operators in it, placing modelling tools and options I use regularly like -

Occlude Mesh
Automerge
Snap, Align Pivot options etc

I’ll look at one of my other add-on attempts that worked and see if I can make a simple example for the automerge. It is a True or False bool, so it isn’t a simple one line operator insert here.

import bpy


class AutoMergeBool(bpy.types.Operator):
    """Automerge Toggle"""
    bl_idname = "mesh.automerge_toggle"
    bl_label = "Automerge Toggle Operator"
    
    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        #main(context)
        merge = bpy.context.scene.tool_settings

        
        if merge.use_mesh_automerge != True:
            merge.use_mesh_automerge = True
        else:
            merge.use_mesh_automerge = False
        return {'FINISHED'}
    
class CustomMenu(bpy.types.Menu):
    bl_label = "Custom Menu"
    bl_idname = "view3D.custom_menu"
    
    
    def draw(self, context):
        
        self.layout.operator("mesh.automerge_toggle")
        self.layout.operator("mesh.primitive_cube_add")
        
def register():
    bpy.utils.register_class(CustomMenu)
    bpy.utils.register_class(AutoMergeBool)
    bpy.ops.wm.call_menu(name=CustomMenu.bl_idname)
    
def unregister():
    bpy.utils.unregister_class(CustomMenu)
    bpy.utils.unregister_class(AutoMergeBool)
    
if __name__ == "__main__":
    register()

What was your reason for using an operator to set the property? You should be able to put most properties directly in a menu. Boolean properties default display with a nice checkbox when placed in menus.

If you don’t want the menu to close upon changing properties, then you will have to set a bit flag on the uiBlock of the menu that prevents it from closing (UI_BLOCK_KEEP_OPEN = 1 << 8), they don’t yet give access via python though, so you would have to recreate those structs from the source code with the ctypes module (you would need uiLayout, uiLayoutRoot, uiBlock, and any other embedded intermediate structs).

Once you recreate those structs it can be as simple as:

layout = uiLayout.from_address(self.layout.as_pointer())
block = layout.root.block
block.flag |= UI_BLOCK_KEEP_OPEN
block.dt = UI_EMBOSS # Most props look better this way

If you are using Blender 2.8 you can instead use a popover, which can also be spawned via the window manager. I believe the popovers have that flag set, or at least behave like they do.

1 Like

Because I am self-taught, and found the way on my own using the included templates and watching/reading python tutorials… so what you say there makes some sense but is beyond my experience.

And if using 2.8, couldn’t the OP just use the quick prefs?

You can use this operators to set properties:
bpy.ops.wm.context_
collection_boolean_set(
cycle_array(
cycle_enum(
cycle_int(
menu_enum(
modal_mouse(
pie_enum(
scale_float(
scale_int(
set_boolean(
set_enum(
set_float(
set_id(
set_int(
set_string(
set_value(
toggle(
toggle_enum(

1 Like

Thanks for your reply Craig.

I get what you’ve done - the ToolSettings (tool_settings) always threw an error that it was not a property (attribute?) of ‘layout’ so I could never get it in there. I thought of ways to store this a variable but was a bit lost.

This works. But I’d like to know how tool properties can be put in popup dialogues as suggested by @SynaGl0w


I’m also self taught. Over the last 8 months I’ve used Codecademy and Eric Matthes ‘Python Crash Course’ book to get started in Python.

I’ve watched a couple of Blender Python tutorials to understand how to access and use the BPY module in Blender but tutorials stopped just short of this, and I’m out of my depth in the API reference.

1 Like

How do you do this?

Like Craig, maybe even more so, I am still just learning Python and Blender Python.

Here is a screen shot of a popup dialogue I made with @roaoao’s PIe Menu Editor

The popup here works but not if the Blender application window is wider than 2090px (on my iMac 27" retina) so I have stopped using it.

(The two versions of the automerge buttons were just an experiment with and without button icons)

1 Like

You would just use the prop method of the layout instead of the operator method. For example:

self.layout.prop(context.scene.tool_settings, "use_mesh_automerge")

2.79: https://docs.blender.org/api/current/bpy.types.UILayout.html#bpy.types.UILayout.prop
2.80: https://docs.blender.org/api/blender2.8/bpy.types.UILayout.html#bpy.types.UILayout.prop

1 Like

Thank you for that, I had tried but must have had a typo because I couldn’t get it to work earlier and figured I was on the wrong track.

Wow. Brilliant. Thanks

I tried the self.layout.prop() with variations of the ‘context’ line, but definitely not using the string bit in quotes.

Look forward to trying this.

Thanks again

they all generate errors. i bet there are things after the opening parenthesis ^^

Yes, you are right. In that post i just show possible commands, but not complete examples. You need to know python little bit to use them.
Here is good start for trying to use them:
Type one of them in console area and hit Ctrl+Space. You`ll get help.

blender_console_help

Example of usage as hotkey.

owww this is quite interresting !
reminds me of nux console and man pages ^^

but is quite useless when ( just like me ) you don’t know the structure of blender API :confused:

i confess i’m slowly sinking in desperation :frowning: