Creating radio buttons in the Blender add-ons interface

State switches so-called “radio buttons” are used in the case to limit the choice by one value from several available ones. There are a lot of such buttons in the Blender interface, for example, switching between RGB and BW rendering modes or setting the texture mapping mode. Such buttons can be created in the Blender add-ons interface too.

Let’s create our own radio button switcher.

First, we need to define available lists of values.

For example, we want to rotate the selected objects to 15, 30, 60, 90 and 120 degrees clockwise and counterclockwise according to the user choice of the angle and rotation direction through the radio button sets.

Each radio buttons set should be defined as an enumerable property “EnumProperty” and filled with the desired data.

For angular values:

Here we defined the “EnumProperty” enumeration property named “angles”, each element of which is a tuple with the following format:

(unique identifier, property name, property description, icon identifier, number)

The name and the icon (by identifier) will be displayed on buttons and the description in the pop-up when the mouse cursor hovers over it.

The identifier, title, and description in our case are the same for each angle. Leave the icon field empty not to display it.

Set the “default” parameter to the identifier of the default selected button. In our case – “15” degrees.

Create another enumerable property for selecting the rotation direction:

There are two elements for clockwise and counterclockwise rotation. An empty name means that the button will not display text, but only the icon according to the specified icon identifier.

To make created properties available to the Blender API, wrap them into a class inherits from the “PropertyGroup”.

And register this class in the Blender API through the “register” function. Specify a variable “interface_vars” that will provide access to the registered data.

Clean up through the “unregister” function.

Create a simple operator to rotate the selected objects.

The rotation value is obtained from the previously registered “interface_vars” variable through the “context.window_manager.interface_vars.angles” and the rotation direction through the “context.window_manager.interface_vars.direction”. The enumerate property returns the identifier of the currently selected item. Rotation is performed around the Z-axis.

Now let’s create in the”3D_View” window in the T-panel a subpanel to draw our interface.

The button sets are displayed on the panel through the “context.window_manager.interface_vars” with the specific properties (“angles” and “direction”). The “expand” parameter setted to “True” specifies that the buttons should be displayed as radio buttons, not a drop-down list.

All created classes must also be registered in the Blender API.

Full code list:

After the script execution, selected objects can be rotated according to the conditions specified in radio buttons set.

0 0 votes
Article Rating
Subscribe
Notify of
guest

3 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Steve Kozma
Steve Kozma
2 years ago

Hi! Is this method still working? (After the changes from 2.8 onward)
I copy/pasted it in to Blender 3.1.2 and the Radio Buttons do not appear.
This is the Error Message:
rna_uiItemR: property not found: InterfaceVars.angles
rna_uiItemR: property not found: InterfaceVars.direction

BR Steve

PS: I found the updated code on github which works:
https://github.com/IntrepidArtineer/SmallScaleBlenderPythonExperiments/blob/master/pyradio.py

Last edited 2 years ago by Steve Kozma
Steve Kozma
Steve Kozma
2 years ago
Reply to  Nikita

Thank you. I keep forgetting this all the time as I rarely write code.
Your coding samples are tremendously helpful for such beginners like me!