Blender  V2.93
anim_ipo_utils.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
24 /* This file contains code for presenting F-Curves and other animation data
25  * in the UI (especially for use in the Animation Editors).
26  *
27  * -- Joshua Leung, Dec 2008
28  */
29 
30 #include "MEM_guardedalloc.h"
31 
32 #include "BLI_blenlib.h"
33 #include "BLI_math.h"
34 #include "BLI_utildefines.h"
35 
36 #include "BLT_translation.h"
37 
38 #include "DNA_anim_types.h"
39 
40 #include "RNA_access.h"
41 
42 #include "ED_anim_api.h"
43 
44 /* ----------------------- Getter functions ----------------------- */
45 
54 int getname_anim_fcurve(char *name, ID *id, FCurve *fcu)
55 {
56  int icon = 0;
57 
58  /* sanity checks */
59  if (name == NULL) {
60  return icon;
61  }
62 
63  if (ELEM(NULL, id, fcu, fcu->rna_path)) {
64  if (fcu == NULL) {
65  strcpy(name, TIP_("<invalid>"));
66  }
67  else if (fcu->rna_path == NULL) {
68  strcpy(name, TIP_("<no path>"));
69  }
70  else { /* id == NULL */
71  BLI_snprintf(name, 256, "%s[%d]", fcu->rna_path, fcu->array_index);
72  }
73  }
74  else {
75  PointerRNA id_ptr, ptr;
76  PropertyRNA *prop;
77 
78  /* get RNA pointer, and resolve the path */
79  RNA_id_pointer_create(id, &id_ptr);
80 
81  /* try to resolve the path */
82  if (RNA_path_resolve_property(&id_ptr, fcu->rna_path, &ptr, &prop)) {
83  const char *structname = NULL, *propname = NULL;
84  char arrayindbuf[16];
85  const char *arrayname = NULL;
86  short free_structname = 0;
87 
88  /* For now, name will consist of 3 parts: struct-name, property name, array index
89  * There are several options possible:
90  * 1) <struct-name>.<property-name>.<array-index>
91  * i.e. Bone1.Location.X, or Object.Location.X
92  * 2) <array-index> <property-name> (<struct name>)
93  * i.e. X Location (Bone1), or X Location (Object)
94  *
95  * Currently, option 2 is in use, to try and make it easier to quickly identify F-Curves
96  * (it does have problems with looking rather odd though).
97  * Option 1 is better in terms of revealing a consistent sense of hierarchy though,
98  * which isn't so clear with option 2.
99  */
100 
101  /* For structname:
102  * - As base, we use a custom name from the structs if one is available
103  * - However, if we're showing subdata of bones
104  * (probably there will be other exceptions later).
105  * need to include that info too since it gets confusing otherwise.
106  * - If a pointer just refers to the ID-block, then don't repeat this info
107  * since this just introduces clutter.
108  */
109  if (strstr(fcu->rna_path, "bones") && strstr(fcu->rna_path, "constraints")) {
110  /* perform string 'chopping' to get "Bone Name : Constraint Name" */
111  char *pchanName = BLI_str_quoted_substrN(fcu->rna_path, "bones[");
112  char *constName = BLI_str_quoted_substrN(fcu->rna_path, "constraints[");
113 
114  /* assemble the string to display in the UI... */
115  structname = BLI_sprintfN(
116  "%s : %s", pchanName ? pchanName : "", constName ? constName : "");
117  free_structname = 1;
118 
119  /* free the temp names */
120  if (pchanName) {
121  MEM_freeN(pchanName);
122  }
123  if (constName) {
124  MEM_freeN(constName);
125  }
126  }
127  else if (ptr.data != ptr.owner_id) {
129  if (nameprop) {
130  /* this gets a string which will need to be freed */
131  structname = RNA_property_string_get_alloc(&ptr, nameprop, NULL, 0, NULL);
132  free_structname = 1;
133  }
134  else {
135  structname = RNA_struct_ui_name(ptr.type);
136  }
137  }
138 
139  /* Property Name is straightforward */
140  propname = RNA_property_ui_name(prop);
141 
142  /* Array Index - only if applicable */
143  if (RNA_property_array_check(prop)) {
144  char c = RNA_property_array_item_char(prop, fcu->array_index);
145 
146  /* we need to write the index to a temp buffer (in py syntax) */
147  if (c) {
148  BLI_snprintf(arrayindbuf, sizeof(arrayindbuf), "%c ", c);
149  }
150  else {
151  BLI_snprintf(arrayindbuf, sizeof(arrayindbuf), "[%d]", fcu->array_index);
152  }
153 
154  arrayname = &arrayindbuf[0];
155  }
156  else {
157  /* no array index */
158  arrayname = "";
159  }
160 
161  /* putting this all together into the buffer */
162  /* XXX we need to check for invalid names...
163  * XXX the name length limit needs to be passed in or as some define */
164  if (structname) {
165  BLI_snprintf(name, 256, "%s%s (%s)", arrayname, propname, structname);
166  }
167  else {
168  BLI_snprintf(name, 256, "%s%s", arrayname, propname);
169  }
170 
171  /* free temp name if nameprop is set */
172  if (free_structname) {
173  MEM_freeN((void *)structname);
174  }
175 
176  /* Icon for this property's owner:
177  * use the struct's icon if it is set
178  */
179  icon = RNA_struct_ui_icon(ptr.type);
180 
181  /* valid path - remove the invalid tag since we now know how to use it saving
182  * users manual effort to re-enable using "Revive Disabled FCurves" T29629. */
183  fcu->flag &= ~FCURVE_DISABLED;
184  }
185  else {
186  /* invalid path */
187  BLI_snprintf(name, 256, "\"%s[%d]\"", fcu->rna_path, fcu->array_index);
188 
189  /* icon for this should be the icon for the base ID */
190  /* TODO: or should we just use the error icon? */
191  icon = RNA_struct_ui_icon(id_ptr.type);
192 
193  /* tag F-Curve as disabled - as not usable path */
194  fcu->flag |= FCURVE_DISABLED;
195  }
196  }
197 
198  /* return the icon that the active data had */
199  return icon;
200 }
201 
202 /* ------------------------------- Color Codes for F-Curve Channels ---------------------------- */
203 
204 /* step between the major distinguishable color bands of the primary colors */
205 #define HSV_BANDWIDTH 0.3f
206 
207 /* used to determine the color of F-Curves with FCURVE_COLOR_AUTO_RAINBOW set */
208 // void fcurve_rainbow(uint cur, uint tot, float *out)
209 void getcolor_fcurve_rainbow(int cur, int tot, float out[3])
210 {
211  float hsv[3], fac;
212  int grouping;
213 
214  /* we try to divide the color into groupings of n colors,
215  * where n is:
216  * 3 - for 'odd' numbers of curves - there should be a majority of triplets of curves
217  * 4 - for 'even' numbers of curves - there should be a majority of quartets of curves
218  * so the base color is simply one of the three primary colors
219  */
220  grouping = (4 - (tot % 2));
221  hsv[0] = HSV_BANDWIDTH * (float)(cur % grouping);
222 
223  /* 'Value' (i.e. darkness) needs to vary so that larger sets of three will be
224  * 'darker' (i.e. smaller value), so that they don't look that similar to previous ones.
225  * However, only a range of 0.3 to 1.0 is really usable to avoid clashing
226  * with some other stuff
227  */
228  fac = ((float)cur / (float)tot) * 0.7f;
229 
230  /* the base color can get offset a bit so that the colors aren't so identical */
231  hsv[0] += fac * HSV_BANDWIDTH;
232  if (hsv[0] > 1.0f) {
233  hsv[0] = fmod(hsv[0], 1.0f);
234  }
235 
236  /* saturation adjustments for more visible range */
237  hsv[1] = ((hsv[0] > 0.5f) && (hsv[0] < 0.8f)) ? 0.5f : 0.6f;
238 
239  /* value is fixed at 1.0f, otherwise we cannot clearly see the curves... */
240  hsv[2] = 1.0f;
241 
242  /* finally, convert this to RGB colors */
243  hsv_to_rgb_v(hsv, out);
244 }
typedef float(TangentPoint)[2]
void hsv_to_rgb_v(const float hsv[3], float r_rgb[3])
Definition: math_color.c:68
size_t size_t char * BLI_sprintfN(const char *__restrict format,...) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_PRINTF_FORMAT(1
char * BLI_str_quoted_substrN(const char *__restrict str, const char *__restrict prefix) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:432
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
#define ELEM(...)
#define TIP_(msgid)
@ 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)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
static unsigned c
Definition: RandGen.cpp:97
bool RNA_property_array_check(PropertyRNA *prop)
Definition: rna_access.c:1223
void RNA_id_pointer_create(ID *id, PointerRNA *r_ptr)
Definition: rna_access.c:122
const char * RNA_struct_ui_name(const StructRNA *type)
Definition: rna_access.c:728
char RNA_property_array_item_char(PropertyRNA *prop, int index)
Definition: rna_access.c:1250
char * RNA_property_string_get_alloc(PointerRNA *ptr, PropertyRNA *prop, char *fixedbuf, int fixedlen, int *r_len)
Definition: rna_access.c:3339
int RNA_struct_ui_icon(const StructRNA *type)
Definition: rna_access.c:738
PropertyRNA * RNA_struct_name_property(const StructRNA *type)
Definition: rna_access.c:761
bool RNA_path_resolve_property(PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop)
Definition: rna_access.c:5434
const char * RNA_property_ui_name(const PropertyRNA *prop)
Definition: rna_access.c:2043
char * rna_path
int array_index
short flag
Definition: DNA_ID.h:273
struct StructRNA * type
Definition: RNA_types.h:51
void * data
Definition: RNA_types.h:52
struct ID * owner_id
Definition: RNA_types.h:50
PointerRNA * ptr
Definition: wm_files.c:3157