Welcome to mirror list, hosted at ThFree Co, Russian Federation.

anim_ipo_utils.c « animation « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f01b35225476dfc395c0ef4ef21657355dc62b5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2001-2002 NaN Holding BV. All rights reserved. */

/** \file
 * \ingroup edanimation
 */

/* This file contains code for presenting F-Curves and other animation data
 * in the UI (especially for use in the Animation Editors).
 *
 * -- Joshua Leung, Dec 2008
 */

#include "MEM_guardedalloc.h"

#include "BLI_blenlib.h"
#include "BLI_math.h"
#include "BLI_utildefines.h"

#include "BLT_translation.h"

#include "DNA_anim_types.h"

#include "RNA_access.h"
#include "RNA_prototypes.h"

#include "ED_anim_api.h"

/* ----------------------- Getter functions ----------------------- */

int getname_anim_fcurve(char *name, ID *id, FCurve *fcu)
{
  int icon = 0;

  /* sanity checks */
  if (name == NULL) {
    return icon;
  }

  if (ELEM(NULL, id, fcu, fcu->rna_path)) {
    if (fcu == NULL) {
      strcpy(name, TIP_("<invalid>"));
    }
    else if (fcu->rna_path == NULL) {
      strcpy(name, TIP_("<no path>"));
    }
    else { /* id == NULL */
      BLI_snprintf(name, 256, "%s[%d]", fcu->rna_path, fcu->array_index);
    }
  }
  else {
    PointerRNA id_ptr, ptr;
    PropertyRNA *prop;

    /* get RNA pointer, and resolve the path */
    RNA_id_pointer_create(id, &id_ptr);

    /* try to resolve the path */
    if (RNA_path_resolve_property(&id_ptr, fcu->rna_path, &ptr, &prop)) {
      const char *structname = NULL, *propname = NULL;
      char arrayindbuf[16];
      const char *arrayname = NULL;
      short free_structname = 0;

      /* For now, name will consist of 3 parts: struct-name, property name, array index
       * There are several options possible:
       * 1) <struct-name>.<property-name>.<array-index>
       *     i.e. Bone1.Location.X, or Object.Location.X
       * 2) <array-index> <property-name> (<struct name>)
       *     i.e. X Location (Bone1), or X Location (Object)
       *
       * Currently, option 2 is in use, to try and make it easier to quickly identify F-Curves
       * (it does have problems with looking rather odd though).
       * Option 1 is better in terms of revealing a consistent sense of hierarchy though,
       * which isn't so clear with option 2.
       */

      /* For structname:
       * - As base, we use a custom name from the structs if one is available
       * - However, if we're showing subdata of bones
       *   (probably there will be other exceptions later).
       *   need to include that info too since it gets confusing otherwise.
       * - If a pointer just refers to the ID-block, then don't repeat this info
       *   since this just introduces clutter.
       */

      char pchanName[256], constName[256];
      if (BLI_str_quoted_substr(fcu->rna_path, "bones[", pchanName, sizeof(pchanName)) &&
          BLI_str_quoted_substr(fcu->rna_path, "constraints[", constName, sizeof(constName))) {

        /* assemble the string to display in the UI... */
        structname = BLI_sprintfN("%s : %s", pchanName, constName);
        free_structname = 1;
      }
      else if (ptr.data != ptr.owner_id) {
        PropertyRNA *nameprop = RNA_struct_name_property(ptr.type);
        if (nameprop) {
          /* this gets a string which will need to be freed */
          structname = RNA_property_string_get_alloc(&ptr, nameprop, NULL, 0, NULL);
          free_structname = 1;
        }
        else {
          structname = RNA_struct_ui_name(ptr.type);
        }

        /* For the sequencer, a strip's 'Transform' or 'Crop' is a nested (under Sequence)
         * struct, but displaying the struct name alone is no meaningful information
         * (and also cannot be filtered well), same for modifiers.
         * So display strip name alongside as well. */
        if (GS(ptr.owner_id->name) == ID_SCE) {
          char stripname[256];
          if (BLI_str_quoted_substr(
                  fcu->rna_path, "sequence_editor.sequences_all[", stripname, sizeof(stripname))) {
            if (strstr(fcu->rna_path, ".transform.") || strstr(fcu->rna_path, ".crop.") ||
                strstr(fcu->rna_path, ".modifiers[")) {
              const char *structname_all = BLI_sprintfN("%s : %s", stripname, structname);
              if (free_structname) {
                MEM_freeN((void *)structname);
              }
              structname = structname_all;
              free_structname = 1;
            }
          }
        }
        /* For node sockets, it is useful to include the node name as well (multiple similar nodes
         * are not distinguishable otherwise). Unfortunately, the node label cannot be retrieved
         * from the rna path, for this to work access to the underlying node is needed (but finding
         * the node iterates all nodes & sockets which would result in bad performance in some
         * circumstances). */
        if (RNA_struct_is_a(ptr.type, &RNA_NodeSocket)) {
          char nodename[256];
          if (BLI_str_quoted_substr(fcu->rna_path, "nodes[", nodename, sizeof(nodename))) {
            const char *structname_all = BLI_sprintfN("%s : %s", nodename, structname);
            if (free_structname) {
              MEM_freeN((void *)structname);
            }
            structname = structname_all;
            free_structname = 1;
          }
        }
      }

      /* Property Name is straightforward */
      propname = RNA_property_ui_name(prop);

      /* Array Index - only if applicable */
      if (RNA_property_array_check(prop)) {
        char c = RNA_property_array_item_char(prop, fcu->array_index);

        /* we need to write the index to a temp buffer (in py syntax) */
        if (c) {
          BLI_snprintf(arrayindbuf, sizeof(arrayindbuf), "%c ", c);
        }
        else {
          BLI_snprintf(arrayindbuf, sizeof(arrayindbuf), "[%d]", fcu->array_index);
        }

        arrayname = &arrayindbuf[0];
      }
      else {
        /* no array index */
        arrayname = "";
      }

      /* putting this all together into the buffer */
      /* XXX we need to check for invalid names...
       * XXX the name length limit needs to be passed in or as some define */
      if (structname) {
        BLI_snprintf(name, 256, "%s%s (%s)", arrayname, propname, structname);
      }
      else {
        BLI_snprintf(name, 256, "%s%s", arrayname, propname);
      }

      /* free temp name if nameprop is set */
      if (free_structname) {
        MEM_freeN((void *)structname);
      }

      /* Icon for this property's owner:
       * use the struct's icon if it is set
       */
      icon = RNA_struct_ui_icon(ptr.type);

      /* valid path - remove the invalid tag since we now know how to use it saving
       * users manual effort to re-enable using "Revive Disabled FCurves" T29629. */
      fcu->flag &= ~FCURVE_DISABLED;
    }
    else {
      /* invalid path */
      BLI_snprintf(name, 256, "\"%s[%d]\"", fcu->rna_path, fcu->array_index);

      /* icon for this should be the icon for the base ID */
      /* TODO: or should we just use the error icon? */
      icon = RNA_struct_ui_icon(id_ptr.type);

      /* tag F-Curve as disabled - as not usable path */
      fcu->flag |= FCURVE_DISABLED;
    }
  }

  /* return the icon that the active data had */
  return icon;
}

/* ------------------------------- Color Codes for F-Curve Channels ---------------------------- */

/* step between the major distinguishable color bands of the primary colors */
#define HSV_BANDWIDTH 0.3f

/* used to determine the color of F-Curves with FCURVE_COLOR_AUTO_RAINBOW set */
// void fcurve_rainbow(uint cur, uint tot, float *out)
void getcolor_fcurve_rainbow(int cur, int tot, float out[3])
{
  float hsv[3], fac;
  int grouping;

  /* we try to divide the color into groupings of n colors,
   * where n is:
   * 3 - for 'odd' numbers of curves - there should be a majority of triplets of curves
   * 4 - for 'even' numbers of curves - there should be a majority of quartets of curves
   * so the base color is simply one of the three primary colors
   */
  grouping = (4 - (tot % 2));
  hsv[0] = HSV_BANDWIDTH * (float)(cur % grouping);

  /* 'Value' (i.e. darkness) needs to vary so that larger sets of three will be
   * 'darker' (i.e. smaller value), so that they don't look that similar to previous ones.
   * However, only a range of 0.3 to 1.0 is really usable to avoid clashing
   * with some other stuff
   */
  fac = ((float)cur / (float)tot) * 0.7f;

  /* the base color can get offset a bit so that the colors aren't so identical */
  hsv[0] += fac * HSV_BANDWIDTH;
  if (hsv[0] > 1.0f) {
    hsv[0] = fmod(hsv[0], 1.0f);
  }

  /* saturation adjustments for more visible range */
  hsv[1] = ((hsv[0] > 0.5f) && (hsv[0] < 0.8f)) ? 0.5f : 0.6f;

  /* value is fixed at 1.0f, otherwise we cannot clearly see the curves... */
  hsv[2] = 1.0f;

  /* finally, convert this to RGB colors */
  hsv_to_rgb_v(hsv, out);
}