Blender V5.3
anim_ipo_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9/* This file contains code for presenting F-Curves and other animation data
10 * in the UI (especially for use in the Animation Editors).
11 *
12 * -- Joshua Leung, Dec 2008
13 */
14
15#include "MEM_guardedalloc.h"
16
17#include "BLI_math_color_c.hh"
18#include "BLI_string.hh"
19#include "BLI_string_utf8.hh"
20
21#include "BLT_translation.hh"
22
23#include "DNA_anim_types.h"
24#include "DNA_modifier_types.h"
25#include "DNA_node_types.h"
26
27#include "BKE_node.hh"
28#include "BKE_node_runtime.hh"
29
30#include "RNA_access.hh"
31#include "RNA_path.hh"
32#include "RNA_prototypes.hh"
33
34#include "ED_anim_api.hh"
35
36#include "ANIM_action.hh"
37
38#include <fmt/format.h>
39
40#include <cstring>
41
42namespace blender {
43
44struct StructRNA;
45
46/* ----------------------- Getter functions ----------------------- */
47
48std::optional<int> getname_anim_fcurve(char *name, ID *id, FCurve *fcu)
49{
50 /* Could make an argument, it's a documented limit at the moment. */
51 constexpr size_t name_maxncpy = 256;
52
53 /* Handle some nullptr cases. */
54 if (name == nullptr) {
55 /* A 'get name' function should be able to get the name, otherwise it's a bug. */
57 return {};
58 }
59 if (fcu == nullptr) {
60 BLI_strncpy_utf8(name, RPT_("<invalid>"), name_maxncpy);
61 return {};
62 }
63 const StringRefNull rna_path = fcu->rna_path();
64 if (rna_path.is_empty()) {
65 BLI_strncpy_utf8(name, RPT_("<no path>"), name_maxncpy);
66 return {};
67 }
68 if (id == nullptr) {
69 BLI_snprintf_utf8(name, name_maxncpy, "%s[%d]", rna_path.c_str(), fcu->array_index);
70 return {};
71 }
72
74
75 PointerRNA ptr;
76 PropertyRNA *prop;
77
78 if (!RNA_path_resolve_property(&id_ptr, rna_path.c_str(), &ptr, &prop)) {
79 /* Could not resolve the path, so just use the path itself as 'name'. */
80 BLI_snprintf_utf8(name, name_maxncpy, "\"%s[%d]\"", rna_path.c_str(), fcu->array_index);
81
82 /* Tag F-Curve as disabled - as not usable path. */
83 fcu->flag |= FCURVE_DISABLED;
84 return {};
85 }
86
87 const char *structname = nullptr, *propname = nullptr;
88 char arrayindbuf[16];
89 const char *arrayname = nullptr;
90 bool free_structname = false;
91
92 /* For now, name will consist of 3 parts: struct-name, property name, array index
93 * There are several options possible:
94 * 1) <struct-name>.<property-name>.<array-index>
95 * i.e. Bone1.Location.X, or Object.Location.X
96 * 2) <array-index> <property-name> (<struct name>)
97 * i.e. X Location (Bone1), or X Location (Object)
98 *
99 * Currently, option 2 is in use, to try and make it easier to quickly identify F-Curves
100 * (it does have problems with looking rather odd though).
101 * Option 1 is better in terms of revealing a consistent sense of hierarchy though,
102 * which isn't so clear with option 2.
103 */
104
105 /* For struct-name:
106 * - As base, we use a custom name from the structs if one is available
107 * - However, if we're showing sub-data of bones
108 * (probably there will be other exceptions later).
109 * need to include that info too since it gets confusing otherwise.
110 * - If a pointer just refers to the ID-block, then don't repeat this info
111 * since this just introduces clutter.
112 */
113
114 char pchanName[name_maxncpy], constName[name_maxncpy];
115 if (BLI_str_quoted_substr(rna_path.c_str(), "bones[", pchanName, sizeof(pchanName)) &&
116 BLI_str_quoted_substr(rna_path.c_str(), "constraints[", constName, sizeof(constName)))
117 {
118 structname = BLI_sprintfN("%s : %s", pchanName, constName);
119 free_structname = true;
120 }
121 else if (ptr.data != ptr.owner_id) {
123 if (nameprop) {
124 structname = RNA_property_string_get_alloc(&ptr, nameprop, nullptr, 0, nullptr);
125 free_structname = true;
126 }
127 else {
128 structname = RNA_struct_ui_name(ptr.type);
129 }
130
131 /* For the sequencer, a strip's 'Transform' or 'Crop' is a nested (under Strip)
132 * struct, but displaying the struct name alone is no meaningful information
133 * (and also cannot be filtered well), same for modifiers.
134 * So display strip name alongside as well. */
135 if (GS(ptr.owner_id->name) == ID_SCE) {
136 char stripname[name_maxncpy];
138 rna_path.c_str(), "sequence_editor.strips_all[", stripname, sizeof(stripname)))
139 {
140 if (strstr(rna_path.c_str(), ".transform.") || strstr(rna_path.c_str(), ".crop.") ||
141 strstr(rna_path.c_str(), ".modifiers["))
142 {
143 const char *structname_all = BLI_sprintfN("%s : %s", stripname, structname);
144 if (free_structname) {
145 MEM_delete(structname);
146 }
147 structname = structname_all;
148 free_structname = true;
149 }
150 }
151 }
152
153 if (RNA_struct_is_a(ptr.type, RNA_NodeSocket)) {
154 /* Display the name/label of a node socket's node to allow distinguishing multiple nodes. */
155 BLI_assert(GS(ptr.owner_id->name) == ID_NT);
156 const bNodeTree *ntree = reinterpret_cast<const bNodeTree *>(ptr.owner_id);
157 const bNodeSocket *socket = static_cast<const bNodeSocket *>(ptr.data);
158 const bNode &node = bke::node_find_node(*ntree, *socket);
159 if (free_structname) {
160 MEM_delete(structname);
161 }
162 structname = node.label_or_name().c_str();
163 free_structname = false;
164 }
165 else if (RNA_struct_is_a(ptr.type, RNA_Node)) {
166 /* Display the label of the node if available to distinguish nodes like "Value". */
167 BLI_assert(GS(ptr.owner_id->name) == ID_NT);
168 const bNode *node = static_cast<const bNode *>(ptr.data);
169 if (free_structname) {
170 MEM_delete(structname);
171 }
172 structname = node->label_or_name().c_str();
173 free_structname = false;
174 }
175 }
176
177 propname = RNA_property_ui_name(prop);
178
179 if (RNA_struct_is_a(ptr.type, RNA_NodesModifier)) {
180 /* Display geometry node properties with node-tree socket labels. */
181 const NodesModifierData *nmd = static_cast<const NodesModifierData *>(ptr.data);
182 if (nmd->node_group && !ID_MISSING(nmd->node_group)) {
184 *nmd->node_group, propname))
185 {
186 propname = input->name().c_str();
187 }
188 }
189 }
190 else if (RNA_struct_is_a(ptr.type, RNA_NodeSocket)) {
191 /* Use the socket's name rather than the "Default Value" name of the socket's RNA property. */
192 const bNodeSocket *socket = static_cast<const bNodeSocket *>(ptr.data);
193 propname = socket->name;
194 }
195
196 /* Array Index - only if applicable */
197 if (RNA_property_array_check(prop)) {
198 char c = RNA_property_array_item_char(prop, fcu->array_index);
199
200 /* we need to write the index to a temp buffer (in py syntax) */
201 if (c) {
202 SNPRINTF_UTF8(arrayindbuf, "%c ", c);
203 }
204 else {
205 SNPRINTF_UTF8(arrayindbuf, "[%d]", fcu->array_index);
206 }
207
208 arrayname = &arrayindbuf[0];
209 }
210 else {
211 /* no array index */
212 arrayname = "";
213 }
214
215 /* putting this all together into the buffer */
216 /* XXX we need to check for invalid names...
217 * XXX the name length limit needs to be passed in or as some define */
218 if (structname) {
219 BLI_snprintf_utf8(name, name_maxncpy, "%s%s (%s)", arrayname, propname, structname);
220 }
221 else {
222 BLI_snprintf_utf8(name, name_maxncpy, "%s%s", arrayname, propname);
223 }
224
225 /* free temp name if nameprop is set */
226 if (free_structname) {
227 MEM_delete(structname);
228 }
229
230 /* Use the property's owner struct icon. */
231 return RNA_struct_ui_icon(ptr.type);
232}
233
234std::string getname_anim_fcurve_for_slot(Main &bmain, const animrig::Slot &slot, FCurve &fcurve)
235{
236 /* TODO: Refactor to avoid this variable. */
237 constexpr size_t name_maxncpy = 256;
238 char name_buffer[name_maxncpy];
239 name_buffer[0] = '\0';
240
241 /* Check the Slot's users to see if we can find an ID* that can resolve the F-Curve. */
242 for (ID *user : slot.users(bmain)) {
243 const std::optional<int> icon = getname_anim_fcurve(name_buffer, user, &fcurve);
244 if (icon.has_value()) {
245 /* Managed to find a name! */
246 return name_buffer;
247 }
248 }
249
250 if (!slot.users(bmain).is_empty()) {
251 /* This slot is assigned to at least one ID, and still the property it animates could not be
252 * found. There is no use in continuing. */
253 fcurve.flag |= FCURVE_DISABLED;
254 return fmt::format("\"{}[{}]\"", fcurve.rna_path().c_str(), fcurve.array_index);
255 }
256
257 /* If this part of the code is hit, the slot is not assigned to anything. The remainder of
258 * this function is all a best-effort attempt. Because of that, it will not set the
259 * FCURVE_DISABLED flag on the F-Curve, as having unassigned animation data is not an error (and
260 * that flag indicates an error). */
261
262 /* Fall back to the ID type of the slot for simple properties. */
263 if (!slot.has_idtype()) {
264 /* The Slot has never been assigned to any ID, so we don't even know what type of ID it is
265 * meant for. */
266 return fmt::format("\"{}[{}]\"", fcurve.rna_path().c_str(), fcurve.array_index);
267 }
268
269 if (fcurve.rna_path().find(".") != StringRef::not_found) {
270 /* Not a simple property, so bail out. This needs path resolution, which needs an ID*. */
271 return fmt::format("\"{}[{}]\"", fcurve.rna_path().c_str(), fcurve.array_index);
272 }
273
274 /* Find the StructRNA for this Slot's ID type. */
276 if (!srna) {
277 return fmt::format("\"{}[{}]\"", fcurve.rna_path().c_str(), fcurve.array_index);
278 }
279
280 /* Find the property. */
281 PropertyRNA *prop = RNA_struct_type_find_property(srna, fcurve.rna_path().c_str());
282 if (!prop) {
283 return fmt::format("\"{}[{}]\"", fcurve.rna_path().c_str(), fcurve.array_index);
284 }
285
286 /* Property Name is straightforward */
287 const char *propname = RNA_property_ui_name(prop);
288
289 /* Array Index - only if applicable */
290 if (!RNA_property_array_check(prop)) {
291 return propname;
292 }
293
294 std::string arrayname;
295 char c = RNA_property_array_item_char(prop, fcurve.array_index);
296 if (c) {
297 arrayname = std::string(1, c);
298 }
299 else {
300 arrayname = fmt::format("[{}]", fcurve.array_index);
301 }
302 return arrayname + " " + propname;
303}
304
305/* ------------------------------- Color Codes for F-Curve Channels ---------------------------- */
306
307/* step between the major distinguishable color bands of the primary colors */
308#define HSV_BANDWIDTH 0.3f
309
310/* used to determine the color of F-Curves with FCURVE_COLOR_AUTO_RAINBOW set */
311// void fcurve_rainbow(uint cur, uint tot, float *out)
312void getcolor_fcurve_rainbow(int cur, int tot, float out[3])
313{
314 float hsv[3], fac;
315 int grouping;
316
317 /* we try to divide the color into groupings of n colors,
318 * where n is:
319 * 3 - for 'odd' numbers of curves - there should be a majority of triplets of curves
320 * 4 - for 'even' numbers of curves - there should be a majority of quartets of curves
321 * so the base color is simply one of the three primary colors
322 */
323 grouping = (4 - (tot % 2));
324 hsv[0] = HSV_BANDWIDTH * float(cur % grouping);
325
326 /* 'Value' (i.e. darkness) needs to vary so that larger sets of three will be
327 * 'darker' (i.e. smaller value), so that they don't look that similar to previous ones.
328 * However, only a range of 0.3 to 1.0 is really usable to avoid clashing
329 * with some other stuff
330 */
331 fac = (float(cur) / float(tot)) * 0.7f;
332
333 /* the base color can get offset a bit so that the colors aren't so identical */
334 hsv[0] += fac * HSV_BANDWIDTH;
335 if (hsv[0] > 1.0f) {
336 hsv[0] = fmod(hsv[0], 1.0f);
337 }
338
339 /* saturation adjustments for more visible range */
340 hsv[1] = ((hsv[0] > 0.5f) && (hsv[0] < 0.8f)) ? 0.5f : 0.6f;
341
342 /* value is fixed at 1.0f, otherwise we cannot clearly see the curves... */
343 hsv[2] = 1.0f;
344
345 /* finally, convert this to RGB colors */
346 hsv_to_rgb_v(hsv, out);
347}
348
349} // namespace blender
Functions and classes to work with Actions.
#define SNPRINTF_UTF8(dst, format,...)
#define RPT_(msgid)
#define ID_MISSING(_id)
Definition DNA_ID.h:695
Read Blender memory allocation and freeing.
#define HSV_BANDWIDTH
#define BLI_assert_unreachable()
Span< ID * > users(Main &bmain) const
static constexpr int64_t not_found
ccl_device_inline float2 fmod(const float2 a, const float b)
const bNodeTreeInterfaceSocket * node_find_interface_input_by_identifier(const bNodeTree &ntree, StringRef identifier)
Definition node.cc:4079
bNode & node_find_node(bNodeTree &ntree, bNodeSocket &socket)
Definition node.cc:4053
BMesh Iterators.
char * RNA_property_string_get_alloc(PointerRNA *ptr, PropertyRNA *prop, char *fixedbuf, int fixedlen, int *r_len)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
size_t size_t size_t BLI_snprintf_utf8(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
bool bool BLI_str_quoted_substr(const char *__restrict str, const char *__restrict prefix, char *result, size_t result_maxncpy)
PointerRNA RNA_id_pointer_create(ID *id)
StructRNA * ID_code_to_RNA_type(short idcode)
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
char * BLI_sprintfN(const char *__restrict format,...) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_PRINTF_FORMAT(1
const char * RNA_struct_ui_name(const StructRNA *type)
bool RNA_property_array_check(PropertyRNA *prop)
void hsv_to_rgb_v(const float hsv[3], float r_rgb[3])
Definition math_color.cc:61
char RNA_property_array_item_char(PropertyRNA *prop, int index)
PropertyRNA * RNA_struct_type_find_property(StructRNA *srna, const char *identifier)
PropertyRNA * RNA_struct_name_property(const StructRNA *type)
int RNA_struct_ui_icon(const StructRNA *type)
std::optional< int > getname_anim_fcurve(char *name, ID *id, FCurve *fcu)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
void getcolor_fcurve_rainbow(int cur, int tot, float out[3])
std::string getname_anim_fcurve_for_slot(Main &bmain, const animrig::Slot &slot, FCurve &fcurve)
bool RNA_path_resolve_property(const PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop)
Definition rna_path.cc:578
const char * RNA_property_ui_name(const PropertyRNA *prop, const PointerRNA *ptr)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eFCurve_Flags flag
char name[258]
Definition DNA_ID.h:433
StructRNA * type
Definition RNA_types.hh:63