Blender V5.3
interface_template_matrix.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <fmt/format.h>
10
11#include "BKE_unit.hh"
12
13#include "BLI_math_matrix_c.hh"
15
16#include "BLT_translation.hh"
17
18#include "RNA_access.hh"
19#include "RNA_enum_types.hh"
20
21#include "interface_intern.hh"
22
23namespace blender::ui {
24
25/* Format translation/rotation value as a string based on Blender unit settings. */
26static std::string format_unit_value(float value, PropertySubType subtype, Layout &layout)
27{
28 const UnitSettings *unit = layout.block()->unit;
29 const int unit_type = RNA_SUBTYPE_UNIT(subtype);
30
31 /* Change negative zero to regular zero, without altering anything else. */
32 value += +0.0f;
33 double value_scaled = BKE_unit_value_scale(*unit, unit_type, value);
34
35 char new_str[UI_MAX_DRAW_STR];
37 sizeof(new_str),
38 value_scaled,
40 RNA_SUBTYPE_UNIT_VALUE(unit_type),
41 *unit,
42 true,
43 true);
44 return std::string(new_str);
45}
46
48static std::string format_coefficient(float value)
49{
50 /* Change negative zero to regular zero, without altering anything else. */
51 value += +0.0f;
52 /* Same precision that we use in `Object.scale`. */
53 const int RNA_SCALE_PREC_DEFAULT = 3;
54 return fmt::format("{:.{}f}", value, RNA_SCALE_PREC_DEFAULT);
55}
56
57/* Static variable to store rotation mode button state at runtime.
58 * Defaults to XYZ Euler. */
60
61static void rotation_mode_menu_callback(bContext * /*C*/, Layout *layout, void * /*arg1*/)
62{
63 for (size_t i = 0; i < RNA_enum_items_count(rna_enum_object_rotation_mode_items); i++) {
65 const int yco = -1.5f * UI_UNIT_Y;
66 const int width = 9 * UI_UNIT_X;
67 Button *but = uiDefButV(layout->block(),
69 IFACE_(mode_info.name),
70 0,
71 yco,
72 width / 2,
75 i,
76 i,
77 TIP_(mode_info.description));
79 if (i == rotation_mode_index) {
81 }
82 }
83}
84
85static void draw_matrix_template(Layout &layout, PointerRNA &ptr, PropertyRNA &prop)
86{
87 /* Matrix template UI is mirroring Object's Transform UI for better UX. */
88 Layout *row, *col;
89 Layout &layout_ = layout.box();
90
91 float m4[4][4];
92 RNA_property_float_get_array(&ptr, &prop, &m4[0][0]);
93
94 /* Show a warning as a matrix with a shear cannot be represented fully
95 * by a decomposition.
96 * Use the 3x3 matrix, as shear in the 4x4 homogeneous matrix
97 * is expected due to the translation component. */
98 float m3[3][3];
99 copy_m3_m4(m3, m4);
100 if (!is_orthogonal_m3(m3)) {
101 layout_.label(RPT_("Matrix has a shear"), ICON_STATUS_WARNING);
102 }
103
104 float loc[3], quat[4], size[3];
105 mat4_decompose(loc, quat, size, m4);
106
107 /* Translation. */
108 col = &layout_.column(true);
109 col->use_property_split_set(true);
110
111 row = &col->row(true);
112 uiItemL_respect_property_split(row, IFACE_("Location X"), ICON_NONE);
113 row->label(format_unit_value(loc[0], PROP_TRANSLATION, layout_), ICON_NONE);
114
115 row = &col->row(true);
116 uiItemL_respect_property_split(row, IFACE_("Y"), ICON_NONE);
117 row->label(format_unit_value(loc[1], PROP_TRANSLATION, layout_), ICON_NONE);
118
119 row = &col->row(true);
120 uiItemL_respect_property_split(row, IFACE_("Z"), ICON_NONE);
121 row->label(format_unit_value(loc[2], PROP_TRANSLATION, layout_), ICON_NONE);
122
123 /* Rotation. */
124 float eul[3], axis[3];
125 float angle;
127 col = &layout_.column(true);
128 col->use_property_split_set(true);
129
130 if (mode_info.value == ROT_MODE_QUAT) {
131 row = &col->row(true);
132 uiItemL_respect_property_split(row, IFACE_("Rotation W"), ICON_NONE);
133 row->label(format_coefficient(quat[0]), ICON_NONE);
134
135 row = &col->row(true);
136 uiItemL_respect_property_split(row, IFACE_("X"), ICON_NONE);
137 row->label(format_coefficient(quat[1]), ICON_NONE);
138
139 row = &col->row(true);
140 uiItemL_respect_property_split(row, IFACE_("Y"), ICON_NONE);
141 row->label(format_coefficient(quat[2]), ICON_NONE);
142
143 row = &col->row(true);
144 uiItemL_respect_property_split(row, IFACE_("Z"), ICON_NONE);
145 row->label(format_coefficient(quat[3]), ICON_NONE);
146 }
147 else if (mode_info.value == ROT_MODE_AXISANGLE) {
148 quat_to_axis_angle(axis, &angle, quat);
149
150 row = &col->row(true);
151 uiItemL_respect_property_split(row, IFACE_("Rotation W"), ICON_NONE);
152 row->label(format_unit_value(angle, PROP_ANGLE, layout_), ICON_NONE);
153
154 row = &col->row(true);
155 uiItemL_respect_property_split(row, IFACE_("X"), ICON_NONE);
156 row->label(format_coefficient(axis[0]), ICON_NONE);
157
158 row = &col->row(true);
159 uiItemL_respect_property_split(row, IFACE_("Y"), ICON_NONE);
160 row->label(format_coefficient(axis[1]), ICON_NONE);
161
162 row = &col->row(true);
163 uiItemL_respect_property_split(row, IFACE_("Z"), ICON_NONE);
164 row->label(format_coefficient(axis[2]), ICON_NONE);
165 }
166 else { /* Euler modes. */
167 quat_to_eulO(eul, mode_info.value, quat);
168
169 row = &col->row(true);
170 uiItemL_respect_property_split(row, IFACE_("Rotation X"), ICON_NONE);
171 row->label(format_unit_value(eul[0], PROP_ANGLE, layout_), ICON_NONE);
172
173 row = &col->row(true);
174 uiItemL_respect_property_split(row, IFACE_("Y"), ICON_NONE);
175 row->label(format_unit_value(eul[1], PROP_ANGLE, layout_), ICON_NONE);
176
177 row = &col->row(true);
178 uiItemL_respect_property_split(row, IFACE_("Z"), ICON_NONE);
179 row->label(format_unit_value(eul[2], PROP_ANGLE, layout_), ICON_NONE);
180 }
181
182 /* Mirror RNA enum property drop-down UI - with menu triangle an drop-down items. */
183 row = &layout_.row(true);
184 uiItemL_respect_property_split(row, IFACE_("Mode"), ICON_NONE);
185 Block *block = row->block();
186 Button *but = uiDefMenuBut(block,
188 nullptr,
189 IFACE_(mode_info.name),
190 0,
191 0,
192 UI_UNIT_X * 10,
193 UI_UNIT_Y,
194 TIP_("Rotation mode.\n\nOnly affects the way "
195 "rotation is displayed, rotation itself is unaffected."));
197
198 /* Scale. */
199 col = &layout_.column(true);
200 col->use_property_split_set(true);
201
202 row = &col->row(true);
203 uiItemL_respect_property_split(row, IFACE_("Scale X"), ICON_NONE);
204 row->label(format_coefficient(size[0]), ICON_NONE);
205
206 row = &col->row(true);
207 uiItemL_respect_property_split(row, IFACE_("Y"), ICON_NONE);
208 row->label(format_coefficient(size[1]), ICON_NONE);
209
210 row = &col->row(true);
211 uiItemL_respect_property_split(row, IFACE_("Z"), ICON_NONE);
212 row->label(format_coefficient(size[2]), ICON_NONE);
213}
214
215void template_matrix(Layout *layout, PointerRNA *ptr, const StringRefNull propname)
216{
217 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
218
219 if (!prop || RNA_property_type(prop) != PROP_FLOAT ||
221 {
222 RNA_warning("4x4 Matrix property not found: %s.%s",
224 propname.c_str());
225 return;
226 }
227 draw_matrix_template(*layout, *ptr, *prop);
228}
229
230} // namespace blender::ui
#define RPT_(msgid)
#define TIP_(msgid)
#define IFACE_(msgid)
#define RNA_warning(format,...)
#define RNA_SUBTYPE_UNIT_VALUE(subtype)
Definition RNA_types.hh:268
#define RNA_TRANSLATION_PREC_DEFAULT
Definition RNA_types.hh:272
#define RNA_SUBTYPE_UNIT(subtype)
Definition RNA_types.hh:266
#define UI_UNIT_Y
#define UI_UNIT_X
#define UI_MAX_DRAW_STR
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
constexpr const char * c_str() const
static void draw_matrix_template(Layout &layout, PointerRNA &ptr, PropertyRNA &prop)
static void rotation_mode_menu_callback(bContext *, Layout *layout, void *)
void template_matrix(Layout *layout, PointerRNA *ptr, StringRefNull propname)
static std::string format_unit_value(float value, PropertySubType subtype, Layout &layout)
static std::string format_coefficient(float value)
void button_flag_enable(Button *but, int64_t flag)
Button * uiDefMenuBut(Block *block, MenuCreateFunc func, void *arg, StringRef str, int x, int y, short width, short height, std::optional< StringRef > tip)
void button_flag_disable(Button *but, int64_t flag)
Layout * uiItemL_respect_property_split(Layout *layout, StringRef text, int icon)
Button * uiDefButV(Block *block, ButtonType type, StringRef str, int x, int y, short width, short height, T *poin, float min, float max, std::optional< StringRef > tip)
void button_type_set_menu_from_pulldown(Button *but)
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
double BKE_unit_value_scale(const UnitSettings &settings, int unit_type, double value)
Definition unit.cc:1963
void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void copy_m3_m4(float m1[3][3], const float m2[4][4])
const EnumPropertyItem rna_enum_object_rotation_mode_items[]
uint RNA_enum_items_count(const EnumPropertyItem *item)
PropertyType RNA_property_type(PropertyRNA *prop)
bool is_orthogonal_m3(const float m[3][3])
void quat_to_eulO(float e[3], short order, const float q[4])
void quat_to_axis_angle(float axis[3], float *angle, const float q[4])
size_t BKE_unit_value_as_string(char *str, int str_maxncpy, double value, int prec, int type, const UnitSettings &settings, bool pad, bool do_unit_suffix)
Definition unit.cc:1929
const char * RNA_struct_identifier(const StructRNA *type)
PropertySubType RNA_property_subtype(PropertyRNA *prop)
@ PROP_TRANSLATION
Definition RNA_types.hh:318
@ PROP_MATRIX
Definition RNA_types.hh:322
void mat4_decompose(float loc[3], float quat[4], float size[3], const float wmat[4][4])
StructRNA * type
Definition RNA_types.hh:63
const UnitSettings * unit
void use_property_split_set(bool is_sep)
void label(StringRef name, int icon)
Layout & column(bool align)
Layout & row(bool align)