Blender V5.3
rna_armature_api.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdlib>
10#include <cstring>
11#include <ctime>
12
13#include "RNA_define.hh"
14
15#include "rna_internal.hh" /* own include */
16
17#ifdef RNA_RUNTIME
18
19# include <cstddef>
20
21# include "DNA_armature_types.h"
22
23# include "BLI_math_matrix_c.hh"
24# include "BLI_math_vector_c.hh"
25
26# include "BKE_armature.hh"
27# include "BKE_report.hh"
28
29# include "ED_armature.hh"
30
32
33# include "WM_api.hh"
34
35namespace blender {
36
37static void rna_EditBone_align_roll(EditBone *ebo, const float no[3])
38{
39 ebo->roll = ED_armature_ebone_roll_to_vector(ebo, no, false);
41}
42
43static float rna_Bone_do_envelope(Bone *bone, const float vec[3])
44{
45 float scale = (bone->flag & BONE_MULT_VG_ENV) == BONE_MULT_VG_ENV ? bone->weight : 1.0f;
46 return distfactor_to_bone(vec,
47 bone->arm_head,
48 bone->arm_tail,
49 bone->rad_head * scale,
50 bone->rad_tail * scale,
51 bone->dist * scale);
52}
53
54static void rna_Bone_convert_local_to_pose(Bone *bone,
55 float r_matrix[16],
56 const float matrix[16],
57 const float matrix_local[16],
58 const float parent_matrix[16],
59 const float parent_matrix_local[16],
60 bool invert)
61{
63 float offs_bone[4][4];
64 float (*bone_arm_mat)[4] = reinterpret_cast<float (*)[4]>(const_cast<float *>(matrix_local));
65 float (*parent_pose_mat)[4] = reinterpret_cast<float (*)[4]>(const_cast<float *>(parent_matrix));
66 float (*parent_arm_mat)[4] = reinterpret_cast<float (*)[4]>(
67 const_cast<float *>(parent_matrix_local));
68
69 if (is_zero_m4(parent_pose_mat) || is_zero_m4(parent_arm_mat)) {
70 /* No parent case. */
72 bone->flag, bone->inherit_scale_mode, bone_arm_mat, nullptr, nullptr, &bpt);
73 }
74 else {
75 invert_m4_m4(offs_bone, parent_arm_mat);
76 mul_m4_m4m4(offs_bone, offs_bone, bone_arm_mat);
77
79 bone->flag, bone->inherit_scale_mode, offs_bone, parent_arm_mat, parent_pose_mat, &bpt);
80 }
81
82 if (invert) {
84 }
85
87 reinterpret_cast<float (*)[4]>(const_cast<float *>(matrix)),
88 reinterpret_cast<float (*)[4]>(r_matrix));
89}
90
91static void rna_Bone_MatrixFromAxisRoll(const float axis[3], float roll, float r_matrix[9])
92{
93 vec_roll_to_mat3(axis, roll, reinterpret_cast<float (*)[3]>(r_matrix));
94}
95
96static void rna_Bone_AxisRollFromMatrix(const float matrix[9],
97 const float axis_override[3],
98 float r_axis[3],
99 float *r_roll)
100{
101 float mat[3][3];
102
103 normalize_m3_m3(mat, reinterpret_cast<float (*)[3]>(const_cast<float *>(matrix)));
104
105 if (normalize_v3_v3(r_axis, axis_override) != 0.0f) {
106 mat3_vec_to_roll(mat, r_axis, r_roll);
107 }
108 else {
109 mat3_to_vec_roll(mat, r_axis, r_roll);
110 }
111}
112
113using bonecoll_assign_func_bone = bool (*)(BoneCollection *, Bone *);
114using bonecoll_assign_func_ebone = bool (*)(BoneCollection *, EditBone *);
115
116static bool rna_BoneCollection_assign_abstract(BoneCollection *bcoll,
117 bContext *C,
118 ReportList *reports,
119 PointerRNA *bone_ptr,
120 bonecoll_assign_func_bone assign_bone,
121 bonecoll_assign_func_ebone assign_ebone)
122
123{
124 if (!*bone_ptr) {
125 return false;
126 }
127
128 if (RNA_struct_is_a(bone_ptr->type, RNA_PoseBone)) {
129 Object *ob = id_cast<Object *>(bone_ptr->owner_id);
130 bPoseChannel *pchan = static_cast<bPoseChannel *>(bone_ptr->data);
131 const bool made_any_change = assign_bone(bcoll, pchan->bone_get(*ob));
132 if (made_any_change) {
134 }
135 return made_any_change;
136 }
137
138 if (RNA_struct_is_a(bone_ptr->type, RNA_Bone)) {
139 Bone *bone = static_cast<Bone *>(bone_ptr->data);
140 const bool made_any_change = assign_bone(bcoll, bone);
141 if (made_any_change) {
143 }
144 return made_any_change;
145 }
146
147 if (RNA_struct_is_a(bone_ptr->type, RNA_EditBone)) {
148 EditBone *ebone = static_cast<EditBone *>(bone_ptr->data);
149 const bool made_any_change = assign_ebone(bcoll, ebone);
150 if (made_any_change) {
152 }
153 return made_any_change;
154 }
155 BKE_reportf(reports,
156 RPT_ERROR,
157 "%s is not supported, pass a Bone, PoseBone, or EditBone",
158 RNA_struct_identifier(bone_ptr->type));
159 return false;
160}
161
162static bool rna_BoneCollection_assign(BoneCollection *bcoll,
163 bContext *C,
164 ReportList *reports,
165 PointerRNA *bone_ptr)
166{
167 return rna_BoneCollection_assign_abstract(bcoll,
168 C,
169 reports,
170 bone_ptr,
173}
174
175static bool rna_BoneCollection_unassign(BoneCollection *bcoll,
176 bContext *C,
177 ReportList *reports,
178 PointerRNA *bone_ptr)
179{
180 return rna_BoneCollection_assign_abstract(bcoll,
181 C,
182 reports,
183 bone_ptr,
186}
187
188} // namespace blender
189
190#else
191
192namespace blender {
193
195{
196 FunctionRNA *func;
197 PropertyRNA *parm;
198
199 func = RNA_def_function(srna, "align_roll", "rna_EditBone_align_roll");
201 "Align the bone to a local-space roll so the Z axis "
202 "points in the direction of the vector given");
204 func, "vector", 3, nullptr, -FLT_MAX, FLT_MAX, "Vector", "", -FLT_MAX, FLT_MAX);
206}
207
209{
210 PropertyRNA *parm;
211 FunctionRNA *func;
212
213 func = RNA_def_function(srna, "evaluate_envelope", "rna_Bone_do_envelope");
214 RNA_def_function_ui_description(func, "Calculate bone envelope at given point");
215 parm = RNA_def_float_vector_xyz(func,
216 "point",
217 3,
218 nullptr,
219 -FLT_MAX,
220 FLT_MAX,
221 "Point",
222 "Position in 3d space to evaluate",
223 -FLT_MAX,
224 FLT_MAX);
226 /* return value */
227 parm = RNA_def_float(
228 func, "factor", 0, -FLT_MAX, FLT_MAX, "Factor", "Envelope factor", -FLT_MAX, FLT_MAX);
229 RNA_def_function_return(func, parm);
230
231 func = RNA_def_function(srna, "convert_local_to_pose", "rna_Bone_convert_local_to_pose");
233 "Transform a matrix from Local to Pose space (or back), taking "
234 "into account options like Inherit Scale and Local Location. "
235 "Unlike Object.convert_space, this uses custom rest and pose "
236 "matrices provided by the caller. If the parent matrices are "
237 "omitted, the bone is assumed to have no parent.");
238 parm = RNA_def_property(func, "matrix_return", PROP_FLOAT, PROP_MATRIX);
240 RNA_def_property_ui_text(parm, "", "The transformed matrix");
241 RNA_def_function_output(func, parm);
242 parm = RNA_def_property(func, "matrix", PROP_FLOAT, PROP_MATRIX);
244 RNA_def_property_ui_text(parm, "", "The matrix to transform");
246 parm = RNA_def_property(func, "matrix_local", PROP_FLOAT, PROP_MATRIX);
248 RNA_def_property_ui_text(parm, "", "The custom rest matrix of this bone (Bone.matrix_local)");
250 parm = RNA_def_property(func, "parent_matrix", PROP_FLOAT, PROP_MATRIX);
253 parm, "", "The custom pose matrix of the parent bone (PoseBone.matrix)");
254 parm = RNA_def_property(func, "parent_matrix_local", PROP_FLOAT, PROP_MATRIX);
257 parm, "", "The custom rest matrix of the parent bone (Bone.matrix_local)");
258 RNA_def_boolean(func, "invert", false, "", "Convert from Pose to Local space");
259
260 /* Conversions between Matrix and Axis + Roll representations. */
261 func = RNA_def_function(srna, "MatrixFromAxisRoll", "rna_Bone_MatrixFromAxisRoll");
262 RNA_def_function_ui_description(func, "Convert the axis + roll representation to a matrix");
264 parm = RNA_def_property(func, "axis", PROP_FLOAT, PROP_XYZ);
265 RNA_def_property_array(parm, 3);
266 RNA_def_property_ui_text(parm, "", "The main axis of the bone (tail - head)");
268 parm = RNA_def_property(func, "roll", PROP_FLOAT, PROP_NONE);
269 RNA_def_property_ui_text(parm, "", "The roll of the bone");
271 parm = RNA_def_property(func, "result_matrix", PROP_FLOAT, PROP_MATRIX);
273 RNA_def_property_ui_text(parm, "", "The resulting orientation matrix");
274 RNA_def_function_output(func, parm);
275
276 func = RNA_def_function(srna, "AxisRollFromMatrix", "rna_Bone_AxisRollFromMatrix");
278 "Convert a rotational matrix to the axis + roll representation. "
279 "Note that the resulting value of the roll may not be as "
280 "expected if the matrix has shear or negative determinant.");
282 parm = RNA_def_property(func, "matrix", PROP_FLOAT, PROP_MATRIX);
284 RNA_def_property_ui_text(parm, "", "The orientation matrix of the bone");
286 parm = RNA_def_property(func, "axis", PROP_FLOAT, PROP_MATRIX);
287 RNA_def_property_array(parm, 3);
289 parm, "", "The optional override for the axis (finds closest approximation for the matrix)");
290 parm = RNA_def_property(func, "result_axis", PROP_FLOAT, PROP_XYZ);
291 RNA_def_property_array(parm, 3);
292 RNA_def_property_ui_text(parm, "", "The main axis of the bone");
293 RNA_def_function_output(func, parm);
294 parm = RNA_def_property(func, "result_roll", PROP_FLOAT, PROP_NONE);
295 RNA_def_property_ui_text(parm, "", "The roll of the bone");
296 RNA_def_function_output(func, parm);
297}
298
300{
301 PropertyRNA *parm;
302 FunctionRNA *func;
303
304 func = RNA_def_function(srna, "assign", "rna_BoneCollection_assign");
306 RNA_def_function_ui_description(func, "Assign the given bone to this collection");
307 parm = RNA_def_pointer(
308 func, "bone", "AnyType", "", "Bone, PoseBone, or EditBone to assign to this collection");
310 /* return value */
311 parm = RNA_def_boolean(func,
312 "assigned",
313 false,
314 "Assigned",
315 "Whether the bone was actually assigned; will be false if the bone was "
316 "already member of the collection");
317 RNA_def_function_return(func, parm);
318
319 func = RNA_def_function(srna, "unassign", "rna_BoneCollection_unassign");
321 RNA_def_function_ui_description(func, "Remove the given bone from this collection");
322 parm = RNA_def_pointer(
323 func, "bone", "AnyType", "", "Bone, PoseBone, or EditBone to remove from this collection");
325 /* return value */
326 parm = RNA_def_boolean(func,
327 "assigned",
328 false,
329 "Unassigned",
330 "Whether the bone was actually removed; will be false if the bone was "
331 "not a member of the collection to begin with");
332 RNA_def_function_return(func, parm);
333}
334
335} // namespace blender
336
337#endif
C++ functions to deal with Armature collections (i.e. the successor of bone layers).
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define ND_BONE_COLLECTION
Definition WM_types.hh:515
#define NC_OBJECT
Definition WM_types.hh:392
#define FLT_MAX
float2x2 invert(float2x2 mat)
float3x3 scale(float3x3 mat, float2 scale)
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
void mat3_vec_to_roll(const float mat[3][3], const float vec[3], float *r_roll)
bool ANIM_armature_bonecoll_assign(BoneCollection *bcoll, Bone *bone)
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
bool ANIM_armature_bonecoll_assign_editbone(BoneCollection *bcoll, EditBone *ebone)
void vec_roll_to_mat3(const float vec[3], float roll, float r_mat[3][3])
void RNA_def_function_output(FunctionRNA *, PropertyRNA *ret)
void RNA_api_bone(StructRNA *srna)
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
void mat3_to_vec_roll(const float mat[3][3], float r_vec[3], float *r_roll)
void RNA_def_property_multi_array(PropertyRNA *prop, int dimension, const int length[])
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
@ FUNC_USE_REPORTS
@ FUNC_NO_SELF
Definition RNA_types.hh:998
@ FUNC_USE_CONTEXT
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
PropertyRNA * RNA_def_float_vector(StructOrFunctionRNA *cont_, const char *identifier, const int len, const float *default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
float distfactor_to_bone(const float3 &position, const float3 &head, const float3 &tail, float radius_head, float radius_tail, float falloff_distance)
void BKE_bone_parent_transform_apply(const BoneParentTransform *bpt, const float inmat[4][4], float outmat[4][4])
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier_c_str, int type, int subtype)
void RNA_api_bonecollection(StructRNA *srna)
void BKE_bone_parent_transform_calc_from_matrices(int bone_flag, int inherit_scale_mode, const float offs_bone[4][4], const float parent_arm_mat[4][4], const float parent_pose_mat[4][4], BoneParentTransform *r_bpt)
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
bool ANIM_armature_bonecoll_unassign_editbone(BoneCollection *bcoll, EditBone *ebone)
void BKE_bone_parent_transform_invert(BoneParentTransform *bpt)
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier_c_str, const char *call)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
bool is_zero_m4(const float mat[4][4])
void normalize_m3_m3(float R[3][3], const float M[3][3]) ATTR_NONNULL()
MINLINE float normalize_v3_v3(float r[3], const float a[3])
const char * RNA_struct_identifier(const StructRNA *type)
void RNA_def_function_flag(FunctionRNA *func, FunctionFlag flag)
PropertyRNA * RNA_def_float_vector_xyz(StructOrFunctionRNA *cont_, const char *identifier, const int len, const float *default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
void RNA_api_armature_edit_bone(StructRNA *srna)
@ PROP_NEVER_NULL
Definition RNA_types.hh:447
bool ANIM_armature_bonecoll_unassign(BoneCollection *bcoll, Bone *bone)
float ED_armature_ebone_roll_to_vector(const EditBone *bone, const float align_axis[3], const bool axis_only)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
@ PROP_MATRIX
Definition RNA_types.hh:322
@ PARM_REQUIRED
Definition RNA_types.hh:631
@ PARM_RNAPTR
Definition RNA_types.hh:633
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
void RNA_def_property_array(PropertyRNA *prop, int length)
const int rna_matrix_dimsize_4x4[]
const int rna_matrix_dimsize_3x3[]
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float