Blender  V3.3
anim_ipo_utils.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2001-2002 NaN Holding BV. All rights reserved. */
3 
8 /* This file contains code for presenting F-Curves and other animation data
9  * in the UI (especially for use in the Animation Editors).
10  *
11  * -- Joshua Leung, Dec 2008
12  */
13 
14 #include "MEM_guardedalloc.h"
15 
16 #include "BLI_blenlib.h"
17 #include "BLI_math.h"
18 #include "BLI_utildefines.h"
19 
20 #include "BLT_translation.h"
21 
22 #include "DNA_anim_types.h"
23 
24 #include "RNA_access.h"
25 #include "RNA_path.h"
26 #include "RNA_prototypes.h"
27 
28 #include "ED_anim_api.h"
29 
30 /* ----------------------- Getter functions ----------------------- */
31 
32 int getname_anim_fcurve(char *name, ID *id, FCurve *fcu)
33 {
34  int icon = 0;
35 
36  /* sanity checks */
37  if (name == NULL) {
38  return icon;
39  }
40 
41  if (ELEM(NULL, id, fcu, fcu->rna_path)) {
42  if (fcu == NULL) {
43  strcpy(name, TIP_("<invalid>"));
44  }
45  else if (fcu->rna_path == NULL) {
46  strcpy(name, TIP_("<no path>"));
47  }
48  else { /* id == NULL */
49  BLI_snprintf(name, 256, "%s[%d]", fcu->rna_path, fcu->array_index);
50  }
51  }
52  else {
53  PointerRNA id_ptr, ptr;
54  PropertyRNA *prop;
55 
56  /* get RNA pointer, and resolve the path */
57  RNA_id_pointer_create(id, &id_ptr);
58 
59  /* try to resolve the path */
60  if (RNA_path_resolve_property(&id_ptr, fcu->rna_path, &ptr, &prop)) {
61  const char *structname = NULL, *propname = NULL;
62  char arrayindbuf[16];
63  const char *arrayname = NULL;
64  short free_structname = 0;
65 
66  /* For now, name will consist of 3 parts: struct-name, property name, array index
67  * There are several options possible:
68  * 1) <struct-name>.<property-name>.<array-index>
69  * i.e. Bone1.Location.X, or Object.Location.X
70  * 2) <array-index> <property-name> (<struct name>)
71  * i.e. X Location (Bone1), or X Location (Object)
72  *
73  * Currently, option 2 is in use, to try and make it easier to quickly identify F-Curves
74  * (it does have problems with looking rather odd though).
75  * Option 1 is better in terms of revealing a consistent sense of hierarchy though,
76  * which isn't so clear with option 2.
77  */
78 
79  /* For structname:
80  * - As base, we use a custom name from the structs if one is available
81  * - However, if we're showing subdata of bones
82  * (probably there will be other exceptions later).
83  * need to include that info too since it gets confusing otherwise.
84  * - If a pointer just refers to the ID-block, then don't repeat this info
85  * since this just introduces clutter.
86  */
87 
88  char pchanName[256], constName[256];
89  if (BLI_str_quoted_substr(fcu->rna_path, "bones[", pchanName, sizeof(pchanName)) &&
90  BLI_str_quoted_substr(fcu->rna_path, "constraints[", constName, sizeof(constName))) {
91 
92  /* assemble the string to display in the UI... */
93  structname = BLI_sprintfN("%s : %s", pchanName, constName);
94  free_structname = 1;
95  }
96  else if (ptr.data != ptr.owner_id) {
98  if (nameprop) {
99  /* this gets a string which will need to be freed */
100  structname = RNA_property_string_get_alloc(&ptr, nameprop, NULL, 0, NULL);
101  free_structname = 1;
102  }
103  else {
104  structname = RNA_struct_ui_name(ptr.type);
105  }
106 
107  /* For the sequencer, a strip's 'Transform' or 'Crop' is a nested (under Sequence)
108  * struct, but displaying the struct name alone is no meaningful information
109  * (and also cannot be filtered well), same for modifiers.
110  * So display strip name alongside as well. */
111  if (GS(ptr.owner_id->name) == ID_SCE) {
112  char stripname[256];
114  fcu->rna_path, "sequence_editor.sequences_all[", stripname, sizeof(stripname))) {
115  if (strstr(fcu->rna_path, ".transform.") || strstr(fcu->rna_path, ".crop.") ||
116  strstr(fcu->rna_path, ".modifiers[")) {
117  const char *structname_all = BLI_sprintfN("%s : %s", stripname, structname);
118  if (free_structname) {
119  MEM_freeN((void *)structname);
120  }
121  structname = structname_all;
122  free_structname = 1;
123  }
124  }
125  }
126  /* For node sockets, it is useful to include the node name as well (multiple similar nodes
127  * are not distinguishable otherwise). Unfortunately, the node label cannot be retrieved
128  * from the rna path, for this to work access to the underlying node is needed (but finding
129  * the node iterates all nodes & sockets which would result in bad performance in some
130  * circumstances). */
131  if (RNA_struct_is_a(ptr.type, &RNA_NodeSocket)) {
132  char nodename[256];
133  if (BLI_str_quoted_substr(fcu->rna_path, "nodes[", nodename, sizeof(nodename))) {
134  const char *structname_all = BLI_sprintfN("%s : %s", nodename, structname);
135  if (free_structname) {
136  MEM_freeN((void *)structname);
137  }
138  structname = structname_all;
139  free_structname = 1;
140  }
141  }
142  }
143 
144  /* Property Name is straightforward */
145  propname = RNA_property_ui_name(prop);
146 
147  /* Array Index - only if applicable */
148  if (RNA_property_array_check(prop)) {
149  char c = RNA_property_array_item_char(prop, fcu->array_index);
150 
151  /* we need to write the index to a temp buffer (in py syntax) */
152  if (c) {
153  BLI_snprintf(arrayindbuf, sizeof(arrayindbuf), "%c ", c);
154  }
155  else {
156  BLI_snprintf(arrayindbuf, sizeof(arrayindbuf), "[%d]", fcu->array_index);
157  }
158 
159  arrayname = &arrayindbuf[0];
160  }
161  else {
162  /* no array index */
163  arrayname = "";
164  }
165 
166  /* putting this all together into the buffer */
167  /* XXX we need to check for invalid names...
168  * XXX the name length limit needs to be passed in or as some define */
169  if (structname) {
170  BLI_snprintf(name, 256, "%s%s (%s)", arrayname, propname, structname);
171  }
172  else {
173  BLI_snprintf(name, 256, "%s%s", arrayname, propname);
174  }
175 
176  /* free temp name if nameprop is set */
177  if (free_structname) {
178  MEM_freeN((void *)structname);
179  }
180 
181  /* Icon for this property's owner:
182  * use the struct's icon if it is set
183  */
184  icon = RNA_struct_ui_icon(ptr.type);
185 
186  /* valid path - remove the invalid tag since we now know how to use it saving
187  * users manual effort to re-enable using "Revive Disabled FCurves" T29629. */
188  fcu->flag &= ~FCURVE_DISABLED;
189  }
190  else {
191  /* invalid path */
192  BLI_snprintf(name, 256, "\"%s[%d]\"", fcu->rna_path, fcu->array_index);
193 
194  /* icon for this should be the icon for the base ID */
195  /* TODO: or should we just use the error icon? */
196  icon = RNA_struct_ui_icon(id_ptr.type);
197 
198  /* tag F-Curve as disabled - as not usable path */
199  fcu->flag |= FCURVE_DISABLED;
200  }
201  }
202 
203  /* return the icon that the active data had */
204  return icon;
205 }
206 
207 /* ------------------------------- Color Codes for F-Curve Channels ---------------------------- */
208 
209 /* step between the major distinguishable color bands of the primary colors */
210 #define HSV_BANDWIDTH 0.3f
211 
212 /* used to determine the color of F-Curves with FCURVE_COLOR_AUTO_RAINBOW set */
213 // void fcurve_rainbow(uint cur, uint tot, float *out)
214 void getcolor_fcurve_rainbow(int cur, int tot, float out[3])
215 {
216  float hsv[3], fac;
217  int grouping;
218 
219  /* we try to divide the color into groupings of n colors,
220  * where n is:
221  * 3 - for 'odd' numbers of curves - there should be a majority of triplets of curves
222  * 4 - for 'even' numbers of curves - there should be a majority of quartets of curves
223  * so the base color is simply one of the three primary colors
224  */
225  grouping = (4 - (tot % 2));
226  hsv[0] = HSV_BANDWIDTH * (float)(cur % grouping);
227 
228  /* 'Value' (i.e. darkness) needs to vary so that larger sets of three will be
229  * 'darker' (i.e. smaller value), so that they don't look that similar to previous ones.
230  * However, only a range of 0.3 to 1.0 is really usable to avoid clashing
231  * with some other stuff
232  */
233  fac = ((float)cur / (float)tot) * 0.7f;
234 
235  /* the base color can get offset a bit so that the colors aren't so identical */
236  hsv[0] += fac * HSV_BANDWIDTH;
237  if (hsv[0] > 1.0f) {
238  hsv[0] = fmod(hsv[0], 1.0f);
239  }
240 
241  /* saturation adjustments for more visible range */
242  hsv[1] = ((hsv[0] > 0.5f) && (hsv[0] < 0.8f)) ? 0.5f : 0.6f;
243 
244  /* value is fixed at 1.0f, otherwise we cannot clearly see the curves... */
245  hsv[2] = 1.0f;
246 
247  /* finally, convert this to RGB colors */
248  hsv_to_rgb_v(hsv, out);
249 }
typedef float(TangentPoint)[2]
void hsv_to_rgb_v(const float hsv[3], float r_rgb[3])
Definition: math_color.c:49
size_t size_t char * BLI_sprintfN(const char *__restrict format,...) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_PRINTF_FORMAT(1
bool bool BLI_str_quoted_substr(const char *__restrict str, const char *__restrict prefix, char *result, size_t result_maxlen)
Definition: string.c:424
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
#define ELEM(...)
#define TIP_(msgid)
@ ID_SCE
Definition: DNA_ID_enums.h:45
@ FCURVE_DISABLED
Read Guarded memory(de)allocation.
void getcolor_fcurve_rainbow(int cur, int tot, float out[3])
#define HSV_BANDWIDTH
int getname_anim_fcurve(char *name, ID *id, FCurve *fcu)
#define GS(x)
Definition: iris.c:225
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
static unsigned c
Definition: RandGen.cpp:83
static const pxr::TfToken out("out", pxr::TfToken::Immortal)
bool RNA_property_array_check(PropertyRNA *prop)
Definition: rna_access.c:1080
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
Definition: rna_access.c:695
void RNA_id_pointer_create(ID *id, PointerRNA *r_ptr)
Definition: rna_access.c:112
const char * RNA_struct_ui_name(const StructRNA *type)
Definition: rna_access.c:591
char RNA_property_array_item_char(PropertyRNA *prop, int index)
Definition: rna_access.c:1105
char * RNA_property_string_get_alloc(PointerRNA *ptr, PropertyRNA *prop, char *fixedbuf, int fixedlen, int *r_len)
Definition: rna_access.c:3178
int RNA_struct_ui_icon(const StructRNA *type)
Definition: rna_access.c:601
PropertyRNA * RNA_struct_name_property(const StructRNA *type)
Definition: rna_access.c:624
const char * RNA_property_ui_name(const PropertyRNA *prop)
Definition: rna_access.c:1875
bool RNA_path_resolve_property(const PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop)
Definition: rna_path.cc:531
char * rna_path
int array_index
short flag
Definition: DNA_ID.h:368
char name[66]
Definition: DNA_ID.h:378
struct StructRNA * type
Definition: RNA_types.h:37
void * data
Definition: RNA_types.h:38
struct ID * owner_id
Definition: RNA_types.h:36
PointerRNA * ptr
Definition: wm_files.c:3480