# Calling a function from a button

**URL:** <https://blenderartists.org/t/calling-a-function-from-a-button/616752>\
**Category:** Python Support\
**Created:** [July 22, 2014, 4:50pm UTC](https://blenderartists.org/t/calling-a-function-from-a-button/616752 "2014-07-22T16:50:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![gordon13](https://blenderartists.org/user_avatar/blenderartists.org/gordon13/32/17738_2.png) [@gordon13](https://blenderartists.org/u/gordon13)\
**Post date:** [July 22, 2014, 4:50pm UTC](https://blenderartists.org/t/calling-a-function-from-a-button/616752/1 "2014-07-22T16:50:30Z")

</div>

Hello, I am new to Python in Blender ( and python in general). I’m sure this will be very basic. I am trying to make a panel with a button which calls a function. I am having a hard time finding examples of that. I have seen examples of buttons use to spawn things or call existing blender functions but none showing the use of custom functions.

Hope someone can help.  
Thanks

---

<div class="post-metadata">

**Author:** ![CoDEmanX](https://blenderartists.org/letter_avatar_proxy/v4/letter/c/77aa72/32.png) [@CoDEmanX](https://blenderartists.org/u/CoDEmanX)\
**Post date:** [July 22, 2014, 5:40pm UTC](https://blenderartists.org/t/calling-a-function-from-a-button/616752/2 "2014-07-22T17:40:37Z")

</div>

Have you checked the Templates in the Text Editor?

And read here: [http://wiki.blender.org/index.php/Dev:2.5/Py/Scripts/Cookbook/Code\_snippets/Interface](http://wiki.blender.org/index.php/Dev:2.5/Py/Scripts/Cookbook/Code_snippets/Interface)

This prepends a custom operator to the 3D View header:

```auto
import bpy

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        self.report({'INFO'}, "Button clicked!")
        return {'FINISHED'}

def draw_func(self, context):
    layout = self.layout
    layout.operator("object.simple_operator")
    

def register():
    bpy.utils.register_class(SimpleOperator)
    bpy.types.VIEW3D_HT_header.prepend(draw_func)

def unregister():
    bpy.utils.unregister_class(SimpleOperator)
    bpy.types.VIEW3D_HT_header.remove(draw_func)

if __name__ == " __main__":
    register()

```

Run script, hover with mouse over 3D View header, then click the button!

---

<div class="post-metadata">

**Author:** ![gordon13](https://blenderartists.org/user_avatar/blenderartists.org/gordon13/32/17738_2.png) [@gordon13](https://blenderartists.org/u/gordon13)\
**Post date:** [July 23, 2014, 12:43pm UTC](https://blenderartists.org/t/calling-a-function-from-a-button/616752/3 "2014-07-23T12:43:48Z")

</div>

Hey, thanks for the example. I tried modifying it to call a custom class however I cant get it to work. I modified the message in execute and it compiles fine but only seems to display the default message when the button is clicked. Also the caption on the button is the default one. I know I must be doing something wrong…

Here is my code:

```auto

bl_info = {
    "name": "Exploded Bake",
    "category": "Render",
}

import bpy

class ExplodedBake(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Exploded Bake"
    bl_idname = "OBJECT_PT_exploded"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

    class SimpleOperator(bpy.types.Operator):
        """Tooltip"""
        bl_idname = "object.simple_operator"
        bl_label = "Button text"

        @classmethod
        def execute(self, context):
            self.report({'INFO'}, "Hello world!")
            return {'FINISHED'}
 
    
    def draw(self, context):
        layout = self.layout
        obj = context.object
        row = layout.row()
        row.label(text="Exploded Bake", 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.simple_operator")

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

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

if __name__ == " __main__":
    register()

```

Edit, also I have an issue with the code you provided earlier, How do you remove the buttons that it added to the 3d view? I’ve got like, 50 of the same button on the header bar of my 3D view and I dont know how to remove them 😛 Please help

---

<div class="post-metadata">

**Author:** ![CoDEmanX](https://blenderartists.org/letter_avatar_proxy/v4/letter/c/77aa72/32.png) [@CoDEmanX](https://blenderartists.org/u/CoDEmanX)\
**Post date:** [July 23, 2014, 3:39pm UTC](https://blenderartists.org/t/calling-a-function-from-a-button/616752/4 "2014-07-23T15:39:00Z")

</div>

You placed the operator inside the panel class, that’s not allowed.

> How do you remove the buttons that it added to the 3d view?

F8

---

<div class="post-metadata">

**Author:** ![gordon13](https://blenderartists.org/user_avatar/blenderartists.org/gordon13/32/17738_2.png) [@gordon13](https://blenderartists.org/u/gordon13)\
**Post date:** [July 23, 2014, 4:31pm UTC](https://blenderartists.org/t/calling-a-function-from-a-button/616752/5 "2014-07-23T16:31:57Z")

</div>

Been playing with it for the past few hours, looking at examples etc. Can’t find where I’m going wrong. I’m getting an error now and I’m totally confused:  
search for unknown operator “BUTTON\_OT\_explode”, “button.explode” rna\_uiItem0: operator missing srna “button.explode”

No idea where the BUTTON\_OT\_explode came from!

Heres my latest code:

```auto

bl_info = {
    "name": "Exploded Bake",
    "category": "Render",
}

import bpy

class ExplodedBake(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Exploded Bake"
    bl_idname = "exploded_bake"
    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.prop(obj, "name")
        row = layout.row()
        row.operator("button.explode")
        
class buttonExplode(bpy.types.Operator):
    bl_idname = "button.explode"
    bl_label = "Button text"

    def execute(self, context):
        #self.report({'INFO'}, "Hello world!")
        print("hello")
        return {'FINISHED'}
        

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

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

if __name__ == " __main__":
    register()bl_info = {
    "name": "Exploded Bake",
    "category": "Render",
}

import bpy

class ExplodedBake(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Exploded Bake"
    bl_idname = "exploded_bake"
    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.prop(obj, "name")
        row = layout.row()
        row.operator("button.explode")
        
class buttonExplode(bpy.types.Operator):
    bl_idname = "button.explode"
    bl_label = "Button text"

    def execute(self, context):
        #self.report({'INFO'}, "Hello world!")
        print("hello")
        return {'FINISHED'}
        

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

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

if __name__ == " __main__":
    register()

```

---

<div class="post-metadata">

**Author:** ![Linusy](https://blenderartists.org/letter_avatar_proxy/v4/letter/l/7cd45c/32.png) [@Linusy](https://blenderartists.org/u/Linusy)\
**Post date:** [July 24, 2014, 12:44am UTC](https://blenderartists.org/t/calling-a-function-from-a-button/616752/6 "2014-07-24T00:44:21Z")

</div>

You need to register the operator as well.

```auto

def register():
    bpy.utils.register_class(ExplodedBake)
    bpy.utils.register_class(buttonExplode)

```

etc

Also look into naming conventions and I what you paste should be from two files.  
Only one register function per file.  
So with some cleanup you should be good to go.

---

<div class="post-metadata">

**Author:** ![CoDEmanX](https://blenderartists.org/letter_avatar_proxy/v4/letter/c/77aa72/32.png) [@CoDEmanX](https://blenderartists.org/u/CoDEmanX)\
**Post date:** [July 24, 2014, 10:06am UTC](https://blenderartists.org/t/calling-a-function-from-a-button/616752/7 "2014-07-24T10:06:17Z")

</div>

Nicer formatted + 3 inline comments

```auto
bl_info = {
    "name": "Exploded Bake",
    "category": "Render",
}

import bpy

class ExplodedBake(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Exploded Bake"
    bl_idname = "OBJECT_PT_exploded_bake" # follow Blender convention for id names
    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.prop(obj, "name")
        
        row = layout.row()
        row.operator("button.explode")

class buttonExplode(bpy.types.Operator):
    bl_idname = "button.explode" # translates to C-name BUTTON_OT_explode
    bl_label = "Button text"

    def execute(self, context):
        #self.report({'INFO'}, "Hello world!")
        print("hello")
        return {'FINISHED'}
    
    
# (un-)register entire module, so you don't need to add every class here...
def register():
    bpy.utils.register_module( __name__ )

def unregister():
    bpy.utils.unregister_module( __name__ )

if __name__ == " __main__":
    register()

```

---

<div class="post-metadata">

**Author:** ![gordon13](https://blenderartists.org/user_avatar/blenderartists.org/gordon13/32/17738_2.png) [@gordon13](https://blenderartists.org/u/gordon13)\
**Post date:** [July 24, 2014, 4:22pm UTC](https://blenderartists.org/t/calling-a-function-from-a-button/616752/8 "2014-07-24T16:22:36Z")

</div>

Ooooh totally awesome! Thanks guys. I think I was confused with the whole registering thing. It makes sense now. I registered the class of the new button I made and it now works as expected.

And thanks CoDEmanx for the tip about registering the whole module. Could you explain what you mean by “translates to C-name BUTTON\_OT\_explode” ?

---

<div class="post-metadata">

**Author:** ![CoDEmanX](https://blenderartists.org/letter_avatar_proxy/v4/letter/c/77aa72/32.png) [@CoDEmanX](https://blenderartists.org/u/CoDEmanX)\
**Post date:** [July 24, 2014, 5:26pm UTC](https://blenderartists.org/t/calling-a-function-from-a-button/616752/9 "2014-07-24T17:26:37Z")

</div>

Operator notation in C code is CATEGORY\_OT\_name\_in\_snake\_case (OT stands for operator type).

In scripts, a more pythonic naming style is used: category.name\_in\_snake\_case

However, the python names are converted to the C-notation, button.explode ends up as bpy.types.BUTTON\_OT\_explode

---

<div class="post-metadata">

**Author:** ![gordon13](https://blenderartists.org/user_avatar/blenderartists.org/gordon13/32/17738_2.png) [@gordon13](https://blenderartists.org/u/gordon13)\
**Post date:** [July 25, 2014, 4:33am UTC](https://blenderartists.org/t/calling-a-function-from-a-button/616752/10 "2014-07-25T04:33:49Z")

</div>

I see! Thanks for the explanation
