Calling Tools from the UI Panel in Blender

In most cases, Tools are easiest to activate by clicking on the buttons in the Toolbar (T-Panel). However, when developing an add-on with a custom UI, it may be convenient to transfer the call to the necessary tools to buttons in the user panel.

Let’s create a class for a simple UI panel:

In its draw() function, we will create one button to activate a tool, for example – the Transform tool.

Using the ToolSelectPanelHelper class, we can get the Transform tool icon identifier to set it on our button.

Now we need to bind the operator call to the button. A tool can be activated in the Blender Python API by calling the wm.tool_set_by_id() operator, passing the identifier of the desired tool in its “name” parameter.

The code might be something like this:

In general, this code will work. However, some bugs will occasionally occur, for example, buttons in the user panel will not be drawn correctly, or the tool will not be called if the panel is opened from the keyboard, etc.

To avoid these problems, we can wrap the tool call in a custom operator class.

Let’s create the simplest user operator class. In its execute() function, we will place a call of the wm.tool_set_by_id().

All that remains is to bind a call to our custom operator to the button in the panel class in the draw() function.

And don’t forget to register the panel and operator classes in the Blender Python API.

0 0 votes
Article Rating
Notify of

0 Comment
Inline Feedbacks
View all comments