2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * Contributor(s): Blender Foundation, 2005. Full recode
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/editors/animation/anim_ipo_utils.c
28 * \ingroup edanimation
32 /* This file contains code for presenting F-Curves and other animation data
33 * in the UI (especially for use in the Animation Editors).
35 * -- Joshua Leung, Dec 2008
39 #include "MEM_guardedalloc.h"
41 #include "BLI_blenlib.h"
43 #include "BLI_utildefines.h"
45 #include "BLT_translation.h"
47 #include "DNA_anim_types.h"
49 #include "RNA_access.h"
51 #include "ED_anim_api.h"
53 /* ----------------------- Getter functions ----------------------- */
55 /* Write into "name" buffer, the name of the property (retrieved using RNA from the curve's settings),
56 * and return the icon used for the struct that this property refers to
57 * WARNING: name buffer we're writing to cannot exceed 256 chars (check anim_channels_defines.c for details)
59 int getname_anim_fcurve(char *name, ID *id, FCurve *fcu)
66 else if (ELEM(NULL, id, fcu, fcu->rna_path)) {
68 strcpy(name, IFACE_("<invalid>"));
69 else if (fcu->rna_path == NULL)
70 strcpy(name, IFACE_("<no path>"));
72 BLI_snprintf(name, 256, "%s[%d]", fcu->rna_path, fcu->array_index);
75 PointerRNA id_ptr, ptr;
78 /* get RNA pointer, and resolve the path */
79 RNA_id_pointer_create(id, &id_ptr);
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;
85 const char *arrayname = NULL;
86 short free_structname = 0;
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)
95 * Currently, option 2 is in use, to try and make it easier to quickly identify F-Curves (it does have
96 * problems with looking rather odd though). Option 1 is better in terms of revealing a consistent sense of
97 * hierarchy though, which isn't so clear with option 2.
101 * - as base, we use a custom name from the structs if one is available
102 * - however, if we're showing subdata of bones (probably there will be other exceptions later)
103 * need to include that info too since it gets confusing otherwise
104 * - if a pointer just refers to the ID-block, then don't repeat this info
105 * since this just introduces clutter
107 if (strstr(fcu->rna_path, "bones") && strstr(fcu->rna_path, "constraints")) {
108 /* perform string 'chopping' to get "Bone Name : Constraint Name" */
109 char *pchanName = BLI_str_quoted_substrN(fcu->rna_path, "bones[");
110 char *constName = BLI_str_quoted_substrN(fcu->rna_path, "constraints[");
112 /* assemble the string to display in the UI... */
113 structname = BLI_sprintfN("%s : %s", pchanName, constName);
116 /* free the temp names */
117 if (pchanName) MEM_freeN(pchanName);
118 if (constName) MEM_freeN(constName);
120 else if (ptr.data != ptr.id.data) {
121 PropertyRNA *nameprop = RNA_struct_name_property(ptr.type);
123 /* this gets a string which will need to be freed */
124 structname = RNA_property_string_get_alloc(&ptr, nameprop, NULL, 0, NULL);
128 structname = RNA_struct_ui_name(ptr.type);
131 /* Property Name is straightforward */
132 propname = RNA_property_ui_name(prop);
134 /* Array Index - only if applicable */
135 if (RNA_property_array_check(prop)) {
136 char c = RNA_property_array_item_char(prop, fcu->array_index);
138 /* we need to write the index to a temp buffer (in py syntax) */
139 if (c) BLI_snprintf(arrayindbuf, sizeof(arrayindbuf), "%c ", c);
140 else BLI_snprintf(arrayindbuf, sizeof(arrayindbuf), "[%d]", fcu->array_index);
142 arrayname = &arrayindbuf[0];
149 /* putting this all together into the buffer */
150 /* XXX we need to check for invalid names...
151 * XXX the name length limit needs to be passed in or as some define */
153 BLI_snprintf(name, 256, "%s%s (%s)", arrayname, propname, structname);
155 BLI_snprintf(name, 256, "%s%s", arrayname, propname);
157 /* free temp name if nameprop is set */
159 MEM_freeN((void *)structname);
162 /* Icon for this property's owner:
163 * use the struct's icon if it is set
165 icon = RNA_struct_ui_icon(ptr.type);
167 /* valid path - remove the invalid tag since we now know how to use it saving
168 * users manual effort to reenable using "Revive Disabled FCurves" [#29629]
170 fcu->flag &= ~FCURVE_DISABLED;
174 BLI_snprintf(name, 256, "\"%s[%d]\"", fcu->rna_path, fcu->array_index);
176 /* icon for this should be the icon for the base ID */
177 /* TODO: or should we just use the error icon? */
178 icon = RNA_struct_ui_icon(id_ptr.type);
180 /* tag F-Curve as disabled - as not usable path */
181 fcu->flag |= FCURVE_DISABLED;
185 /* return the icon that the active data had */
189 /* ------------------------------- Color Codes for F-Curve Channels ---------------------------- */
191 /* step between the major distinguishable color bands of the primary colors */
192 #define HSV_BANDWIDTH 0.3f
194 /* used to determine the color of F-Curves with FCURVE_COLOR_AUTO_RAINBOW set */
195 // void fcurve_rainbow(unsigned int cur, unsigned int tot, float *out)
196 void getcolor_fcurve_rainbow(int cur, int tot, float out[3])
201 /* we try to divide the color into groupings of n colors,
203 * 3 - for 'odd' numbers of curves - there should be a majority of triplets of curves
204 * 4 - for 'even' numbers of curves - there should be a majority of quartets of curves
205 * so the base color is simply one of the three primary colors
207 grouping = (4 - (tot % 2));
208 hsv[0] = HSV_BANDWIDTH * (float)(cur % grouping);
210 /* 'Value' (i.e. darkness) needs to vary so that larger sets of three will be
211 * 'darker' (i.e. smaller value), so that they don't look that similar to previous ones.
212 * However, only a range of 0.3 to 1.0 is really usable to avoid clashing
213 * with some other stuff
215 fac = ((float)cur / (float)tot) * 0.7f;
217 /* the base color can get offset a bit so that the colors aren't so identical */
218 hsv[0] += fac * HSV_BANDWIDTH;
219 if (hsv[0] > 1.0f) hsv[0] = fmod(hsv[0], 1.0f);
221 /* saturation adjustments for more visible range */
222 hsv[1] = ((hsv[0] > 0.5f) && (hsv[0] < 0.8f)) ? 0.5f : 0.6f;
224 /* value is fixed at 1.0f, otherwise we cannot clearly see the curves... */
227 /* finally, conver this to RGB colors */
228 hsv_to_rgb_v(hsv, out);