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

BCAnimationCurve.cpp « collada « io « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fe90dc5d5fad6760d92df9b6fdf49e8298062ce0 (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2008 Blender Foundation. All rights reserved. */

#include "RNA_path.h"

#include "BCAnimationCurve.h"

BCAnimationCurve::BCAnimationCurve()
{
  this->curve_key.set_object_type(BC_ANIMATION_TYPE_OBJECT);
  this->fcurve = nullptr;
  this->curve_is_local_copy = false;
}

BCAnimationCurve::BCAnimationCurve(const BCAnimationCurve &other)
{
  this->min = other.min;
  this->max = other.max;
  this->fcurve = other.fcurve;
  this->curve_key = other.curve_key;
  this->curve_is_local_copy = false;
  this->id_ptr = other.id_ptr;

  /* The fcurve of the new instance is a copy and can be modified */

  get_edit_fcurve();
}

BCAnimationCurve::BCAnimationCurve(BCCurveKey key, Object *ob, FCurve *fcu)
{
  this->min = 0;
  this->max = 0;
  this->curve_key = key;
  this->fcurve = fcu;
  this->curve_is_local_copy = false;
  init_pointer_rna(ob);
}

BCAnimationCurve::BCAnimationCurve(const BCCurveKey &key, Object *ob)
{
  this->curve_key = key;
  this->fcurve = nullptr;
  this->curve_is_local_copy = false;
  init_pointer_rna(ob);
}

void BCAnimationCurve::init_pointer_rna(Object *ob)
{
  switch (this->curve_key.get_animation_type()) {
    case BC_ANIMATION_TYPE_BONE: {
      bArmature *arm = (bArmature *)ob->data;
      RNA_id_pointer_create(&arm->id, &id_ptr);
    } break;
    case BC_ANIMATION_TYPE_OBJECT: {
      RNA_id_pointer_create(&ob->id, &id_ptr);
    } break;
    case BC_ANIMATION_TYPE_MATERIAL: {
      Material *ma = BKE_object_material_get(ob, curve_key.get_subindex() + 1);
      RNA_id_pointer_create(&ma->id, &id_ptr);
    } break;
    case BC_ANIMATION_TYPE_CAMERA: {
      Camera *camera = (Camera *)ob->data;
      RNA_id_pointer_create(&camera->id, &id_ptr);
    } break;
    case BC_ANIMATION_TYPE_LIGHT: {
      Light *lamp = (Light *)ob->data;
      RNA_id_pointer_create(&lamp->id, &id_ptr);
    } break;
    default:
      fprintf(
          stderr, "BC_animation_curve_type %d not supported", this->curve_key.get_array_index());
      break;
  }
}

void BCAnimationCurve::delete_fcurve(FCurve *fcu)
{
  BKE_fcurve_free(fcu);
}

FCurve *BCAnimationCurve::create_fcurve(int array_index, const char *rna_path)
{
  FCurve *fcu = BKE_fcurve_create();
  fcu->flag = (FCURVE_VISIBLE | FCURVE_SELECTED);
  fcu->rna_path = BLI_strdupn(rna_path, strlen(rna_path));
  fcu->array_index = array_index;
  return fcu;
}

void BCAnimationCurve::create_bezt(float frame, float output)
{
  FCurve *fcu = get_edit_fcurve();
  BezTriple bez;
  memset(&bez, 0, sizeof(BezTriple));
  bez.vec[1][0] = frame;
  bez.vec[1][1] = output;
  bez.ipo = U.ipo_new; /* use default interpolation mode here... */
  bez.f1 = bez.f2 = bez.f3 = SELECT;
  bez.h1 = bez.h2 = HD_AUTO;
  insert_bezt_fcurve(fcu, &bez, INSERTKEY_NOFLAGS);
  BKE_fcurve_handles_recalc(fcu);
}

BCAnimationCurve::~BCAnimationCurve()
{
  if (curve_is_local_copy && fcurve) {
    // fprintf(stderr, "removed fcurve %s\n", fcurve->rna_path);
    delete_fcurve(fcurve);
    this->fcurve = nullptr;
  }
}

bool BCAnimationCurve::is_of_animation_type(BC_animation_type type) const
{
  return curve_key.get_animation_type() == type;
}

std::string BCAnimationCurve::get_channel_target() const
{
  const std::string path = curve_key.get_path();

  if (bc_startswith(path, "pose.bones")) {
    return bc_string_after(path, "pose.bones");
  }
  return bc_string_after(path, ".");
}

std::string BCAnimationCurve::get_channel_type() const
{
  const std::string channel = get_channel_target();
  return bc_string_after(channel, ".");
}

std::string BCAnimationCurve::get_channel_posebone() const
{
  const std::string channel = get_channel_target();
  std::string pose_bone_name = bc_string_before(channel, ".");
  if (pose_bone_name == channel) {
    pose_bone_name = "";
  }
  else {
    pose_bone_name = bc_string_after(pose_bone_name, "\"[");
    pose_bone_name = bc_string_before(pose_bone_name, "]\"");
  }
  return pose_bone_name;
}

std::string BCAnimationCurve::get_animation_name(Object *ob) const
{
  std::string name;

  switch (curve_key.get_animation_type()) {
    case BC_ANIMATION_TYPE_OBJECT: {
      name = id_name(ob);
    } break;

    case BC_ANIMATION_TYPE_BONE: {
      if (fcurve == nullptr || fcurve->rna_path == nullptr) {
        name = "";
      }
      else {
        char boneName[MAXBONENAME];
        if (BLI_str_quoted_substr(fcurve->rna_path, "pose.bones[", boneName, sizeof(boneName))) {
          name = id_name(ob) + "_" + std::string(boneName);
        }
        else {
          name = "";
        }
      }
    } break;

    case BC_ANIMATION_TYPE_CAMERA: {
      Camera *camera = (Camera *)ob->data;
      name = id_name(ob) + "-" + id_name(camera) + "-camera";
    } break;

    case BC_ANIMATION_TYPE_LIGHT: {
      Light *lamp = (Light *)ob->data;
      name = id_name(ob) + "-" + id_name(lamp) + "-light";
    } break;

    case BC_ANIMATION_TYPE_MATERIAL: {
      Material *ma = BKE_object_material_get(ob, this->curve_key.get_subindex() + 1);
      name = id_name(ob) + "-" + id_name(ma) + "-material";
    } break;

    default: {
      name = "";
    }
  }

  return name;
}

int BCAnimationCurve::get_channel_index() const
{
  return curve_key.get_array_index();
}

int BCAnimationCurve::get_subindex() const
{
  return curve_key.get_subindex();
}

std::string BCAnimationCurve::get_rna_path() const
{
  return curve_key.get_path();
}

int BCAnimationCurve::sample_count() const
{
  if (fcurve == nullptr) {
    return 0;
  }
  return fcurve->totvert;
}

int BCAnimationCurve::closest_index_above(const float sample_frame, const int start_at) const
{
  if (fcurve == nullptr) {
    return -1;
  }

  const int cframe = fcurve->bezt[start_at].vec[1][0]; /* inaccurate! */

  if (fabs(cframe - sample_frame) < 0.00001) {
    return start_at;
  }
  return (fcurve->totvert > start_at + 1) ? start_at + 1 : start_at;
}

int BCAnimationCurve::closest_index_below(const float sample_frame) const
{
  if (fcurve == nullptr) {
    return -1;
  }

  float lower_frame = sample_frame;
  float upper_frame = sample_frame;
  int lower_index = 0;
  int upper_index = 0;

  for (int fcu_index = 0; fcu_index < fcurve->totvert; fcu_index++) {
    upper_index = fcu_index;

    const int cframe = fcurve->bezt[fcu_index].vec[1][0]; /* inaccurate! */
    if (cframe <= sample_frame) {
      lower_frame = cframe;
      lower_index = fcu_index;
    }
    if (cframe >= sample_frame) {
      upper_frame = cframe;
      break;
    }
  }

  if (lower_index == upper_index) {
    return lower_index;
  }

  const float fraction = float(sample_frame - lower_frame) / (upper_frame - lower_frame);
  return (fraction < 0.5) ? lower_index : upper_index;
}

int BCAnimationCurve::get_interpolation_type(float sample_frame) const
{
  const int index = closest_index_below(sample_frame);
  if (index < 0) {
    return BEZT_IPO_BEZ;
  }
  return fcurve->bezt[index].ipo;
}

FCurve *BCAnimationCurve::get_fcurve() const
{
  return fcurve;
}

FCurve *BCAnimationCurve::get_edit_fcurve()
{
  if (!curve_is_local_copy) {
    const int index = curve_key.get_array_index();
    const std::string &path = curve_key.get_path();
    fcurve = create_fcurve(index, path.c_str());

    /* Caution here:
     * Replacing the pointer here is OK only because the original value
     * of FCurve was a const pointer into Blender territory. We do not
     * touch that! We use the local copy to prepare data for export. */

    curve_is_local_copy = true;
  }
  return fcurve;
}

void BCAnimationCurve::clean_handles()
{
  if (fcurve == nullptr) {
    fcurve = get_edit_fcurve();
  }

  /* Keep old bezt data for copy). */
  BezTriple *old_bezts = fcurve->bezt;
  int totvert = fcurve->totvert;
  fcurve->bezt = nullptr;
  fcurve->totvert = 0;

  for (int i = 0; i < totvert; i++) {
    BezTriple *bezt = &old_bezts[i];
    float x = bezt->vec[1][0];
    float y = bezt->vec[1][1];
    insert_vert_fcurve(fcurve, x, y, (eBezTriple_KeyframeType)BEZKEYTYPE(bezt), INSERTKEY_NOFLAGS);
    BezTriple *lastb = fcurve->bezt + (fcurve->totvert - 1);
    lastb->f1 = lastb->f2 = lastb->f3 = 0;
  }

  /* now free the memory used by the old BezTriples */
  if (old_bezts) {
    MEM_freeN(old_bezts);
  }
}

bool BCAnimationCurve::is_transform_curve() const
{
  std::string channel_type = this->get_channel_type();
  return (is_rotation_curve() || channel_type == "scale" || channel_type == "location");
}

bool BCAnimationCurve::is_rotation_curve() const
{
  std::string channel_type = this->get_channel_type();
  return (channel_type == "rotation" || channel_type == "rotation_euler" ||
          channel_type == "rotation_quaternion");
}

float BCAnimationCurve::get_value(const float frame)
{
  if (fcurve) {
    return evaluate_fcurve(fcurve, frame);
  }
  return 0; /* TODO: handle case where neither sample nor fcu exist */
}

void BCAnimationCurve::update_range(float val)
{
  if (val < min) {
    min = val;
  }
  if (val > max) {
    max = val;
  }
}

void BCAnimationCurve::init_range(float val)
{
  min = max = val;
}

void BCAnimationCurve::adjust_range(const int frame_index)
{
  if (fcurve && fcurve->totvert > 1) {
    const float eval = evaluate_fcurve(fcurve, frame_index);

    int first_frame = fcurve->bezt[0].vec[1][0];
    if (first_frame == frame_index) {
      init_range(eval);
    }
    else {
      update_range(eval);
    }
  }
}

void BCAnimationCurve::add_value(const float val, const int frame_index)
{
  FCurve *fcu = get_edit_fcurve();
  fcu->auto_smoothing = U.auto_smoothing_new;
  insert_vert_fcurve(fcu, frame_index, val, BEZT_KEYTYPE_KEYFRAME, INSERTKEY_NOFLAGS);

  if (fcu->totvert == 1) {
    init_range(val);
  }
  else {
    update_range(val);
  }
}

bool BCAnimationCurve::add_value_from_matrix(const BCSample &sample, const int frame_index)
{
  int array_index = curve_key.get_array_index();

  /* transformation curves are fed directly from the transformation matrix
   * to resolve parent inverse matrix issues with object hierarchies.
   * Maybe this can be unified with the
   */
  const std::string channel_target = get_channel_target();
  float val = 0;
  /* Pick the value from the sample according to the definition of the FCurve */
  bool good = sample.get_value(channel_target, array_index, &val);
  if (good) {
    add_value(val, frame_index);
  }
  return good;
}

bool BCAnimationCurve::add_value_from_rna(const int frame_index)
{
  PointerRNA ptr;
  PropertyRNA *prop;
  float value = 0.0f;
  int array_index = curve_key.get_array_index();
  const std::string full_path = curve_key.get_full_path();

  /* get property to read from, and get value as appropriate */
  bool path_resolved = RNA_path_resolve_full(
      &id_ptr, full_path.c_str(), &ptr, &prop, &array_index);
  if (!path_resolved && array_index == 0) {
    const std::string rna_path = curve_key.get_path();
    path_resolved = RNA_path_resolve_full(&id_ptr, rna_path.c_str(), &ptr, &prop, &array_index);
  }

  if (path_resolved) {
    bool is_array = RNA_property_array_check(prop);
    if (is_array) {
      /* array */
      if ((array_index >= 0) && (array_index < RNA_property_array_length(&ptr, prop))) {
        switch (RNA_property_type(prop)) {
          case PROP_BOOLEAN:
            value = (float)RNA_property_boolean_get_index(&ptr, prop, array_index);
            break;
          case PROP_INT:
            value = (float)RNA_property_int_get_index(&ptr, prop, array_index);
            break;
          case PROP_FLOAT:
            value = RNA_property_float_get_index(&ptr, prop, array_index);
            break;
          default:
            break;
        }
      }
      else {
        fprintf(stderr,
                "Out of Bounds while reading data for Curve %s\n",
                curve_key.get_full_path().c_str());
        return false;
      }
    }
    else {
      /* not an array */
      switch (RNA_property_type(prop)) {
        case PROP_BOOLEAN:
          value = (float)RNA_property_boolean_get(&ptr, prop);
          break;
        case PROP_INT:
          value = (float)RNA_property_int_get(&ptr, prop);
          break;
        case PROP_FLOAT:
          value = RNA_property_float_get(&ptr, prop);
          break;
        case PROP_ENUM:
          value = (float)RNA_property_enum_get(&ptr, prop);
          break;
        default:
          fprintf(stderr,
                  "property type %d not supported for Curve %s\n",
                  RNA_property_type(prop),
                  curve_key.get_full_path().c_str());
          return false;
          break;
      }
    }
  }
  else {
    /* path couldn't be resolved */
    fprintf(stderr, "Path not recognized for Curve %s\n", curve_key.get_full_path().c_str());
    return false;
  }

  add_value(value, frame_index);
  return true;
}

void BCAnimationCurve::get_value_map(BCValueMap &value_map)
{
  value_map.clear();
  if (fcurve == nullptr) {
    return;
  }

  for (int i = 0; i < fcurve->totvert; i++) {
    const float frame = fcurve->bezt[i].vec[1][0];
    const float val = fcurve->bezt[i].vec[1][1];
    value_map[frame] = val;
  }
}

void BCAnimationCurve::get_frames(BCFrames &frames) const
{
  frames.clear();
  if (fcurve) {
    for (int i = 0; i < fcurve->totvert; i++) {
      const float val = fcurve->bezt[i].vec[1][0];
      frames.push_back(val);
    }
  }
}

void BCAnimationCurve::get_values(BCValues &values) const
{
  values.clear();
  if (fcurve) {
    for (int i = 0; i < fcurve->totvert; i++) {
      const float val = fcurve->bezt[i].vec[1][1];
      values.push_back(val);
    }
  }
}

bool BCAnimationCurve::is_animated()
{
  static float MIN_DISTANCE = 0.00001;
  return fabs(max - min) > MIN_DISTANCE;
}

bool BCAnimationCurve::is_keyframe(int frame)
{
  if (this->fcurve == nullptr) {
    return false;
  }

  for (int i = 0; i < fcurve->totvert; i++) {
    const int cframe = nearbyint(fcurve->bezt[i].vec[1][0]);
    if (cframe == frame) {
      return true;
    }
    if (cframe > frame) {
      break;
    }
  }
  return false;
}

/* Needed for adding a BCAnimationCurve into a BCAnimationCurveSet */
inline bool operator<(const BCAnimationCurve &lhs, const BCAnimationCurve &rhs)
{
  std::string lhtgt = lhs.get_channel_target();
  std::string rhtgt = rhs.get_channel_target();
  if (lhtgt == rhtgt) {
    const int lha = lhs.get_channel_index();
    const int rha = rhs.get_channel_index();
    return lha < rha;
  }

  return lhtgt < rhtgt;
}

BCCurveKey::BCCurveKey()
{
  this->key_type = BC_ANIMATION_TYPE_OBJECT;
  this->rna_path = "";
  this->curve_array_index = 0;
  this->curve_subindex = -1;
}

BCCurveKey::BCCurveKey(const BC_animation_type type,
                       const std::string path,
                       const int array_index,
                       const int subindex)
{
  this->key_type = type;
  this->rna_path = path;
  this->curve_array_index = array_index;
  this->curve_subindex = subindex;
}

void BCCurveKey::operator=(const BCCurveKey &other)
{
  this->key_type = other.key_type;
  this->rna_path = other.rna_path;
  this->curve_array_index = other.curve_array_index;
  this->curve_subindex = other.curve_subindex;
}

std::string BCCurveKey::get_full_path() const
{
  return this->rna_path + '[' + std::to_string(this->curve_array_index) + ']';
}

std::string BCCurveKey::get_path() const
{
  return this->rna_path;
}

int BCCurveKey::get_array_index() const
{
  return this->curve_array_index;
}

int BCCurveKey::get_subindex() const
{
  return this->curve_subindex;
}

void BCCurveKey::set_object_type(BC_animation_type object_type)
{
  this->key_type = object_type;
}

BC_animation_type BCCurveKey::get_animation_type() const
{
  return this->key_type;
}

bool BCCurveKey::operator<(const BCCurveKey &other) const
{
  /* needed for using this class as key in maps and sets */
  if (this->key_type != other.key_type) {
    return this->key_type < other.key_type;
  }

  if (this->curve_subindex != other.curve_subindex) {
    return this->curve_subindex < other.curve_subindex;
  }

  if (this->rna_path != other.rna_path) {
    return this->rna_path < other.rna_path;
  }

  return this->curve_array_index < other.curve_array_index;
}

BCBezTriple::BCBezTriple(BezTriple &bezt) : bezt(bezt)
{
}

float BCBezTriple::get_frame() const
{
  return bezt.vec[1][0];
}

float BCBezTriple::get_time(Scene *scene) const
{
  return FRA2TIME(bezt.vec[1][0]);
}

float BCBezTriple::get_value() const
{
  return bezt.vec[1][1];
}

float BCBezTriple::get_angle() const
{
  return RAD2DEGF(get_value());
}

void BCBezTriple::get_in_tangent(Scene *scene, float point[2], bool as_angle) const
{
  get_tangent(scene, point, as_angle, 0);
}

void BCBezTriple::get_out_tangent(Scene *scene, float point[2], bool as_angle) const
{
  get_tangent(scene, point, as_angle, 2);
}

void BCBezTriple::get_tangent(Scene *scene, float point[2], bool as_angle, int index) const
{
  point[0] = FRA2TIME(bezt.vec[index][0]);
  if (bezt.ipo != BEZT_IPO_BEZ) {
    /* We're in a mixed interpolation scenario, set zero as it's irrelevant but value might contain
     * unused data */
    point[0] = 0;
    point[1] = 0;
  }
  else if (as_angle) {
    point[1] = RAD2DEGF(bezt.vec[index][1]);
  }
  else {
    point[1] = bezt.vec[index][1];
  }
}