Calling functions by pressing buttons in Blender custom UI

The button click is basically connected with the operator calling in the Blender user interface. However, some times actions, that need to be performed when a button is pressed, are quite simple and do not require a separate operator for them. And it makes no sense to fill a registered operators stack with a multitude of specific operators designed to perform one highly specialized function. It would be much more convenient to associate a button press with a separate function call but the Blender API allows to associate buttons only with an operator call.

To solve the problem of creating a separate operator for each button we can use the fact that the operator can be called with the input parameters.

Let’s create a simple operator class:

and define here some separate functions:

  • clear_scene – for removing all the meshes from the scene
  • add_cube – for adding a cube
  • add_sphere – for adding a sphere

Now the main feature – let’s add an input parameter of EnumProperty type for our operator.

According to this parameter value, we will call the desired function. The first item in the list is a text identifier, which will be used as a parameter value.

Modify the operator’s “execute” method to call the desired function depending on the “action” value:

Now, if the operator is called with the “CLEAR” parameter, its “clear_scene” function will be executed, with the “ADD_CUBE” parameter – the “add_cube” function will be executed and so on.

The complete operator code looks the following:

Let’s create a tab in the N-panel with our custom sub-panel to place the buttons:

In the “draw” method of our panel we defined three buttons: “Clear scene”, “Add cube” and “Add sphere”. And assigned each of them a call of our operator, but for each – with its own “action”: “CLEAR”, “ADD_CUBE “and” ADD_SPHERE”. As a result, when the button is pressed, the same operator is called, but with a different parameter to call different functions in accordance with this parameter value.

The final code:

Now we got a mechanism for calling various functions by pressing buttons without creating and registering a set of operators for each of the buttons.

5 2 votes
Article Rating
Subscribe
Notify of
guest

13 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
cscholl
cscholl
2 years ago

Nice code thanks !
Is there any way of adding a tooltip when hovering over the buttons ?
I know we can use

but I don’t know how to access the current id of the property
any thoughts ?
thanks

Last edited 2 years ago by cscholl
NikitaD
Admin
2 years ago
Reply to  cscholl

You have the properties parameter to do this.

Steve
Steve
2 years ago

Sorry for the trivial question but I’m new to object oriented programming. How can I call TEST_OT_test_op from inside another function? If for example I would like to execute the ‘CLEAR’ option without any button press or other UI action from another function?

NikitaD
Admin
2 years ago
Reply to  Steve

All registered operators could be called through the bpy.ops
For example for the operator from this article with the “CLEAR” parameter:
bpy.ops.test.test_op(action=’CLEAR’)

Steve
Steve
2 years ago
Reply to  Nikita

Many thanks! This helps me a lot to understand the combination of Python and Blender.

Rombout Versluijs
Rombout Versluijs
3 years ago

Thanks for showing how to call static method. Isnt there an “easier” way as well. I somehow get the feeling using that enum to call a function is not needed. I tried a couple methods, but cant get it to run though

hayden
hayden
3 years ago
Reply to  Nikita

but you cannot create a costume function, this method is so poor that it rely on fixed functions of the blender. For example, you cannot add your own function, it has to be within (clear add cube add sphere etc). lets say i want to make a function called (clint_eastwood). ERROR

hayden
hayden
3 years ago
Reply to  Nikita

the enum approach is poor, because you cannot work on your own dictionary. Direct approach always better, check this out

hayden
hayden
3 years ago
Reply to  Nikita

but arn’t you limited by the functions that blender provide? like if i want to add a costume enum field value , not (add sphere or add cube)<- these are simple. Lets assume i want to add another field to the enum randomly to do complicated actions. Like adding modifier and change that modifier then execute the modifier on the selected object with one button. always generate error. then your example is only fit for 1 task, creating objects.