1
$\begingroup$

I used UILayout.column_flow(...) and UILayout.operator(...) to arrange buttons horizontally in a panel.

enter image description here

These buttons need to be enabled and disabled independently depending on the context. Currently, buttons with icons are differentiated by displaying CHECKBOX_DEHLT when disabled, and text buttons are on separate lines. But they're not cool. Is there any trick? I would like to avoid creating my own icons or embossing.
I've prepared a template for you to answer.

import bpy

class LayoutDemoPanel(bpy.types.Panel):
    """Creates a Panel in the scene context of the properties editor"""
    bl_label = "Layout Demo"
    bl_idname = "SCENE_PT_layout"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Test"


    def draw(self, context):

        layout = self.layout
        
        #---------------------------------------
        # Here is  question
        col = layout.column_flow(columns=2)
        col.enabled = False
        col.operator("view3d.test_operator1", text="Ico Sphere")
        col.enabled = True
        col.operator("view3d.test_operator2", text="Monkey")
        #---------------------------------------


class VIEW3D_OT_test_operator1(bpy.types.Operator):
    bl_idname = "view3d.test_operator1"
    bl_label = "Operator1"

    def execute(self, context):
        bpy.ops.mesh.primitive_ico_sphere_add()
        return {'FINISHED'}

class VIEW3D_OT_test_operator2(bpy.types.Operator):
    bl_idname = "view3d.test_operator2"
    bl_label = "Operator2"

    def execute(self, context):
        bpy.ops.mesh.primitive_monkey_add()
        return {'FINISHED'}

classes = (
    LayoutDemoPanel, 
    VIEW3D_OT_test_operator1, 
    VIEW3D_OT_test_operator2, 
        )

def register():
    for cls in classes:
        bpy.utils.register_class(cls)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)


if __name__ == "__main__":
    register()

$\endgroup$

1 Answer 1

0
$\begingroup$

you can change the enabled/disabled state of each button by using the poll method in the operator:

e.g.:

@classmethod
  def poll(cls, context):
      return context.active_object is not None

yes, this is dependent on context - but of course you could e.g. use any global variable to enable/disable it. e.g. return show_button1 == True

$\endgroup$
1
  • $\begingroup$ Using the poll(...) method has a side effect. If the mouse remains over the button after clicking it, the button will not return to its enabled state even if poll(...) return True. I might give it some more thought and then make a new question. Thanks $\endgroup$ Commented Jul 31 at 11:27

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.