0

Hello I want my button to be disabled when a certain variable from a enum list property has been selected and enabled again when some other variable has been selected. How can I achieve this?

sample code:

def draw(self, context):
    layout = self.layout
    scene = context.scene
    addonPrefs = scene.addon
layout.prop(addonPrefs, "variables")
layout.operator(addonExec.bl_idname, text="SAMPLE", icon="RENDER_RESULT") 

#Results I want -> if addonPrefs.variables == "foo": disable operator button; else: enable operator button 

KripC2160
  • 111
  • 6
  • i think you need to do that in the poll function https://blender.stackexchange.com/questions/19416/what-do-operator-methods-do-poll-invoke-execute-draw-modal – Harry McKenzie Jul 09 '22 at 14:49
  • Do you wish to not display the button when it is disabled or do you wish to display it but show that it is disabled? In either case I would use an if/then/else with the test returning true if you want enabled. Don't have an else clause if you want to not display. Have an else clause that draws a label instead if you want to show disabled. AFAICT UILayout.operator doesn't have a 'display disabled' option. – Marty Fouts Jul 09 '22 at 15:48

1 Answers1

1

You would do it like this:

def draw(self, context):
    layout = self.layout
    scene = context.scene
    addonPrefs = scene.addon
layout.prop(addonPrefs, "variables")

op_row = layout.row()
if addonPrefs.my_attr == "foo":
    op_row.enabled = False
op_row.operator(addonExec.bl_idname, text="SAMPLE", icon="RENDER_RESULT") 

You create a separate sub-layout and then set that layout's "enabled" attribute depending on the props condition.

Jakemoyo
  • 4,375
  • 10
  • 20