1
$\begingroup$

I wrote the following code.

import bpy

bl_info = {
    "name": "test addon",
    "author": "taichi suzuki",
    "version": (1, 0),
    "blender": (2, 80, 0),
    "location": "3DView",
    "description": "test addon",
    "warning": "",
    "support": "COMMUNITY",
    "wiki_url": "",
    "tracker_url": "",
    "category": "test"
}

class TestAddon_PT_Panel(bpy.types.Panel):
    blidname = "TestAddon_PT_Panel"
    bl_label = "TestAddon Panel"
    bl_category = "TestAddon"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"

    def draw(self, context):
        layout = self.layout
        
        col = layout.column()
        col.operator(TestAddon_OT_Operator.bl_idname,text ='modal start1').number=1
        col.operator(TestAddon_OT_Operator.bl_idname,text ='modal start2').number=2

class TestAddon_OT_Operator(bpy.types.Operator):
    bl_idname = "wm.modal_testaddon_operator"
    bl_label = "Modal testaddon Operator"
    _timer = None
    number = 0
        
    def __init__(self):
        self.test = None

    def modal(self, context, event):
        if event.type in {'ESC'}:
            self.cancel(context)
            return{'CANCELLED'}

        if event.type=='TIMER':
            print(self.number)

        return{'PASS_THROUGH'}

    def execute(self, context):
        wm = context.window_manager

        if TestAddon_OT_Operator._timer is None:
            TestAddon_OT_Operator._timer = wm.event_timer_add(1.00/60.00, window=context.window)
            wm.modal_handler_add(self)
            print(self.number)
        
        return {'RUNNING_MODAL'}

    def cancel(self, context):
        wm = context.window_manager
        wm.event_timer_remove(self._timer)
        TestAddon_OT_Operator._timer = None

classes = (TestAddon_PT_Panel,TestAddon_OT_Operator)

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

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

if __name__ == "__main__":
    register()

After installing this add-on, you will see only one button labeled'modal start1'.

enter image description here

But I actually want to see the 'modal start2' button as well.

Why does this code show only one button?

$\endgroup$
2
  • 1
    $\begingroup$ Recommend you look in the system console for an error message. The number property needs to be set up as a number: bpy.props.IntProperty(...) annotation of the operator class to be used with layout.operator("foo.bar").number = 1 , hence it draws the first button and then error is thrown. Would say this is an XY Problem since the layout is not the issue. $\endgroup$
    – batFINGER
    Nov 10 '20 at 8:51
  • 1
    $\begingroup$ Thank you. That's the solution! $\endgroup$
    – taichi
    Nov 10 '20 at 9:15

Your Answer

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

Browse other questions tagged or ask your own question.