Blender V5.3
visualkey.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdio>
10#include <cstring>
11
12#include "ANIM_rna.hh"
13#include "ANIM_visualkey.hh"
14
15#include "BKE_armature.hh"
16
17#include "BLI_math_matrix_c.hh"
19
21#include "DNA_object_types.h"
22#include "DNA_rigidbody_types.h"
23
24#include "RNA_access.hh"
25#include "RNA_prototypes.hh"
26
27namespace blender::animrig {
28
29/* Internal status codes for visualkey_can_use. */
30enum {
35};
36
38{
39 bConstraint *con = nullptr;
40 bool has_rigidbody = false;
41 bool has_parent = false;
42
43 if (!ptr || !*ptr || !prop) {
44 return false;
45 }
46
47 /* Get first constraint and determine type of keyframe constraints to check for
48 * - constraints can be on either Objects or PoseChannels, so we only check if the
49 * ptr->type is RNA_Object or RNA_PoseBone, which are the RNA wrapping-info for
50 * those structs, allowing us to identify the owner of the data
51 */
52 if (ptr->type == RNA_Object) {
53 Object *ob = static_cast<Object *>(ptr->data);
55
56 con = ob->constraints.first();
57 has_parent = (ob->parent != nullptr);
58
59 /* Active rigidbody objects only, as only those are affected by sim. */
60 has_rigidbody = ((rbo) && (rbo->type == RBO_TYPE_ACTIVE));
61 }
62 else if (ptr->type == RNA_PoseBone) {
63 bPoseChannel *pchan = static_cast<bPoseChannel *>(ptr->data);
64
66 /* Spline IK cannot generally be keyed visually, because (at least with the default
67 * constraint settings) it requires non-uniform scaling that causes shearing in child bones,
68 * which cannot be represented by the bone's loc/rot/scale properties. */
69 return true;
70 }
71
72 con = pchan->constraints.first();
73 has_parent = (pchan->parent != nullptr);
74 }
75 else {
76 BLI_assert_msg(false,
77 "visualkey_can_use called for data-block that is not an Object or PoseBone.");
78 return false;
79 }
80
81 /* Parent or rigidbody are always matching, no need to check further. */
82 if (has_parent || has_rigidbody) {
83 return true;
84 }
85
86 /* Only do visual keying on transforms. */
87 const char *identifier = RNA_property_identifier(prop);
88 if (identifier == nullptr) {
89 printf("%s failed: nullptr identifier\n", __func__);
90 return false;
91 }
92
93 short searchtype = VISUALKEY_NONE;
94 if (strstr(identifier, "location")) {
95 searchtype = VISUALKEY_LOC;
96 }
97 else if (strstr(identifier, "rotation")) {
98 searchtype = VISUALKEY_ROT;
99 }
100 else if (strstr(identifier, "scale")) {
101 searchtype = VISUALKEY_SCA;
102 }
103 else {
104 printf("%s failed: identifier - '%s'\n", __func__, identifier);
105 return false;
106 }
107
108 /* Check constraints. */
109 for (; con; con = con->next) {
110 /* only consider constraint if it is not disabled, and has influence */
111 if (con->flag & CONSTRAINT_DISABLE) {
112 continue;
113 }
114 if (con->enforce == 0.0f) {
115 continue;
116 }
117
118 /* Some constraints may alter these transforms. */
119 switch (con->type) {
120 /* Multi-transform constraints. */
123 return true;
126 return true;
128 return true;
130 return true;
131
132 /* Single-transform constraints. */
134 if (searchtype == VISUALKEY_ROT) {
135 return true;
136 }
137 break;
139 if (searchtype == VISUALKEY_ROT) {
140 return true;
141 }
142 break;
144 if (searchtype == VISUALKEY_ROT) {
145 return true;
146 }
147 break;
149 if (searchtype == VISUALKEY_LOC) {
150 return true;
151 }
152 break;
154 if (searchtype == VISUALKEY_SCA) {
155 return true;
156 }
157 break;
159 if (searchtype == VISUALKEY_LOC) {
160 return true;
161 }
162 break;
164 if (searchtype == VISUALKEY_ROT) {
165 return true;
166 }
167 break;
169 if (searchtype == VISUALKEY_LOC) {
170 return true;
171 }
172 break;
174 if (searchtype == VISUALKEY_SCA) {
175 return true;
176 }
177 break;
179 if (searchtype == VISUALKEY_ROT) {
180 return true;
181 }
182 break;
184 if (searchtype == VISUALKEY_LOC) {
185 return true;
186 }
187 break;
188
189 default:
190 break;
191 }
192 }
193
194 return false;
195}
196
198{
199 Vector<float> values;
200 const char *identifier = RNA_property_identifier(prop);
201 float tmat[4][4];
202 int rotmode;
203
204 /* Handle for Objects or PoseChannels only
205 * - only Location, Rotation or Scale keyframes are supported currently
206 * - constraints can be on either Objects or PoseChannels, so we only check if the
207 * ptr->type is RNA_Object or RNA_PoseBone, which are the RNA wrapping-info for
208 * those structs, allowing us to identify the owner of the data
209 * - assume that array_index will be sane
210 */
211 if (ptr->type == RNA_Object) {
212 Object *ob = static_cast<Object *>(ptr->data);
213 /* Loc code is specific... */
214 if (strstr(identifier, "location")) {
215 values.extend({ob->object_to_world().location(), 3});
216 return values;
217 }
218
219 copy_m4_m4(tmat, ob->object_to_world().ptr());
220 rotmode = ob->rotmode;
221 }
222 else if (ptr->type == RNA_PoseBone) {
223 Object *ob = id_cast<Object *>(ptr->owner_id);
224 bPoseChannel *pchan = static_cast<bPoseChannel *>(ptr->data);
225 Bone *bone = pchan->bone_get(*ob);
226
227 BKE_armature_mat_pose_to_bone({pchan, bone}, pchan->pose_mat, tmat);
228 rotmode = pchan->rotmode;
229
230 /* Loc code is specific... */
231 if (strstr(identifier, "location")) {
232 /* Only use for non-connected bones. */
233 if ((bone->parent == nullptr) || !(bone->flag & BONE_CONNECTED)) {
234 values.extend({tmat[3], 3});
235 return values;
236 }
237 }
238 }
239 else {
240 return get_rna_values(ptr, prop);
241 }
242
243 /* Rot/Scale code are common! */
244 if (strstr(identifier, "rotation_euler")) {
245 values.resize(3);
246 mat4_to_eulO(values.data(), rotmode, tmat);
247 return values;
248 }
249
250 if (strstr(identifier, "rotation_quaternion")) {
251 values.resize(4);
252 mat4_to_quat(values.data(), tmat);
253 return values;
254 }
255
256 if (strstr(identifier, "rotation_axis_angle")) {
257 /* w = 0, x,y,z = 1,2,3 */
258 values.resize(4);
259 mat4_to_axis_angle(values.data() + 1, values.data() + 0, tmat);
260 return values;
261 }
262
263 if (strstr(identifier, "scale")) {
264 values.resize(3);
265 mat4_to_size(values.data(), tmat);
266 return values;
267 }
268
269 /* As the function hasn't returned yet, read value from system in the default way. */
270 return get_rna_values(ptr, prop);
271}
272} // namespace blender::animrig
Helper functions for animation to interact with the RNA system.
Functions to work with the visual keying system.
Object is a sort of wrapper for general info.
Types and defines for representing Rigid Body entities.
#define BLI_assert_msg(a, msg)
void resize(const int64_t new_size)
void extend(Span< T > array)
Vector< float > visualkey_get_values(PointerRNA *ptr, PropertyRNA *prop)
Definition visualkey.cc:197
bool visualkey_can_use(PointerRNA *ptr, PropertyRNA *prop)
Definition visualkey.cc:37
Vector< float > get_rna_values(PointerRNA *ptr, PropertyRNA *prop)
Definition anim_rna.cc:26
void mat4_to_quat(float q[4], const float mat[4][4])
const char * RNA_property_identifier(const PropertyRNA *prop)
void BKE_armature_mat_pose_to_bone(bke::PChanBoneConst pchanbone, const float inmat[4][4], float outmat[4][4])
void mat4_to_eulO(float eul[3], short order, const float m[4][4])
void mat4_to_size(float size[3], const float M[4][4])
void mat4_to_axis_angle(float axis[3], float *angle, const float mat[4][4])
void copy_m4_m4(float m1[4][4], const float m2[4][4])
struct Bone * parent
struct Object * parent
ListBaseT< bConstraint > constraints
struct RigidBodyOb * rigidbody_object
StructRNA * type
Definition RNA_types.hh:63
struct bConstraint * next
struct bPoseChannel * parent
ListBaseT< struct bConstraint > constraints
ePchan_ConstFlag constflag