Blender V5.3
rna_pose_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 "BLI_sys_types.hh" */
14
15#include "RNA_define.hh"
16#include "RNA_types.hh"
17#include "rna_internal.hh" /* own include */
18
19#ifdef RNA_RUNTIME
20
21# include "BKE_animsys.hh"
22# include "BKE_armature.hh"
23# include "BKE_context.hh"
24# include "BKE_pose_backup.h"
25
26# include "DNA_action_types.h"
27# include "DNA_anim_types.h"
28# include "DNA_armature_types.h"
29
30# include "BLI_ghash.hh"
31# include "BLI_math_matrix_c.hh"
32
33# include "ANIM_action.hh"
34# include "ANIM_pose.hh"
35
36# include "DEG_depsgraph.hh"
37
38# include "WM_api.hh"
39
40namespace blender {
41
42static float rna_PoseBone_do_envelope(ID *self, bPoseChannel *chan, const float vec[3])
43{
44 Bone *bone = chan->bone_get(id_cast<Object &>(*self));
45
46 float scale = (bone->flag & BONE_MULT_VG_ENV) == BONE_MULT_VG_ENV ? bone->weight : 1.0f;
47
48 return distfactor_to_bone(vec,
49 chan->pose_head,
50 chan->pose_tail,
51 bone->rad_head * scale,
52 bone->rad_tail * scale,
53 bone->dist * scale);
54}
55
56static void rna_PoseBone_bbone_segment_index(ID *self,
57 bPoseChannel *pchan,
58 ReportList *reports,
59 const float pt[3],
60 int *r_index,
61 float *r_blend_next)
62{
63 Bone *bone = pchan->bone_get(id_cast<Object &>(*self));
64 if (!bone || bone->segments <= 1) {
65 BKE_reportf(reports, RPT_ERROR, "Bone '%s' is not a B-Bone!", pchan->name);
66 return;
67 }
68 if (pchan->runtime.bbone_segments != bone->segments) {
69 BKE_reportf(reports,
71 "Bone '%s' has out of date B-Bone segment data - depsgraph update required!",
72 pchan->name);
73 return;
74 }
75
76 BKE_pchan_bbone_deform_segment_index({pchan, bone}, pt, r_index, r_blend_next);
77}
78
79static void rna_PoseBone_bbone_segment_matrix(
80 ID *self, bPoseChannel *pchan, ReportList *reports, float mat_ret[16], int index, bool rest)
81{
82 Bone *bone = pchan->bone_get(id_cast<Object &>(*self));
83 if (!bone || bone->segments <= 1) {
84 BKE_reportf(reports, RPT_ERROR, "Bone '%s' is not a B-Bone!", pchan->name);
85 return;
86 }
87 if (pchan->runtime.bbone_segments != bone->segments) {
88 BKE_reportf(reports,
90 "Bone '%s' has out of date B-Bone segment data - depsgraph update required!",
91 pchan->name);
92 return;
93 }
94 if (index < 0 || index > pchan->runtime.bbone_segments) {
96 reports, RPT_ERROR, "Invalid index %d for B-Bone segments of '%s'!", index, pchan->name);
97 return;
98 }
99
100 if (rest) {
101 copy_m4_m4(reinterpret_cast<float (*)[4]>(mat_ret), pchan->runtime.bbone_rest_mats[index].mat);
102 }
103 else {
104 copy_m4_m4(reinterpret_cast<float (*)[4]>(mat_ret), pchan->runtime.bbone_pose_mats[index].mat);
105 }
106}
107
108static void rna_PoseBone_compute_bbone_handles(ID *self,
109 bPoseChannel *pchan,
110 ReportList *reports,
111 float ret_h1[3],
112 float *ret_roll1,
113 float ret_h2[3],
114 float *ret_roll2,
115 bool rest,
116 bool ease,
117 bool offsets)
118{
119 Object *ob = id_cast<Object *>(self);
120 bArmature &armature = id_cast<bArmature &>(*ob->data);
121
122 Bone *bone = pchan->bone_get(*ob);
123 if (!bone || bone->segments <= 1) {
124 BKE_reportf(reports, RPT_ERROR, "Bone '%s' is not a B-Bone!", pchan->name);
125 return;
126 }
127
129
130 BKE_pchan_bbone_spline_params_get({pchan, bone}, armature, rest, &params);
132 &params, ret_h1, ret_roll1, ret_h2, ret_roll2, ease || offsets, offsets);
133}
134
135static void rna_Pose_apply_pose_from_action(ID *pose_owner,
136 bContext *C,
137 bAction *action,
138 const float evaluation_time)
139{
140 BLI_assert(GS(pose_owner->name) == ID_OB);
141 Object *pose_owner_ob = id_cast<Object *>(pose_owner);
142
143 AnimationEvalContext anim_eval_context = {CTX_data_depsgraph_pointer(C), evaluation_time};
144 animrig::pose_apply_action({pose_owner_ob}, action->wrap(), &anim_eval_context, 1.0);
145
146 /* Do NOT tag with ID_RECALC_ANIMATION, as that would overwrite the just-applied pose. */
149}
150
151static void rna_Pose_blend_pose_from_action(ID *pose_owner,
152 bContext *C,
153 bAction *action,
154 const float blend_factor,
155 const float evaluation_time)
156{
157 BLI_assert(GS(pose_owner->name) == ID_OB);
158 Object *pose_owner_ob = id_cast<Object *>(pose_owner);
159
160 AnimationEvalContext anim_eval_context = {CTX_data_depsgraph_pointer(C), evaluation_time};
161 animrig::pose_apply_action({pose_owner_ob}, action->wrap(), &anim_eval_context, blend_factor);
162
163 /* Do NOT tag with ID_RECALC_ANIMATION, as that would overwrite the just-applied pose. */
166}
167
168static void rna_Pose_backup_create(ID *pose_owner, bAction *action)
169{
170 BLI_assert(GS(pose_owner->name) == ID_OB);
171 if (!action || action->wrap().slot_array_num == 0) {
172 /* A pose asset without slots has no data, this usually doesn't happen but can happen by
173 * tagging an empty action as a pose asset. */
174 return;
175 }
176 Object *pose_owner_ob = id_cast<Object *>(pose_owner);
177 BKE_pose_backup_create_on_object(pose_owner_ob, action);
178}
179
180static bool rna_Pose_backup_restore(ID *pose_owner, bContext *C)
181{
182 BLI_assert(GS(pose_owner->name) == ID_OB);
183 Object *pose_owner_ob = id_cast<Object *>(pose_owner);
184
185 const bool success = BKE_pose_backup_restore_on_object(pose_owner_ob);
186 if (!success) {
187 return false;
188 }
189
190 /* Do NOT tag with ID_RECALC_ANIMATION, as that would overwrite the just-applied pose. */
193
194 return true;
195}
196
197static void rna_Pose_backup_clear(ID *pose_owner)
198{
199 BLI_assert(GS(pose_owner->name) == ID_OB);
200 Object *pose_owner_ob = id_cast<Object *>(pose_owner);
201
202 BKE_pose_backup_clear(pose_owner_ob);
203}
204
205} // namespace blender
206
207#else
208
209namespace blender {
210
212{
213 FunctionRNA *func;
214 PropertyRNA *parm;
215
216 func = RNA_def_function(srna, "apply_pose_from_action", "rna_Pose_apply_pose_from_action");
219 func,
220 "Apply the given action to this pose by evaluating it at a specific time. Only updates the "
221 "pose of selected bones, or all bones if none are selected.");
222 parm = RNA_def_pointer(func, "action", "Action", "Action", "The Action containing the pose");
224 parm = RNA_def_float(func,
225 "evaluation_time",
226 0.0f,
227 -FLT_MAX,
228 FLT_MAX,
229 "Evaluation Time",
230 "Time at which the given action is evaluated to obtain the pose",
231 -FLT_MAX,
232 FLT_MAX);
233
234 func = RNA_def_function(srna, "blend_pose_from_action", "rna_Pose_blend_pose_from_action");
237 "Blend the given action into this pose by evaluating it at a "
238 "specific time. Only updates the "
239 "pose of selected bones, or all bones if none are selected.");
240 parm = RNA_def_pointer(func, "action", "Action", "Action", "The Action containing the pose");
242 RNA_def_float(func,
243 "blend_factor",
244 1.0f,
245 0.0f,
246 1.0f,
247 "Blend Factor",
248 "How much the given Action affects the final pose",
249 0.0f,
250 1.0f);
251 RNA_def_float(func,
252 "evaluation_time",
253 0.0f,
254 -FLT_MAX,
255 FLT_MAX,
256 "Evaluation Time",
257 "Time at which the given action is evaluated to obtain the pose",
258 -FLT_MAX,
259 FLT_MAX);
260
261 func = RNA_def_function(srna, "backup_create", "rna_Pose_backup_create");
263 func,
264 "Create a backup of the current pose. Only those bones that are animated in the Action are "
265 "backed up. The object owns the backup, and each object can have only one backup at a time. "
266 "When you no longer need it, it must be freed use ``backup_clear()``.");
268 parm = RNA_def_pointer(func,
269 "action",
270 "Action",
271 "Action",
272 "An Action with animation data for the bones. "
273 "Only the animated bones will be included in the backup.");
275
276 func = RNA_def_function(srna, "backup_restore", "rna_Pose_backup_restore");
279 func,
280 "Restore the previously made pose backup. "
281 "This can be called multiple times. See ``Pose.backup_create()`` for more info.");
282 /* return value */
283 parm = RNA_def_boolean(
284 func,
285 "success",
286 false,
287 "",
288 "``True`` when the backup was restored, ``False`` if there was no backup to restore");
289 RNA_def_function_return(func, parm);
290
291 func = RNA_def_function(srna, "backup_clear", "rna_Pose_backup_clear");
293 func, "Free a previously made pose backup. See ``Pose.backup_create()`` for more info.");
295}
296
298{
299 PropertyRNA *parm;
300 FunctionRNA *func;
301
302 func = RNA_def_function(srna, "evaluate_envelope", "rna_PoseBone_do_envelope");
304 RNA_def_function_ui_description(func, "Calculate bone envelope at given point");
305 parm = RNA_def_float_vector_xyz(func,
306 "point",
307 3,
308 nullptr,
309 -FLT_MAX,
310 FLT_MAX,
311 "Point",
312 "Position in 3d space to evaluate",
313 -FLT_MAX,
314 FLT_MAX);
316 /* return value */
317 parm = RNA_def_float(
318 func, "factor", 0, -FLT_MAX, FLT_MAX, "Factor", "Envelope factor", -FLT_MAX, FLT_MAX);
319 RNA_def_function_return(func, parm);
320
321 /* B-Bone segment index from point */
322 func = RNA_def_function(srna, "bbone_segment_index", "rna_PoseBone_bbone_segment_index");
325 func, "Retrieve the index and blend factor of the B-Bone segments based on vertex position");
326 parm = RNA_def_float_vector_xyz(func,
327 "point",
328 3,
329 nullptr,
330 -FLT_MAX,
331 FLT_MAX,
332 "Point",
333 "Vertex position in armature pose space",
334 -FLT_MAX,
335 FLT_MAX);
337 /* outputs */
338 parm = RNA_def_property(func, "index", PROP_INT, PROP_NONE);
339 RNA_def_property_ui_text(parm, "", "The index of the first segment joint affecting the point");
340 RNA_def_function_output(func, parm);
341 parm = RNA_def_property(func, "blend_next", PROP_FLOAT, PROP_NONE);
342 RNA_def_property_ui_text(parm, "", "The blend factor between the given and the following joint");
343 RNA_def_function_output(func, parm);
344
345 /* B-Bone segment matrices */
346 func = RNA_def_function(srna, "bbone_segment_matrix", "rna_PoseBone_bbone_segment_matrix");
348 func, "Retrieve the matrix of the joint between B-Bone segments if available");
350 parm = RNA_def_property(func, "matrix_return", PROP_FLOAT, PROP_MATRIX);
352 RNA_def_property_ui_text(parm, "", "The resulting matrix in bone local space");
353 RNA_def_function_output(func, parm);
354 parm = RNA_def_int(func, "index", 0, 0, INT_MAX, "", "Index of the segment endpoint", 0, 10000);
356 parm = RNA_def_boolean(func, "rest", false, "", "Return the rest pose matrix");
357
358 /* B-Bone custom handle positions */
359 func = RNA_def_function(srna, "compute_bbone_handles", "rna_PoseBone_compute_bbone_handles");
361 func, "Retrieve the vectors and rolls coming from B-Bone custom handles");
363 parm = RNA_def_property(func, "handle1", PROP_FLOAT, PROP_XYZ);
364 RNA_def_property_array(parm, 3);
366 parm, "", "The direction vector of the start handle in bone local space");
367 RNA_def_function_output(func, parm);
368 parm = RNA_def_float(
369 func, "roll1", 0, -FLT_MAX, FLT_MAX, "", "Roll of the start handle", -FLT_MAX, FLT_MAX);
370 RNA_def_function_output(func, parm);
371 parm = RNA_def_property(func, "handle2", PROP_FLOAT, PROP_XYZ);
372 RNA_def_property_array(parm, 3);
373 RNA_def_property_ui_text(parm, "", "The direction vector of the end handle in bone local space");
374 RNA_def_function_output(func, parm);
375 parm = RNA_def_float(
376 func, "roll2", 0, -FLT_MAX, FLT_MAX, "", "Roll of the end handle", -FLT_MAX, FLT_MAX);
377 RNA_def_function_output(func, parm);
378 parm = RNA_def_boolean(func, "rest", false, "", "Return the rest pose state");
379 parm = RNA_def_boolean(func, "ease", false, "", "Apply scale from ease values");
380 parm = RNA_def_boolean(
381 func, "offsets", false, "", "Apply roll and curve offsets from bone properties");
382}
383
384} // namespace blender
385
386#endif
Functions and classes to work with Actions.
Functions to work with animation poses.
#define C
Definition RandGen.cpp:29
#define ND_POSE
Definition WM_types.hh:482
#define NC_OBJECT
Definition WM_types.hh:392
#define FLT_MAX
#define INT_MAX
float3x3 scale(float3x3 mat, float2 scale)
void pose_apply_action(Span< Object * > objects, Action &pose_action, const AnimationEvalContext *anim_eval_context, float blend_factor)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
void RNA_def_function_output(FunctionRNA *, PropertyRNA *ret)
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
void RNA_def_property_multi_array(PropertyRNA *prop, int dimension, const int length[])
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
@ FUNC_USE_REPORTS
@ FUNC_USE_SELF_ID
Definition RNA_types.hh:980
@ 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 RNA_api_pose_channel(StructRNA *srna)
float distfactor_to_bone(const float3 &position, const float3 &head, const float3 &tail, float radius_head, float radius_tail, float falloff_distance)
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier_c_str, int type, int subtype)
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
void BKE_pchan_bbone_deform_segment_index(bke::PChanBoneConst pchanbone, const float *co, int *r_index, float *r_blend_next)
void DEG_id_tag_update(ID *id, unsigned int flags)
void RNA_api_pose(StructRNA *srna)
bool BKE_pose_backup_restore_on_object(struct Object *ob)
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
void BKE_pchan_bbone_spline_params_get(bke::PChanBoneConst pchanbone, const bArmature &armature, bool rest, BBoneSplineParameters *param)
void BKE_pose_backup_create_on_object(struct Object *ob, const struct bAction *action)
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)
void BKE_pchan_bbone_handles_compute(const BBoneSplineParameters *param, float h1[3], float *r_roll1, float h2[3], float *r_roll2, bool ease, bool offsets)
void RNA_def_function_flag(FunctionRNA *func, FunctionFlag flag)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
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 BKE_pose_backup_clear(struct Object *ob)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
ID_Type GS(const char *name)
Definition DNA_ID.h:410
void copy_m4_m4(float m1[4][4], const float m2[4][4])
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
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[]