Boss UIΒΆ

Check out the UI section in Boss for details.

Creating button at center of the region

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
from boss.ui_creator import UICreator

def onClick(caller):
    print('button is clicked')
    caller.op.quit()  # to quit after the button is clicked.


def ui_elements(op):
    UICreator.deleteAllUi(op)  # to delete all existing ui

    btn_width, btn_height = 100, 40

    rr = UICreator.rr(op)  # region_rect:rr

    rd = (
        rr.width/2 - btn_width/2,
        rr.height/2 - btn_height/2,
        btn_width,
        btn_height
    )

    UICreator.button(op, rd, 'text', onClick)

Creating button at mouse position

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from boss.ui_creator import UICreator

def onClick(caller):
    print('button is clicked')
    caller.op.quit()  # to quit after the button is clicked.

def ui_elements(op):
    UICreator.deleteAllUi(op)  # to delete all existing ui

    btn_width, btn_height = 100, 40

    mouse_x, mouse_y = UICreator.mouse_xy(op)

    rd = (mouse_x, mouse_y - btn_height, btn_width, btn_height)

    UICreator.button(op, rd, 'btn_text', onClick)

Creating a cube

This is example of creating a cube from Comparisons page

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import bpy
from boss.ui_creator import UICreator

def onEnterPress(caller):
    bpy.ops.mesh.primitive_cube_add(size=2)
    bpy.context.object.name = caller.value
    caller.op.quit()  # to quit after the button is clicked.


def ui_elements(op):
    UICreator.deleteAllUi(op)  # to delete all existing ui
    btn_width, btn_height = 100, 40

    rr = UICreator.rr(op)  # region_rect(rr)

    rd = (
        rr.width/2 - btn_width/2,
        rr.height/2 - btn_height/2,
        btn_width,
        btn_height
    )
    UICreator.textField(
        op,
        rectData=rd,
        text='Cube',
        onEnterPress=onEnterPress
    )

Creating Cube as an operator

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import bpy
from boss.ui_creator import *

def onEnterPress(caller):
    bpy.ops.mesh.primitive_cube_add(size=2)
    bpy.context.object.name = caller.value
    caller.op.quit()  # to quit after the button is clicked.


class CreateCubeOperator(Boss_OT_base_ui):
    """Create A Simple Cube"""
    bl_idname = "boss.create_cube"
    bl_label = "Create Cube"

    def ui_elements(self):
        op = self
        UICreator.deleteAllUi(op)  # to delete all existing ui
        btn_width, btn_height = 100, 40

        rr = UICreator.rr(op)  # region_rect(rr)

        rd = (
            rr.width / 2 - btn_width / 2,
            rr.height / 2 - btn_height / 2,
            btn_width,
            btn_height
        )
        UICreator.textField(
            op,
            rectData=rd,
            text='Cube',
            onEnterPress=onEnterPress
        )

def register():
    bpy.utils.register_class(CreateCubeOperator)

def unregister():
    bpy.utils.unregister_class(CreateCubeOperator)


if __name__ == "__main__":
    register()

# test call
# bpy.ops.boss.create_cube("INVOKE_DEFAULT")

Creating a draggable TitleBar

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from boss.ui_creator import RectData
from boss.ui_creator import UICreator as cc

def onClick(caller):
    print(caller)

def ui_elements(op):
    cc.deleteAllUi(op)  # to delete all existing ui
    mouse_x, mouse_y = cc.mouse_xy(op)
    btn_height, btn_width = 50, 200

    rd = RectData(mouse_x, mouse_y - 50, btn_width, btn_height)

    btn_title = cc.button(op, rd, text='Title Bar')

    space = 10

    rd = rd.getBottom(space)

    cc.button(
        op,rd,
        text='button 1',
        buttonData=onClick,
        parent = btn_title,
        canDrag = False,
    )

    rd = rd.getBottom(space)

    cc.button(
        op, rd,
        text='button 2',
        buttonData=onClick,
        parent=btn_title,
        canDrag=False,
    )


All basic UI in one script file

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from boss.ui_creator import Button, TextField, RectData
from boss.ui_creator import UICreator as cc

def onClick(caller: Button):
    print(caller)

def onTextField_TextChanged(caller: TextField):
    print(type(caller).__name__, f'value - {caller.value}, text - {caller.text}')

def onTextField_ValueChanged(caller: TextField):
    print(type(caller).__name__, f'value - {caller.value}, text - {caller.text}')

def onTextField_EnterPressed(caller: TextField):
    print(type(caller).__name__, f'value - {caller.value}, text - {caller.text}')

def onValueChange(caller):
    print(type(caller).__name__, caller.value)

def onTextChange(caller:TextField):
    print(type(caller).__name__, caller.value)

def onEnterPress(caller:TextField):
    print(type(caller).__name__, caller.value)



def ui_elements(op):
    cc.deleteAllUi(op)  # to delete all existing ui
    mouse_x, mouse_y = cc.mouse_xy(op)
    btn_height, btn_width = 50, 200

    rd = RectData(mouse_x, mouse_y - 50, btn_width, btn_height)

    btn_title = cc.button(op, rd, text='Title Bar',
                                 ttt='This is a button, you can drag it'
                                 )

    space = 0#10

    rd = rd.getBottom(space)

    cc.button(
        op,rd,
        text='text',
        buttonData=onClick,
        parent = btn_title,
        canDrag = False,
        ttt='This is a button'
    )

    rd = rd.getBottom(space)
    cc.textField(
        op,rd,
        onTextChange=onTextField_TextChanged,
        onValueChange=onTextField_ValueChanged,
        onEnterPress=onTextField_EnterPressed,
        parent = btn_title,
        canDrag = False,
        ttt='This is a TextField'
    )

    rd = rd.getBottom(space)
    cc.intField(
        op,rd,
        value=1,
        onTextChange=onTextChange,
        onValueChange=onValueChange,
        onEnterPress=onEnterPress,
        parent = btn_title,
        canDrag = False,
        ttt='This is a IntField'

    )

    rd = rd.getBottom(space)
    cc.floatField(
        op,rd,
        value=0.0,
        onTextChange=onTextChange,
        onValueChange=onValueChange,
        onEnterPress=onEnterPress,
        parent = btn_title,
        canDrag = False,
        ttt='This is a FloatField'
    )

    rd = rd.getBottom(space)
    cc.checkBox(
        op,rd,
        text='text',
        value=True,
        onValueChange=onValueChange,
        parent = btn_title,
        canDrag = False,
        ttt='This is a Checkbox(boolean)'
    )
    rd = rd.getBottom(space)

    cc.vectorBooleanField(
        op,rd,
        value=(True, False, True),
        onValueChange=onValueChange,
        parent = btn_title,
        canDrag = False,
        ttt='This is VectorBooleanField'
    )

    rd = rd.getBottom(space)

    cc.vectorFloatField(
        op,rd,
        value=(0.0, 0.0, 0.0),
        onValueChange=onValueChange,
        onTextChange=onTextChange,
        onEnterPress=onEnterPress,
        parent = btn_title,
        canDrag = False,
        ttt='This is VectorFloatField'
    )

    rd = rd.getBottom(space)

    cc.vectorIntField(
        op, rd,
        value=(0, 0, 0),
        onValueChange=onValueChange,
        onTextChange=onTextChange,
        onEnterPress=onEnterPress,
        parent = btn_title,
        canDrag = False,
        ttt='This is VectorIntField'
    )


Tip

from boss.ui_creator import UICreator as cc here cc is easy to type with one finger. Remember it as short for Creator Class.