Blender V4.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
9#include <cstdio>
10#include <cstdlib>
11#include <cstring>
12#include <ctime>
13
14#include "RNA_define.hh"
15
16#include "rna_internal.hh" /* own include */
17
18#ifdef RNA_RUNTIME
19
20# include <stddef.h>
21
22# include "DNA_armature_types.h"
23
24# include "BKE_armature.hh"
25# include "BLI_math_matrix.h"
26# include "BLI_math_vector.h"
27
28static void rna_EditBone_align_roll(EditBone *ebo, const float no[3])
29{
30 ebo->roll = ED_armature_ebone_roll_to_vector(ebo, no, false);
31}
32
33static float rna_Bone_do_envelope(Bone *bone, const float vec[3])
34{
35 float scale = (bone->flag & BONE_MULT_VG_ENV) == BONE_MULT_VG_ENV ? bone->weight : 1.0f;
36 return distfactor_to_bone(vec,
37 bone->arm_head,
38 bone->arm_tail,
39 bone->rad_head * scale,
40 bone->rad_tail * scale,
41 bone->dist * scale);
42}
43
44static void rna_Bone_convert_local_to_pose(Bone *bone,
45 float r_matrix[16],
46 const float matrix[16],
47 const float matrix_local[16],
48 const float parent_matrix[16],
49 const float parent_matrix_local[16],
50 bool invert)
51{
53 float offs_bone[4][4];
54 float(*bone_arm_mat)[4] = (float(*)[4])matrix_local;
55 float(*parent_pose_mat)[4] = (float(*)[4])parent_matrix;
56 float(*parent_arm_mat)[4] = (float(*)[4])parent_matrix_local;
57
58 if (is_zero_m4(parent_pose_mat) || is_zero_m4(parent_arm_mat)) {
59 /* No parent case. */
61 bone->flag, bone->inherit_scale_mode, bone_arm_mat, nullptr, nullptr, &bpt);
62 }
63 else {
64 invert_m4_m4(offs_bone, parent_arm_mat);
65 mul_m4_m4m4(offs_bone, offs_bone, bone_arm_mat);
66
68 bone->flag, bone->inherit_scale_mode, offs_bone, parent_arm_mat, parent_pose_mat, &bpt);
69 }
70
71 if (invert) {
73 }
74
75 BKE_bone_parent_transform_apply(&bpt, (float(*)[4])matrix, (float(*)[4])r_matrix);
76}
77
78static void rna_Bone_MatrixFromAxisRoll(const float axis[3], float roll, float r_matrix[9])
79{
80 vec_roll_to_mat3(axis, roll, (float(*)[3])r_matrix);
81}
82
83static void rna_Bone_AxisRollFromMatrix(const float matrix[9],
84 const float axis_override[3],
85 float r_axis[3],
86 float *r_roll)
87{
88 float mat[3][3];
89
90 normalize_m3_m3(mat, (float(*)[3])matrix);
91
92 if (normalize_v3_v3(r_axis, axis_override) != 0.0f) {
93 mat3_vec_to_roll(mat, r_axis, r_roll);
94 }
95 else {
96 mat3_to_vec_roll(mat, r_axis, r_roll);
97 }
98}
99
100using bonecoll_assign_func_bone = bool (*)(BoneCollection *, Bone *);
101using bonecoll_assign_func_ebone = bool (*)(BoneCollection *, EditBone *);
102
103static bool rna_BoneCollection_assign_abstract(BoneCollection *bcoll,
104 bContext *C,
105 ReportList *reports,
106 PointerRNA *bone_ptr,
107 bonecoll_assign_func_bone assign_bone,
108 bonecoll_assign_func_ebone assign_ebone)
109
110{
111 if (RNA_pointer_is_null(bone_ptr)) {
112 return false;
113 }
114
115 if (RNA_struct_is_a(bone_ptr->type, &RNA_PoseBone)) {
116 bPoseChannel *pchan = static_cast<bPoseChannel *>(bone_ptr->data);
117 const bool made_any_change = assign_bone(bcoll, pchan->bone);
118 if (made_any_change) {
120 }
121 return made_any_change;
122 }
123
124 if (RNA_struct_is_a(bone_ptr->type, &RNA_Bone)) {
125 Bone *bone = static_cast<Bone *>(bone_ptr->data);
126 const bool made_any_change = assign_bone(bcoll, bone);
127 if (made_any_change) {
129 }
130 return made_any_change;
131 }
132
133 if (RNA_struct_is_a(bone_ptr->type, &RNA_EditBone)) {
134 EditBone *ebone = static_cast<EditBone *>(bone_ptr->data);
135 const bool made_any_change = assign_ebone(bcoll, ebone);
136 if (made_any_change) {
138 }
139 return made_any_change;
140 }
141 BKE_reportf(reports,
142 RPT_ERROR,
143 "%s is not supported, pass a Bone, PoseBone, or EditBone",
144 RNA_struct_identifier(bone_ptr->type));
145 return false;
146}
147
148static bool rna_BoneCollection_assign(BoneCollection *bcoll,
149 bContext *C,
150 ReportList *reports,
151 PointerRNA *bone_ptr)
152{
153 return rna_BoneCollection_assign_abstract(bcoll,
154 C,
155 reports,
156 bone_ptr,
159}
160
161static bool rna_BoneCollection_unassign(BoneCollection *bcoll,
162 bContext *C,
163 ReportList *reports,
164 PointerRNA *bone_ptr)
165{
166 return rna_BoneCollection_assign_abstract(bcoll,
167 C,
168 reports,
169 bone_ptr,
172}
173
174#else
175
177{
178 FunctionRNA *func;
179 PropertyRNA *parm;
180
181 func = RNA_def_function(srna, "align_roll", "rna_EditBone_align_roll");
183 "Align the bone to a local-space roll so the Z axis "
184 "points in the direction of the vector given");
186 func, "vector", 3, nullptr, -FLT_MAX, FLT_MAX, "Vector", "", -FLT_MAX, FLT_MAX);
188}
189
191{
192 PropertyRNA *parm;
193 FunctionRNA *func;
194
195 func = RNA_def_function(srna, "evaluate_envelope", "rna_Bone_do_envelope");
196 RNA_def_function_ui_description(func, "Calculate bone envelope at given point");
197 parm = RNA_def_float_vector_xyz(func,
198 "point",
199 3,
200 nullptr,
201 -FLT_MAX,
202 FLT_MAX,
203 "Point",
204 "Position in 3d space to evaluate",
205 -FLT_MAX,
206 FLT_MAX);
208 /* return value */
209 parm = RNA_def_float(
210 func, "factor", 0, -FLT_MAX, FLT_MAX, "Factor", "Envelope factor", -FLT_MAX, FLT_MAX);
211 RNA_def_function_return(func, parm);
212
213 func = RNA_def_function(srna, "convert_local_to_pose", "rna_Bone_convert_local_to_pose");
215 "Transform a matrix from Local to Pose space (or back), taking "
216 "into account options like Inherit Scale and Local Location. "
217 "Unlike Object.convert_space, this uses custom rest and pose "
218 "matrices provided by the caller. If the parent matrices are "
219 "omitted, the bone is assumed to have no parent.");
220 parm = RNA_def_property(func, "matrix_return", PROP_FLOAT, PROP_MATRIX);
222 RNA_def_property_ui_text(parm, "", "The transformed matrix");
223 RNA_def_function_output(func, parm);
224 parm = RNA_def_property(func, "matrix", PROP_FLOAT, PROP_MATRIX);
226 RNA_def_property_ui_text(parm, "", "The matrix to transform");
228 parm = RNA_def_property(func, "matrix_local", PROP_FLOAT, PROP_MATRIX);
230 RNA_def_property_ui_text(parm, "", "The custom rest matrix of this bone (Bone.matrix_local)");
232 parm = RNA_def_property(func, "parent_matrix", PROP_FLOAT, PROP_MATRIX);
235 parm, "", "The custom pose matrix of the parent bone (PoseBone.matrix)");
236 parm = RNA_def_property(func, "parent_matrix_local", PROP_FLOAT, PROP_MATRIX);
239 parm, "", "The custom rest matrix of the parent bone (Bone.matrix_local)");
240 RNA_def_boolean(func, "invert", false, "", "Convert from Pose to Local space");
241
242 /* Conversions between Matrix and Axis + Roll representations. */
243 func = RNA_def_function(srna, "MatrixFromAxisRoll", "rna_Bone_MatrixFromAxisRoll");
244 RNA_def_function_ui_description(func, "Convert the axis + roll representation to a matrix");
246 parm = RNA_def_property(func, "axis", PROP_FLOAT, PROP_XYZ);
247 RNA_def_property_array(parm, 3);
248 RNA_def_property_ui_text(parm, "", "The main axis of the bone (tail - head)");
250 parm = RNA_def_property(func, "roll", PROP_FLOAT, PROP_NONE);
251 RNA_def_property_ui_text(parm, "", "The roll of the bone");
253 parm = RNA_def_property(func, "result_matrix", PROP_FLOAT, PROP_MATRIX);
255 RNA_def_property_ui_text(parm, "", "The resulting orientation matrix");
256 RNA_def_function_output(func, parm);
257
258 func = RNA_def_function(srna, "AxisRollFromMatrix", "rna_Bone_AxisRollFromMatrix");
260 "Convert a rotational matrix to the axis + roll representation. "
261 "Note that the resulting value of the roll may not be as "
262 "expected if the matrix has shear or negative determinant.");
264 parm = RNA_def_property(func, "matrix", PROP_FLOAT, PROP_MATRIX);
266 RNA_def_property_ui_text(parm, "", "The orientation matrix of the bone");
268 parm = RNA_def_property(func, "axis", PROP_FLOAT, PROP_MATRIX);
269 RNA_def_property_array(parm, 3);
271 parm, "", "The optional override for the axis (finds closest approximation for the matrix)");
272 parm = RNA_def_property(func, "result_axis", PROP_FLOAT, PROP_XYZ);
273 RNA_def_property_array(parm, 3);
274 RNA_def_property_ui_text(parm, "", "The main axis of the bone");
275 RNA_def_function_output(func, parm);
276 parm = RNA_def_property(func, "result_roll", PROP_FLOAT, PROP_NONE);
277 RNA_def_property_ui_text(parm, "", "The roll of the bone");
278 RNA_def_function_output(func, parm);
279}
280
282{
283 PropertyRNA *parm;
284 FunctionRNA *func;
285
286 func = RNA_def_function(srna, "assign", "rna_BoneCollection_assign");
288 RNA_def_function_ui_description(func, "Assign the given bone to this collection");
289 parm = RNA_def_pointer(
290 func, "bone", "AnyType", "", "Bone, PoseBone, or EditBone to assign to this collection");
292 /* return value */
293 parm = RNA_def_boolean(func,
294 "assigned",
295 false,
296 "Assigned",
297 "Whether the bone was actually assigned; will be false if the bone was "
298 "already member of the collection");
299 RNA_def_function_return(func, parm);
300
301 func = RNA_def_function(srna, "unassign", "rna_BoneCollection_unassign");
303 RNA_def_function_ui_description(func, "Remove the given bone from this collection");
304 parm = RNA_def_pointer(
305 func, "bone", "AnyType", "", "Bone, PoseBone, or EditBone to remove from this collection");
307 /* return value */
308 parm = RNA_def_boolean(func,
309 "assigned",
310 false,
311 "Unassigned",
312 "Whether the bone was actually removed; will be false if the bone was "
313 "not a member of the collection to begin with");
314 RNA_def_function_return(func, parm);
315}
316
317#endif
bool ANIM_armature_bonecoll_unassign_editbone(BoneCollection *bcoll, EditBone *ebone)
bool ANIM_armature_bonecoll_assign(BoneCollection *bcoll, Bone *bone)
bool ANIM_armature_bonecoll_unassign(BoneCollection *bcoll, Bone *bone)
bool ANIM_armature_bonecoll_assign_editbone(BoneCollection *bcoll, EditBone *ebone)
void BKE_bone_parent_transform_invert(BoneParentTransform *bpt)
Definition armature.cc:2220
void mat3_vec_to_roll(const float mat[3][3], const float vec[3], float *r_roll)
Definition armature.cc:2467
float distfactor_to_bone(const float vec[3], const float b1[3], const float b2[3], float rad1, float rad2, float rdist)
void BKE_bone_parent_transform_apply(const BoneParentTransform *bpt, const float inmat[4][4], float outmat[4][4])
Definition armature.cc:2236
void vec_roll_to_mat3(const float vec[3], float roll, float r_mat[3][3])
Definition armature.cc:2611
void mat3_to_vec_roll(const float mat[3][3], float r_vec[3], float *r_roll)
Definition armature.cc:2456
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)
Definition armature.cc:2068
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
void normalize_m3_m3(float R[3][3], const float M[3][3]) ATTR_NONNULL()
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
bool is_zero_m4(const float mat[4][4])
MINLINE float normalize_v3_v3(float r[3], const float a[3])
@ BONE_MULT_VG_ENV
@ PARM_RNAPTR
Definition RNA_types.hh:399
@ PARM_REQUIRED
Definition RNA_types.hh:397
@ FUNC_USE_REPORTS
Definition RNA_types.hh:680
@ FUNC_NO_SELF
Definition RNA_types.hh:673
@ FUNC_USE_CONTEXT
Definition RNA_types.hh:679
@ PROP_FLOAT
Definition RNA_types.hh:67
PropertyFlag
Definition RNA_types.hh:201
@ PROP_NEVER_NULL
Definition RNA_types.hh:266
@ PROP_MATRIX
Definition RNA_types.hh:168
@ PROP_XYZ
Definition RNA_types.hh:172
@ PROP_NONE
Definition RNA_types.hh:136
#define ND_POSE
Definition WM_types.hh:425
#define ND_BONE_SELECT
Definition WM_types.hh:427
#define NC_OBJECT
Definition WM_types.hh:346
float ED_armature_ebone_roll_to_vector(const EditBone *bone, const float align_axis[3], const bool axis_only)
draw_view in_light_buf[] float
CCL_NAMESPACE_BEGIN ccl_device float invert(float color, float factor)
Definition invert.h:9
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
const char * RNA_struct_identifier(const StructRNA *type)
bool RNA_pointer_is_null(const PointerRNA *ptr)
void RNA_api_armature_edit_bone(StructRNA *srna)
void RNA_api_bone(StructRNA *srna)
void RNA_api_bonecollection(StructRNA *srna)
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
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)
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)
void RNA_def_property_multi_array(PropertyRNA *prop, int dimension, const int length[])
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)
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier, const char *call)
void RNA_def_property_array(PropertyRNA *prop, int length)
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
const int rna_matrix_dimsize_4x4[]
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
void RNA_def_function_flag(FunctionRNA *func, int flag)
const int rna_matrix_dimsize_3x3[]
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
void RNA_def_function_output(FunctionRNA *, PropertyRNA *ret)
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
#define FLT_MAX
Definition stdcycles.h:14
float arm_head[3]
float arm_tail[3]
char inherit_scale_mode
StructRNA * type
Definition RNA_types.hh:41
void * data
Definition RNA_types.hh:42
struct Bone * bone
void WM_event_add_notifier(const bContext *C, uint type, void *reference)