1
$\begingroup$

good day. I want to change the props into operators dialog. something like this: enter image description here

enter image description here

if I select the option A....then show for example some buttons enter image description here

but if I select option B then show for example some slider or text entry enter image description here

some code that Im test is:

class PUPA_OT_FitArmature(bpy.types.Operator):
    bl_idname = "view3d.test"
    bl_label = "test"
    bl_category = "test"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_options = {'REGISTER', 'UNDO'}

   
    options = [
            ("Select option","Select option","Select option"),
            ("Option A", "Option A", "Option A"),
            ("Option B", "Option B", "Option B")
    ]
    CollectionName:  EnumProperty( 
        items =options,     
        name="Collections"
    )

    test_checkbox : bpy.props.BoolProperty(
        name="test_checkbox ",
        description="test_checkbox", 
        default= False
    )

    slider : bpy.props.FloatProperty(
        name="slider",
        description="allow change some size", 
        default= 0.1,
        min =0.01,
        max = 2
    )

    def testM(self, context):

        print("test")



    @classmethod
    def poll(self, context):
       return context

    def invoke(self, context, event):
        return context.window_manager.invoke_props_dialog(self)
    def execute(self, context):
        D = bpy.data
        C = bpy.context

        self.testM(C)


        return {'FINISHED'}

but I can not use if else within the property definition because the register is in process... then something like this don't work enter image description here

some ideas please?

$\endgroup$
1
$\begingroup$

You can't doing logic operation when defining class attribute in python. To customize the layout of redo panel—the dialog box that usually came out when you call operator—is using draw method.

Say you want to certain property is hidden when you change another property, first you define property to toggle the change, with EnumProperty

options: EnumProperty(
        name="Options",
        default=0,
        items = [
            # identifier    name       description   number
            ('option_a', "Option A", "Active Button", 0),
            ('option_b', "Option B", "Show a Slider", 1)]
        )

items is a sequence of enum items, list of tuples. You can read the documentation here. Then you define the layout in method draw.

def draw(self, context):
    layout = self.layout
    layout.prop(self, "options")

    if self.options == "option_a":
        layout.prop(self, "prop_A")
    elif self.options == "option_b":
        layout.prop(self, "prop_B")
    else:
        ...

Make sure to draw the layout for enum property as well, unless it will not show you anything on the panel. When you define draw, blender will not automatically draw the layout for all defined properties so you have to do it manually.

Complete code:

import bpy

from bpy.props import EnumProperty, BoolProperty, FloatProperty

class PUPA_OT_FitArmature(bpy.types.Operator):
    bl_idname = "view3d.fit_armature"
    bl_label = "Fit Armature"
    bl_options = {'REGISTER', 'UNDO'}
   
    options: EnumProperty(
        name="Options",
        default=0,
        items = [
            ('option_a', "Option A", "Active Button", 0),
            ('option_b', "Option B", "Show a Slider", 1)]
        )

    test_checkbox: BoolProperty(
        name="Test Checkbox ",
        description="Test Checkbox", 
        default= False
    )

    slider: FloatProperty(
        name="Slider",
        description="Allow Change Some Size", 
        default= 0.1,
        min =0.01,
        max = 2
    )

    def draw(self, context):
        layout = self.layout

        layout.prop(self, "options")

        if self.options == "option_a":
            layout.prop(self, "test_checkbox")
        elif self.options == "option_b":
            layout.prop(self, "slider")

    def testM(self, context):
        print("test")

    def execute(self, context):
        self.testM(bpy.context)
        
        return {"FINISHED"}


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


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


if __name__ == "__main__":
    register()

Result:

Operation

$\endgroup$

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.