1
$\begingroup$

I tried to answer a question on blenderartists from what I already know, and then it was pointed out to me that I should be able to use the property directly in the panel menu instead of creating the operator for it outside the menu like I did 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()

Can anyone give me a single line example of how the property should be accessed in the layout so that it results in the bool? I already tried searching here and elsewhere and haven't found anything that works.

$\endgroup$
1
$\begingroup$

Instead of adding a layout.operator you would add a layout.prop, which would look like this:

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

The first parameter in the parentheses is where the property is stored and the second is the name of the property you wish to access.

This results in this menu, with a property that can be toggled on and off:

enter image description here

$\endgroup$
  • $\begingroup$ Thank you, your explanation is worth as much as the solution itself. $\endgroup$ – Craig D Jones Jan 23 at 2:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.