Making the UI
2 minutes read
In this section I will try to explain how the UI works. If you are interested in developing an add-on, I would recommend watching the Blender conference video I mentioned previously. I also used this Blender stack exchange thread.
Initialization
=
The code starts by defining metadata associated to the add-on. Then, we import modules and more precisely the other .py files contained in the folder. This is to avoid having everything in the same file, which would make reading the code difficult.
The classes
Blender UIs work by defining classes which will create the necessary objects for our UI to work.
UI_Settings
:
:
:
This class defines variables that will be used on the UI panel :
Clean_Sceneis a boolean, which will be displayed as a toggle button to activate or deactivate the scene cleaning function.Save_CSVworks the same way.Tex_Dir_Pathis a string that will contain the path to the textures folder.
UI_Panel
This class defines the panel we will see in the interface.
= # 3D Viewport
= # Sidebar
= # Panel name
= # Sidebar name
"""Define layout"""
=
=
=
=
=
=
=
=
=
=
=
=
=
= True
The class starts by telling it where to place our custom panel. Then, we define a draw_header function to display a title and a draw function to create the interface.
box0 = self.layout.box() creates a dark gray box in which we can place various elements. row0 = box0.row() creates a row in this box. Then, we can either :
- Use
row0.prop(...)to create an input button based on the variables defined in UI_Settings ; - Use
row0.operator(...)to create an button which will execute the function contained in an operator (in this case, OT_Main described below).
OT_Main
This class defines an operator. In our case this will be used to create a button to execute the code contained in the execute function.
= # having "." in the name is mandatory
=
=
=
=
=
=
, , , , , =
=
=
=
=
=
=
return # is mandatory
The execute function uses the data from the UI buttons and the functions defined in the other Python files.
Class registration
This part at the end of the file is used to make the add-on register the classes, otherwise it will not use them. It also creates a Custom_Panel property which is used to refer to the interface in the code.
=
=
del