Creating a custom UI window in Blender using PySide

The Blender Python API provides add-on developers with quite a wide range of options for designing a user interface. However, sometimes the built-in capabilities of the Blender API may not be enough. We can create a window with a UI that is completely independent of the built-in API by connecting and using the PySide module.

PySide is not included in the default Blender distribution, so first we need to install the PySide module to Blender.

After successfully installing PySide, let’s create a separate UI window with a button, for example, which will add a default UVSpehere mesh to the scene when pressed.

Open a Text Editor area in Blender, and define a class responsible for creating and operating such a window.

We have defined the MainWindow class, and the common __init__() function inside it. This function will be called first when creating our UI window.

Let’s fill the __init__() function with some additional code:

Here we set the “always on top” display mode for our window and set the window width to 300 pixels.

Add a widget to our window form and define a layout for it, on which we can then place various interface elements (buttons, etc.).

Now define the button for adding the mesh to the scene.

Here we create a button with the label “Add UVSphere”, set its dimensions to 280 by 50 pixels and place it on the previously created layout.

On the third line we assign a handler to the button press and connect it to the call to the button_pressed() function. We will define this function later a bit.

Let’s also add an example of feedback from a Blender scene to our window – we’ll make it so that the name of the current active scene object is always displayed in the title of our window.

The easiest way to organize feedback is to set a timer.

Here we define a timer that will fire every 100 milliseconds and call the update() function.

The full code of our __init__() function:

Now let’s define the function that will be called when the button is pressed.

In the first line, we define a simple counter that we will use to shift each new object added to the scene along the X axis.

Next, we call the primitive_uv_sphere_add() operator from the Blender API, which adds a UV sphere to the scene. In the location parameter, we set the offset to 2 units along the X axis, based on the current value of the counter.

Since the operator is called from an external window that is not associated with the active 3D viewport, it must be called with context override using the temp_override(), passing a pointer to the active 3D viewport window in the parameters.

In the last line, we simply increase the mesh shift counter for the next pressing the button by the user.

We still have to define the update() function, which is called according to the timer and copies the name of the active scene object to our window title.

To get the name of the active object from the bpy.context.active_object.name, we still need to use temp_override() to point to the 3D viewport context.

The last function of our class is:

It will be responsible for handling the window closing – removing the timer created at the beginning, and clearing the event stack.

After we have fully defined the class for our custom window, we can create an instance of this window and display it on the screen.

The window instance is created using a singleton, so the binding to the Blender process only needs to be specified the first time the window is created.

Now the window will be displayed on the screen, and when we click the “Add UVSphere” button, a UVSphere object will be added to the Blender scene, shifting each subsequent sphere 2 units to the right.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments