0
$\begingroup$

I would like my operator, once created 2 different buttons in the panel, When I fly over the button with the cursor them they give 2 different tips, based on the assigned property. I don't really know how to do it, i place a small example to better understand what I intend to do.

   class MYOPS_OT_Operator(bpy.types.Operator):
        #Crazy Example, obviously it cannot work like this:
        """It will do something""" if self.options = 'OPT_ONE' else """It will do something different"""


        bl_idname = "my.ops"
        bl_label = "My ops"
        bl_options = {'INTERNAL','UNDO'}

        options: StringProperty()

        def execute(self,context):

            if self.options = 'OPT_ONE':
                print('I did something')
            if self.options = 'OPT_TWO':
                print('I have done something different')

            return{'FINISHED'}


    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 = 'PROPERTIES'
        bl_region_type = 'WINDOW'
        bl_context = "scene"

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

            layout.operator("my.ops",text='Do something').options = 'OPT_ONE'
            layout.operator("my.ops",text='Do something different').options = 'OPT_TWO'
$\endgroup$
2
$\begingroup$

Operator description method

Combining answers from Changing tooltip for menu entries which use the same operator

Since 2.81 An operator now has a description(cls, context, event) classmethod. The properties of the operator are available thru the event object. See print methods in script below to expand members available via context and event passed to description

The string returned from this method is used as the operator tooltip. Remember this can be multiline too.

Secondly if an enumproperty is used for the options then the multi button layout is taken care of. Unfortunately the description is seemingly unused. (No matter. Easy enough to use in description method)

import bpy
from  bpy.props import EnumProperty

class MY_OT_ops(bpy.types.Operator):
    ''' Enum Example '''  
    bl_idname = "my.ops"
    bl_label = "My ops"
    bl_options = {'REGISTER'}

    zzz : bpy.props.IntProperty()
    def items(self, context):
        items = (
            ('OPT_ONE', "Option One", "I am ONE"),
            ('OPT_TWO', "Option Two", "I am TWO"),
            )
        return items

    options: EnumProperty(
        name="Options",
        items=items,
        )

    @classmethod
    def description(cls, context, event):
        '''
        print("event")
        for p in dir(event):
            print(p, getattr(event, p))
        print("context")    
        c = context.copy()
        for k, v in c.items():
            print(k, v)
        '''
        opt = getattr(event, "options", "Something")
        return f"Choose {opt}"

    def execute(self, context):

        if self.options == 'OPT_ONE':
            print('I did something')
        if self.options == 'OPT_TWO':
            print('I have done something different')

        return{'FINISHED'}


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 = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

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


        layout.operator_menu_enum("my.ops", "options")
        col = layout.column()
        col.operator_enum("my.ops", "options")
        layout.popover("SCENE_PT_layout", text="Pop Over")

classes = (MY_OT_ops, LayoutDemoPanel)

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

if __name__ == "__main__": 
    register()
| improve this answer | |
$\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.