4

I would like to add a button and I looked at many tutorials (examples) but there are no good explanations. After many tirals, I got confused. I'm sure that the mistake is in the line that starts with row.operator because the button doesn't appear and I don't know how to link it to the function called pressedButton.

class Single_PT_Button(bpy.types.Panel):
    bl_label = "amazing"
    bl_idname = "Single_PT_Butto.n"
    bl_space_type = 'SEQUENCE_EDITOR'
    bl_region_type = 'UI'
    bl_category = 'Strip'
def draw(self, context):
    layout = self.layout

    layout.label(text= "Press the button")
    row = layout.row()        # Create a simple row.
    row.operator("Single_PT_Butto.n", text= "Add Text", icon= 'OUTLINER_OB_FONT')


def pressedButton ():
    print("Button is pressed")

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

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

if name == "main": register()

Result:

enter image description here

Also, I would like to ask about one more quick info, is it possible to add it inside/under an already existing Panel like Effect Strip?

user2824371
  • 796
  • 1
  • 7
  • 27

2 Answers2

3

In the line:

row.operator("Single_PT_Butto.n", text= "Add Text", icon= 'OUTLINER_OB_FONT')

You assigned the panel's bl_idname in the operator method's first argument which is incorrect. This argument should be used to assign whatever operator you want to execute when this button is clicked. You can assign either your operators or any already existing blender operator.

For example this line will assign blender render operator bpy.ops.render.render() to the button:

row.operator("render.render", text= "Add Text", icon= 'OUTLINER_OB_FONT')

enter image description here

Ahmed Ali
  • 563
  • 6
  • 21
  • Well, it seems that the argument is text. What if I want to assign the function? How can I write pressedButton() in the argument? In other words, I still don't know how to link the function to the button. – user2824371 Jul 04 '21 at 16:07
  • I recommend reading this [documentation][1] page it will guide you through writing your first operator, but as a simple rule for now, whatever will you write in the operator's execute() function will be called when you call/assign this operator. [1]: https://docs.blender.org/api/current/bpy.types.Operator.html – Ahmed Ali Jul 04 '21 at 18:16
3

You can find a simple example in Scriping -> Templates -> UI Panel simple:

   import bpy

class HelloWorldPanel(bpy.types.Panel): """Creates a Panel in the Object properties window""" bl_label = "Hello World Panel" bl_idname = "OBJECT_PT_hello" bl_space_type = 'PROPERTIES' bl_region_type = 'WINDOW' bl_context = "object"

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

    obj = context.object

    row = layout.row()
    row.label(text="Hello world!", icon='WORLD_DATA')

    row = layout.row()
    row.label(text="Active object is: " + obj.name)
    row = layout.row()
    row.prop(obj, "name")

    row = layout.row()
    row.operator("object.text_add")


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

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

if name == "main": register()

then you get in the "object properties" this:

enter image description here

and here is an example how you can call your own custom function:

import bpy

class HelloWorldOperator(bpy.types.Operator): bl_idname = "wm.hello_world" bl_label = "Minimal Operator"

def execute(self, context):
    print("Hi World")
    return {'FINISHED'}


bpy.utils.register_class(HelloWorldOperator)

test call to the newly defined operator

bpy.ops.wm.hello_world()

class HelloWorldPanel(bpy.types.Panel): """Creates a Panel in the Object properties window""" bl_label = "Hello World Panel" bl_idname = "OBJECT_PT_hello" bl_space_type = 'PROPERTIES' bl_region_type = 'WINDOW' bl_context = "object"

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

    obj = context.object

    row = layout.row()
    row.label(text="Hello world!", icon='WORLD_DATA')

    row = layout.row()
    row.label(text="Active object is: " + obj.name)
    row = layout.row()
    row.prop(obj, "name")

    row = layout.row()
    row.operator("wm.hello_world")


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

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

if name == "main": register()

This example will print "Hi World" in the console, if you press the button.

Chris
  • 59,454
  • 6
  • 30
  • 84