Rename Selected objects¶
This script is very short in the sense that there is only one line of blender code object.name = textfield.value
to rename object.
In this step we simply rename active object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import bpy
from boss.ui_creator import TextField, UICreator, Boss_OT_base_ui
def renameObj(caller: TextField):
caller.param.name = caller.value
def ui_elements(op: Boss_OT_base_ui):
UICreator.deleteAllUi(op) # delete any existing UI
o = bpy.context.object
mouse_x, mouse_y = UICreator.mouse_xy(op)
rd = (mouse_x, mouse_y - 25, 200, 25)
UICreator.textField(op, rd, text=o.name, param=o, onValueChange=renameObj)
|
In this step we repeat step 1 in a loop.
Note
Logic to create textfield in a loop is same as quick_run_free addon (where buttons are created in a loop.)
So when more objects are selected that can’t fit in one column textfields will be created in new column.
import bpy
from boss.ui_creator import TextField, UICreator, Boss_OT_base_ui, RectData
# for ui
element_height = 25
element_width = 200
element_space = 5
screen_margin = [50, 100]
# for selected object
sels: list = []
def renameObj(caller: TextField):
caller.param.name = caller.value
def ui_elements(op: Boss_OT_base_ui):
UICreator.deleteAllUi(op)
global sels
sels = bpy.context.selected_objects
mouse_x,mouse_y = UICreator.mouse_xy(op)
initX = mouse_x
initY = mouse_y
rd = RectData(
initX,
initY - element_height,
element_width,
element_height
)
reg_height = UICreator.rr(op).height - screen_margin[1]
for o in sels:
rd = rd.getTop(5)
if rd.yMax > reg_height:
initX += (element_width + element_space)
rd.setPosition(initX, screen_margin[0])
UICreator.textField(op, rd, text=o.name, param=o, onValueChange=renameObj)
bpy.ops.object.select_all(action='DESELECT')
Warning
I can see that changing TextField should also change selected object, that will be better user experience.
There is no callback for when you click on Textfield and enter textfield typing mode.
Temporarily, I solved it by making another class and overriding _makeEditable
. but that method is supposed
to be private. And, a user has no way of knowing that, since it’s not documented.
In future updates, there can be a callback method for onTextFieldChanged
or something.
import bpy
from boss.ui_creator import TextField,UICreator,Boss_OT_base_ui,RectData
# for ui
element_height = 25
element_width = 200
element_space = 5
screen_margin = [50, 100]
#
sels: list = []
class RTextField(TextField):
def _makeEditable(self, *args):
super(RTextField, self)._makeEditable()
o = self.param
# deselect all
bpy.ops.object.select_all(action='DESELECT')
# select
o.select_set(True)
# make active
bpy.context.view_layer.objects.active = o
def renameObj(caller: TextField):
# print(caller.param, caller.value)
caller.param.name = caller.value
def ui_elements(op: Boss_OT_base_ui):
UICreator.deleteAllUi(op)
global sels
sels = bpy.context.selected_objects
mouse_x,mouse_y = UICreator.mouse_xy(op)
initX = mouse_x
initY = mouse_y
rd = RectData(
initX,
initY - element_height,
element_width,
element_height
)
reg_height = UICreator.rr(op).height - screen_margin[1]
for o in sels:
rd = rd.getTop(5)
if rd.yMax > reg_height:
initX += (element_width + element_space)
rd.setPosition(initX, screen_margin[0])
RTextField(op, rd, value=o.name, param=o, onValueChange=renameObj)
bpy.ops.object.select_all(action='DESELECT')
In Maya (3d software), this type of script is very useful and enough to rename joint hierarchy (bones). Since blender’s works
differently, and there are context
and modes
some extra work is required. A generic object Renamer isn’t enough here.
To convert this script into an addon - To make an addon.