Blender V5.3
anim_rna.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 <fmt/format.h>
10
11#include "ANIM_rna.hh"
12
13#include "BLI_listbase.hh"
14#include "BLI_math_base_c.hh"
15#include "BLI_string.hh"
16#include "BLI_vector.hh"
17
18#include "DNA_object_types.h"
19
20#include "RNA_access.hh"
21#include "RNA_path.hh"
22#include "RNA_prototypes.hh"
23
24namespace blender::animrig {
25
27{
28 Vector<float> values;
29 if (RNA_property_array_check(prop)) {
30 const int length = RNA_property_array_length(ptr, prop);
31
32 switch (RNA_property_type(prop)) {
33 case PROP_BOOLEAN: {
34 bool *tmp_bool = MEM_new_array_uninitialized<bool>(length, __func__);
35 RNA_property_boolean_get_array(ptr, prop, tmp_bool);
36 for (int i = 0; i < length; i++) {
37 values.append(float(tmp_bool[i]));
38 }
39 MEM_delete(tmp_bool);
40 break;
41 }
42 case PROP_INT: {
43 int *tmp_int = MEM_new_array_uninitialized<int>(length, __func__);
44 RNA_property_int_get_array(ptr, prop, tmp_int);
45 for (int i = 0; i < length; i++) {
46 values.append(float(tmp_int[i]));
47 }
48 MEM_delete(tmp_int);
49 break;
50 }
51 case PROP_FLOAT: {
52 values.reinitialize(length);
53 RNA_property_float_get_array(ptr, prop, values.data());
54 break;
55 }
56 default:
57 values.reinitialize(length);
58 break;
59 }
60 }
61 else {
62 switch (RNA_property_type(prop)) {
63 case PROP_BOOLEAN:
64 values.append(float(RNA_property_boolean_get(ptr, prop)));
65 break;
66 case PROP_INT:
67 values.append(float(RNA_property_int_get(ptr, prop)));
68 break;
69 case PROP_FLOAT:
70 values.append(RNA_property_float_get(ptr, prop));
71 break;
72 case PROP_ENUM:
73 values.append(float(RNA_property_enum_get(ptr, prop)));
74 break;
75 default:
76 values.append(0.0f);
77 }
78 }
79
80 return values;
81}
82
83constexpr const char *pose_bone_path_prefix = "pose.bones[\"";
84
85std::string get_pose_bone_rna_path(const bPoseChannel &pose_bone)
86{
87 char name_esc[sizeof(pose_bone.name) * 2];
88 BLI_str_escape(name_esc, pose_bone.name, sizeof(name_esc));
89 return fmt::format("{}{}\"]", pose_bone_path_prefix, name_esc);
90}
91
93{
94 /* The path should have at least three elements, "pose", "bones" and a bone name. */
95 if (rna_path.size() < 3) {
96 return std::nullopt;
97 }
98 const rna_path::Member *pose = std::get_if<rna_path::Member>(&rna_path.first());
99 const rna_path::Member *bones = std::get_if<rna_path::Member>(&rna_path[1]);
100 const rna_path::LookupKey *bone_name = std::get_if<rna_path::LookupKey>(&rna_path[2]);
101 if (!pose || pose->identifier != "pose"_ustr || !bones || bones->identifier != "bones"_ustr ||
102 !bone_name || bone_name->key.size() >= MAXBONENAME)
103 {
104 return std::nullopt;
105 }
106 return bone_name->key.string();
107}
108
110{
111 switch (rotation_mode) {
112 case ROT_MODE_QUAT:
113 return "rotation_quaternion";
115 return "rotation_axis_angle";
116 default:
117 return "rotation_euler";
118 }
119}
120
121std::optional<eRotationModes> get_rotation_mode_from_path(const ParsedRNAPathRef rna_path)
122{
123 /* Accounting for the difference between objects and bones where the latter is e.g.
124 * `pose.bones["foo"].rotation_euler`: the property name is always the last item of the
125 * path, regardless of what precedes it. */
126 if (rna_path.is_empty()) {
127 return std::nullopt;
128 }
129 const rna_path::Member *property = std::get_if<rna_path::Member>(&rna_path.last());
130 if (!property) {
131 return std::nullopt;
132 }
133 const StringRefNull propname = property->identifier.ref();
134 if (!propname.startswith("rotation_")) {
135 return std::nullopt;
136 }
137 /* We already know that "rotation_" is in the property name, we can skip the full check for
138 * "rotation_quaternion", "rotation_euler" or "rotation_axis_angle". */
139 if (propname.endswith("quaternion")) {
140 return ROT_MODE_QUAT;
141 }
142 if (propname.endswith("euler")) {
143 /* Cannot determine the rotation order from the path alone. */
144 return ROT_MODE_EUL;
145 }
146 if (propname.endswith("axis_angle")) {
147 return ROT_MODE_AXISANGLE;
148 }
149 return std::nullopt;
150}
151
152std::optional<eRotationModes> get_rotation_mode_from_rna_pointer(const PointerRNA &ptr)
153{
154 if (ptr.type == RNA_PoseBone) {
155 bPoseChannel *pchan = static_cast<bPoseChannel *>(ptr.data);
156 return eRotationModes(pchan->rotmode);
157 }
158 if (ptr.type == RNA_Object) {
159 Object *ob = static_cast<Object *>(ptr.data);
160 return eRotationModes(ob->rotmode);
161 }
162 return std::nullopt;
163}
164
169
170static bool is_idproperty_keyable(const IDProperty *id_prop, PointerRNA *ptr, PropertyRNA *prop)
171{
172 /* While you can cast the IDProperty* to a PropertyRNA* and pass it to the RNA_* functions, this
173 * does not work because it will not have the right flags set. Instead the resolved
174 * PointerRNA and PropertyRNA need to be passed. */
175 if (!RNA_property_anim_editable(ptr, prop)) {
176 return false;
177 }
178
179 if (ELEM(id_prop->type,
184 {
185 return true;
186 }
187
188 if (id_prop->type == eIDPropertyType::IDP_ARRAY) {
189 if (ELEM(id_prop->subtype,
194 {
195 return true;
196 }
197 }
198
199 return false;
200}
201
203{
204 IDProperty *properties;
205
206 if (ptr.type == RNA_PoseBone) {
207 const bPoseChannel *pchan = static_cast<bPoseChannel *>(ptr.data);
208 properties = pchan->prop;
209 }
210 else if (ptr.type == RNA_Object) {
211 const Object *ob = static_cast<Object *>(ptr.data);
212 properties = ob->id.properties;
213 }
214 else {
215 /* Pointer type not supported. */
216 return {};
217 }
218
219 if (!properties) {
220 return {};
221 }
222
223 Vector<RNAPath> paths;
224 for (const IDProperty &id_prop : properties->data.group) {
225 PointerRNA resolved_ptr;
226 PropertyRNA *resolved_prop;
227 std::string path = id_prop.name;
228 /* Resolving the path twice, once as RNA property (without brackets, `"propname"`),
229 * and once as ID property (with brackets, `["propname"]`).
230 * This is required to support IDProperties that have been defined as part of an add-on.
231 * Those need to be animated through an RNA path without the brackets. */
232 bool is_resolved = RNA_path_resolve_property(
233 &ptr, path.c_str(), &resolved_ptr, &resolved_prop);
234 /* ID properties can be named the same as internal properties, for example `scale`. In that
235 * case they would resolve, but it wouldn't be the correct property. `RNA_property_is_runtime`
236 * catches that case. */
237 if (!is_resolved || !RNA_property_is_runtime(resolved_prop)) {
238 char name_escaped[MAX_IDPROP_NAME * 2];
239 BLI_str_escape(name_escaped, id_prop.name, sizeof(name_escaped));
240 path = fmt::format("[\"{}\"]", name_escaped);
241 is_resolved = RNA_path_resolve_property(&ptr, path.c_str(), &resolved_ptr, &resolved_prop);
242 }
243 if (!is_resolved) {
244 continue;
245 }
246 if (is_idproperty_keyable(&id_prop, &resolved_ptr, resolved_prop)) {
247 paths.append({path});
248 }
249 }
250 return paths;
251}
252
254{
255 const bool is_array = RNA_property_array_check(&prop);
256 Array<float> values;
257 if (is_array) {
258 values.reinitialize(RNA_property_array_length(&ptr, &prop));
259 }
260 else {
261 values.reinitialize(1);
262 }
263 switch (RNA_property_type(&prop)) {
264 case PROP_BOOLEAN:
265 if (is_array) {
266 for (const int i : values.index_range()) {
267 values[i] = RNA_property_boolean_get_index(&ptr, &prop, i);
268 }
269 }
270 else {
271 values[0] = RNA_property_boolean_get(&ptr, &prop);
272 }
273 break;
274
275 case PROP_INT:
276 if (is_array) {
277 for (const int i : values.index_range()) {
278 values[i] = RNA_property_int_get_index(&ptr, &prop, i);
279 }
280 }
281 else {
282 values[0] = RNA_property_int_get(&ptr, &prop);
283 }
284 break;
285
286 case PROP_FLOAT:
287 if (is_array) {
288 RNA_property_float_get_array(&ptr, &prop, values.data());
289 }
290 else {
291 values[0] = RNA_property_float_get(&ptr, &prop);
292 }
293 break;
294 default:
295 /* Unsupported property type. */
296 return {};
297 }
298 return values;
299}
300
302{
303 const bool is_array = RNA_property_array_check(&prop);
304 if (is_array && RNA_property_array_length(&ptr, &prop) != values.size()) {
305 /* Array length has to match. */
307 return;
308 }
309
310 switch (RNA_property_type(&prop)) {
311 case PROP_BOOLEAN:
312 if (is_array) {
313 for (const int i : values.index_range()) {
314 RNA_property_boolean_set_index(&ptr, &prop, i, values[i]);
315 }
316 }
317 else {
318 RNA_property_boolean_set(&ptr, &prop, values[0]);
319 }
320 break;
321 case PROP_INT:
322 if (is_array) {
323 for (const int i : values.index_range()) {
324 RNA_property_int_set_index(&ptr, &prop, i, values[i]);
325 }
326 }
327 else {
328 RNA_property_int_set(&ptr, &prop, values[0]);
329 }
330 break;
331 case PROP_FLOAT:
332 if (is_array) {
333 RNA_property_float_set_array(&ptr, &prop, values.data());
334 }
335 else {
336 RNA_property_float_set(&ptr, &prop, values[0]);
337 }
338 break;
339 default:
340 /* Unsupported property type. */
342 return;
343 }
344}
345
346} // namespace blender::animrig
Helper functions for animation to interact with the RNA system.
#define MAX_IDPROP_NAME
Definition DNA_ID.h:174
#define MAXBONENAME
Object is a sort of wrapper for general info.
#define BLI_assert_unreachable()
#define ELEM(...)
const T * data() const
Definition BLI_array.hh:323
IndexRange index_range() const
Definition BLI_array.hh:371
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
const std::string & string() const
int64_t size() const
void append(const T &value)
void reinitialize(const int64_t new_size)
constexpr bool startswith(StringRef prefix) const
constexpr bool endswith(StringRef suffix) const
float length(VecOp< float, D >) RET
std::optional< eRotationModes > get_rotation_mode_from_path(ParsedRNAPathRef rna_path)
Definition anim_rna.cc:121
StringRefNull get_rotation_mode_path(eRotationModes rotation_mode)
Definition anim_rna.cc:109
bool is_rotation_path(ParsedRNAPathRef rna_path)
Definition anim_rna.cc:165
static bool is_idproperty_keyable(const IDProperty *id_prop, PointerRNA *ptr, PropertyRNA *prop)
Definition anim_rna.cc:170
void rna_property_set_as_float(PointerRNA &ptr, PropertyRNA &prop, Span< float > values)
Definition anim_rna.cc:301
Vector< RNAPath > get_keyable_id_property_paths(const PointerRNA &ptr)
Definition anim_rna.cc:202
Vector< float > get_rna_values(PointerRNA *ptr, PropertyRNA *prop)
Definition anim_rna.cc:26
std::optional< eRotationModes > get_rotation_mode_from_rna_pointer(const PointerRNA &ptr)
Definition anim_rna.cc:152
std::string get_pose_bone_rna_path(const bPoseChannel &pose_bone)
Definition anim_rna.cc:85
std::optional< std::string > pose_bone_name_from_rna_path(ParsedRNAPathRef rna_path)
Definition anim_rna.cc:92
Array< float > rna_property_get_as_float(PointerRNA &ptr, PropertyRNA &prop)
Definition anim_rna.cc:253
constexpr const char * pose_bone_path_prefix
Definition anim_rna.cc:83
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
void RNA_property_float_get_array(PointerRNA *ptr, PropertyRNA *prop, float *values)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void RNA_property_float_set(PointerRNA *ptr, PropertyRNA *prop, float value)
void RNA_property_boolean_get_array(PointerRNA *ptr, PropertyRNA *prop, bool *values)
int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop)
void RNA_property_float_set_array(PointerRNA *ptr, PropertyRNA *prop, const float *values)
Span< rna_path::Item > ParsedRNAPathRef
Definition RNA_path.hh:113
bool RNA_property_boolean_get(PointerRNA *ptr, PropertyRNA *prop)
bool RNA_property_array_check(PropertyRNA *prop)
void RNA_property_int_set(PointerRNA *ptr, PropertyRNA *prop, int value)
PropertyType RNA_property_type(PropertyRNA *prop)
bool RNA_property_is_runtime(const PropertyRNA *prop)
bool RNA_property_anim_editable(const PointerRNA *ptr, PropertyRNA *prop_orig)
@ PROP_BOOLEAN
Definition RNA_types.hh:210
void RNA_property_int_get_array(PointerRNA *ptr, PropertyRNA *prop, int *values)
bool RNA_property_boolean_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
char char size_t BLI_str_escape(char *__restrict dst, StringRef src, size_t dst_maxncpy) ATTR_NONNULL(1)
float RNA_property_float_get(PointerRNA *ptr, PropertyRNA *prop)
void RNA_property_boolean_set(PointerRNA *ptr, PropertyRNA *prop, bool value)
void RNA_property_int_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, int value)
void RNA_property_boolean_set_index(PointerRNA *ptr, PropertyRNA *prop, int index, bool value)
int RNA_property_int_get(PointerRNA *ptr, PropertyRNA *prop)
bool RNA_path_resolve_property(const PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop)
Definition rna_path.cc:578
int RNA_property_int_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
ListBaseT< struct IDProperty > group
Definition DNA_ID.h:133
eIDPropertyType type
Definition DNA_ID.h:145
IDPropertyData data
Definition DNA_ID.h:157
IDProperty * properties
Definition DNA_ID.h:481
StructRNA * type
Definition RNA_types.hh:63