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

rna_nla.c « intern « makesrna « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 869c2ac10bfb1774afde26d3c1e9ef510dbc7d44 (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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/** \file
 * \ingroup RNA
 */

#include <stdlib.h>

#include "DNA_action_types.h"
#include "DNA_anim_types.h"
#include "DNA_scene_types.h"

#include "BLI_utildefines.h"

#include "MEM_guardedalloc.h"

#include "RNA_access.h"
#include "RNA_define.h"
#include "RNA_enum_types.h"

#include "rna_internal.h"

#include "WM_api.h"
#include "WM_types.h"

/* enum defines exported for rna_animation.c */
const EnumPropertyItem rna_enum_nla_mode_blend_items[] = {
    {NLASTRIP_MODE_REPLACE,
     "REPLACE",
     0,
     "Replace",
     "The strip values replace the accumulated results by amount specified by influence"},
    {NLASTRIP_MODE_COMBINE,
     "COMBINE",
     0,
     "Combine",
     "The strip values are combined with accumulated results by appropriately using addition, "
     "multiplication, or quaternion math, based on channel type"},
    {0, "", 0, NULL, NULL},
    {NLASTRIP_MODE_ADD,
     "ADD",
     0,
     "Add",
     "Weighted result of strip is added to the accumulated results"},
    {NLASTRIP_MODE_SUBTRACT,
     "SUBTRACT",
     0,
     "Subtract",
     "Weighted result of strip is removed from the accumulated results"},
    {NLASTRIP_MODE_MULTIPLY,
     "MULTIPLY",
     0,
     "Multiply",
     "Weighted result of strip is multiplied with the accumulated results"},
    {0, NULL, 0, NULL, NULL},
};

const EnumPropertyItem rna_enum_nla_mode_extend_items[] = {
    {NLASTRIP_EXTEND_NOTHING, "NOTHING", 0, "Nothing", "Strip has no influence past its extents"},
    {NLASTRIP_EXTEND_HOLD,
     "HOLD",
     0,
     "Hold",
     "Hold the first frame if no previous strips in track, and always hold last frame"},
    {NLASTRIP_EXTEND_HOLD_FORWARD, "HOLD_FORWARD", 0, "Hold Forward", "Only hold last frame"},
    {0, NULL, 0, NULL, NULL},
};

#ifdef RNA_RUNTIME

#  include <math.h>
#  include <stdio.h>

/* needed for some of the validation stuff... */
#  include "BKE_anim_data.h"
#  include "BKE_fcurve.h"
#  include "BKE_nla.h"

#  include "DNA_object_types.h"

#  include "ED_anim_api.h"

#  include "DEG_depsgraph.h"
#  include "DEG_depsgraph_build.h"

/* temp constant defined for these funcs only... */
#  define NLASTRIP_MIN_LEN_THRESH 0.1f

static void rna_NlaStrip_name_set(PointerRNA *ptr, const char *value)
{
  NlaStrip *data = (NlaStrip *)ptr->data;

  /* copy the name first */
  BLI_strncpy_utf8(data->name, value, sizeof(data->name));

  /* validate if there's enough info to do so */
  if (ptr->owner_id) {
    AnimData *adt = BKE_animdata_from_id(ptr->owner_id);
    BKE_nlastrip_validate_name(adt, data);
  }
}

static char *rna_NlaStrip_path(PointerRNA *ptr)
{
  NlaStrip *strip = (NlaStrip *)ptr->data;
  AnimData *adt = BKE_animdata_from_id(ptr->owner_id);

  /* if we're attached to AnimData, try to resolve path back to AnimData */
  if (adt) {
    NlaTrack *nlt;
    NlaStrip *nls;

    for (nlt = adt->nla_tracks.first; nlt; nlt = nlt->next) {
      for (nls = nlt->strips.first; nls; nls = nls->next) {
        if (nls == strip) {
          /* XXX but if we animate like this, the control will never work... */
          char name_esc_nlt[sizeof(nlt->name) * 2];
          char name_esc_strip[sizeof(strip->name) * 2];

          BLI_strescape(name_esc_nlt, nlt->name, sizeof(name_esc_nlt));
          BLI_strescape(name_esc_strip, strip->name, sizeof(name_esc_strip));
          return BLI_sprintfN(
              "animation_data.nla_tracks[\"%s\"].strips[\"%s\"]", name_esc_nlt, name_esc_strip);
        }
      }
    }
  }

  /* no path */
  return BLI_strdup("");
}

static char *rna_NlaStripPreBlendTransform_path(PointerRNA *ptr)
{
  NlaStripPreBlendTransform *preblend_xform = (NlaStripPreBlendTransform *)ptr->data;
  AnimData *adt = BKE_animdata_from_id(ptr->owner_id);

  /* if we're attached to AnimData, try to resolve path back to AnimData */
  if (adt) {
    NlaTrack *nlt;
    NlaStrip *nls;
    NlaStripPreBlendTransform *pbxform;

    int pbxform_index;
    for (nlt = adt->nla_tracks.first; nlt; nlt = nlt->next) {
      for (nls = nlt->strips.first; nls; nls = nls->next) {
        pbxform_index = 0;
        for (pbxform = nls->preblend_transforms.first; pbxform;
             pbxform = pbxform->next, pbxform_index++) {
          if (pbxform == preblend_xform) {
            /* XXX but if we animate like this, the control will never work... */
            char name_esc_nlt[sizeof(nlt->name) * 2];
            char name_esc_nls[sizeof(nls->name) * 2];

            BLI_strescape(name_esc_nlt, nlt->name, sizeof(name_esc_nlt));
            BLI_strescape(name_esc_nls, nls->name, sizeof(name_esc_nls));
            return BLI_sprintfN(
                "animation_data.nla_tracks[\"%s\"].strips[\"%s\"].preblend_transforms[%i]",
                name_esc_nlt,
                name_esc_nls,
                pbxform_index);
          }
        }
      }
    }
  }

  /* no path */
  return BLI_strdup("");
}

static char *rna_NlaStripPreBlendTransform_BoneName_path(PointerRNA *ptr)
{
  NlaStripPreBlendTransform_BoneName *search_bone_name = (NlaStripPreBlendTransform_BoneName *)
                                                             ptr->data;
  AnimData *adt = BKE_animdata_from_id(ptr->owner_id);

  /* if we're attached to AnimData, try to resolve path back to AnimData */
  if (adt) {
    NlaTrack *nlt;
    NlaStrip *nls;
    NlaStripPreBlendTransform *pbxform;
    NlaStripPreBlendTransform_BoneName *bone_name;

    int pbxform_index;
    int bone_index;
    for (nlt = adt->nla_tracks.first; nlt; nlt = nlt->next) {
      for (nls = nlt->strips.first; nls; nls = nls->next) {
        pbxform_index = 0;
        for (pbxform = nls->preblend_transforms.first; pbxform;
             pbxform = pbxform->next, pbxform_index++) {
          bone_index = 0;
          for (bone_name = pbxform->bones.first; bone_name;
               bone_name = bone_name->next, bone_index++) {

            if (bone_name == search_bone_name) {
              /* XXX but if we animate like this, the control will never work... */
              char name_esc_nlt[sizeof(nlt->name) * 2];
              char name_esc_nls[sizeof(nls->name) * 2];

              BLI_strescape(name_esc_nlt, nlt->name, sizeof(name_esc_nlt));
              BLI_strescape(name_esc_nls, nls->name, sizeof(name_esc_nls));
              return BLI_sprintfN(
                  "animation_data.nla_tracks[\"%s\"].strips[\"%s\"].preblend_transforms[%i].bones["
                  "%i]",
                  name_esc_nlt,
                  name_esc_nls,
                  pbxform_index,
                  bone_index);
            }
          }
        }
      }
    }
  }

  /* no path */
  return BLI_strdup("");
}

static void rna_NlaStrip_update(Main *bmain, Scene *UNUSED(scene), PointerRNA *ptr)
{
  ID *id = ptr->owner_id;

  ANIM_id_update(bmain, id);
}

static void rna_NlaStrip_dependency_update(Main *bmain, Scene *scene, PointerRNA *ptr)
{
  DEG_relations_tag_update(bmain);

  rna_NlaStrip_update(bmain, scene, ptr);
}

static void rna_NlaStrip_transform_update(Main *bmain, Scene *scene, PointerRNA *ptr)
{
  NlaStrip *strip = (NlaStrip *)ptr->data;

  BKE_nlameta_flush_transforms(strip);

  /* set the flag */
  if ((strip->flag & NLASTRIP_FLAG_AUTO_BLENDS) && ptr->owner_id) {
    /* validate state to ensure that auto-blend gets applied immediately */
    IdAdtTemplate *iat = (IdAdtTemplate *)ptr->owner_id;

    if (iat->adt) {
      BKE_nla_validate_state(iat->adt);
    }
  }

  rna_NlaStrip_update(bmain, scene, ptr);
}

static void rna_NlaStrip_start_frame_set(PointerRNA *ptr, float value)
{
  NlaStrip *data = (NlaStrip *)ptr->data;

  /* Clamp value to lie within valid limits:
   * - Cannot start past the end of the strip + some flexibility threshold.
   * - Cannot start before the previous strip (if present) ends.
   *   -> But if it was a transition,
   *   we could go up to the start of the strip + some flexibility threshold.
   *   as long as we re-adjust the transition afterwards.
   * - Minimum frame is -MAXFRAME so that we don't get clipping on frame 0.
   */
  if (data->prev) {
    if (data->prev->type == NLASTRIP_TYPE_TRANSITION) {
      CLAMP(
          value, data->prev->start + NLASTRIP_MIN_LEN_THRESH, data->end - NLASTRIP_MIN_LEN_THRESH);

      /* re-adjust the transition to stick to the endpoints of the action-clips */
      data->prev->end = value;
    }
    else {
      CLAMP(value, data->prev->end, data->end - NLASTRIP_MIN_LEN_THRESH);
    }
  }
  else {
    CLAMP(value, MINAFRAME, data->end);
  }
  data->start = value;
}

static void rna_NlaStrip_end_frame_set(PointerRNA *ptr, float value)
{
  NlaStrip *data = (NlaStrip *)ptr->data;

  /* clamp value to lie within valid limits
   * - must not have zero or negative length strip, so cannot start before the first frame
   *   + some minimum-strip-length threshold
   * - cannot end later than the start of the next strip (if present)
   *   -> but if it was a transition,
   *   we could go up to the start of the end - some flexibility threshold
   *   as long as we re-adjust the transition afterwards
   */
  if (data->next) {
    if (data->next->type == NLASTRIP_TYPE_TRANSITION) {
      CLAMP(
          value, data->start + NLASTRIP_MIN_LEN_THRESH, data->next->end - NLASTRIP_MIN_LEN_THRESH);

      /* readjust the transition to stick to the endpoints of the action-clips */
      data->next->start = value;
    }
    else {
      CLAMP(value, data->start + NLASTRIP_MIN_LEN_THRESH, data->next->start);
    }
  }
  else {
    CLAMP(value, data->start + NLASTRIP_MIN_LEN_THRESH, MAXFRAME);
  }
  data->end = value;

  /* calculate the lengths the strip and its action (if applicable) */
  if (data->type == NLASTRIP_TYPE_CLIP) {
    float len, actlen;

    len = data->end - data->start;
    actlen = data->actend - data->actstart;
    if (IS_EQF(actlen, 0.0f)) {
      actlen = 1.0f;
    }

    /* now, adjust the 'scale' setting to reflect this (so that this change can be valid) */
    data->scale = len / ((actlen)*data->repeat);
  }
}

static void rna_NlaStrip_scale_set(PointerRNA *ptr, float value)
{
  NlaStrip *data = (NlaStrip *)ptr->data;

  /* set scale value */
  /* NOTE: these need to be synced with the values in the
   * property definition in rna_def_nlastrip() */
  CLAMP(value, 0.0001f, 1000.0f);
  data->scale = value;

  /* adjust the strip extents in response to this */
  BKE_nlastrip_recalculate_bounds(data);
}

static void rna_NlaStrip_repeat_set(PointerRNA *ptr, float value)
{
  NlaStrip *data = (NlaStrip *)ptr->data;

  /* set repeat value */
  /* NOTE: these need to be synced with the values in the
   * property definition in rna_def_nlastrip() */
  CLAMP(value, 0.01f, 1000.0f);
  data->repeat = value;

  /* adjust the strip extents in response to this */
  BKE_nlastrip_recalculate_bounds(data);
}

static void rna_NlaStrip_blend_in_set(PointerRNA *ptr, float value)
{
  NlaStrip *data = (NlaStrip *)ptr->data;
  float len;

  /* blend-in is limited to the length of the strip, and also cannot overlap with blendout */
  len = (data->end - data->start) - data->blendout;
  CLAMP(value, 0, len);

  data->blendin = value;
}

static void rna_NlaStrip_blend_out_set(PointerRNA *ptr, float value)
{
  NlaStrip *data = (NlaStrip *)ptr->data;
  float len;

  /* blend-out is limited to the length of the strip */
  len = (data->end - data->start);
  CLAMP(value, 0, len);

  /* it also cannot overlap with blendin */
  if ((len - value) < data->blendin) {
    value = len - data->blendin;
  }

  data->blendout = value;
}

static void rna_NlaStrip_use_auto_blend_set(PointerRNA *ptr, bool value)
{
  NlaStrip *data = (NlaStrip *)ptr->data;

  if (value) {
    /* set the flag */
    data->flag |= NLASTRIP_FLAG_AUTO_BLENDS;

    /* validate state to ensure that auto-blend gets applied immediately */
    if (ptr->owner_id) {
      IdAdtTemplate *iat = (IdAdtTemplate *)ptr->owner_id;

      if (iat->adt) {
        BKE_nla_validate_state(iat->adt);
      }
    }
  }
  else {
    /* clear the flag */
    data->flag &= ~NLASTRIP_FLAG_AUTO_BLENDS;

    /* clear the values too, so that it's clear that there has been an effect */
    /* TODO: it's somewhat debatable whether it's better to leave these in instead... */
    data->blendin = 0.0f;
    data->blendout = 0.0f;
  }
}

static int rna_NlaStrip_action_editable(PointerRNA *ptr, const char **UNUSED(r_info))
{
  NlaStrip *strip = (NlaStrip *)ptr->data;

  /* strip actions shouldn't be editable if NLA tweakmode is on */
  if (ptr->owner_id) {
    AnimData *adt = BKE_animdata_from_id(ptr->owner_id);

    if (adt) {
      /* active action is only editable when it is not a tweaking strip */
      if ((adt->flag & ADT_NLA_EDIT_ON) || (adt->actstrip) || (adt->tmpact)) {
        return 0;
      }
    }
  }

  /* check for clues that strip probably shouldn't be used... */
  if (strip->flag & NLASTRIP_FLAG_TWEAKUSER) {
    return 0;
  }

  /* should be ok, though we may still miss some cases */
  return PROP_EDITABLE;
}

static void rna_NlaStrip_action_start_frame_set(PointerRNA *ptr, float value)
{
  NlaStrip *data = (NlaStrip *)ptr->data;

  /* prevent start frame from occurring after end of action */
  CLAMP(value, MINAFRAME, data->actend);
  data->actstart = value;

  /* adjust the strip extents in response to this */
  /* TODO: should the strip be moved backwards instead as a special case? */
  BKE_nlastrip_recalculate_bounds(data);
}

static void rna_NlaStrip_action_end_frame_set(PointerRNA *ptr, float value)
{
  NlaStrip *data = (NlaStrip *)ptr->data;

  /* prevent end frame from starting before start of action */
  CLAMP(value, data->actstart, MAXFRAME);
  data->actend = value;

  /* adjust the strip extents in response to this */
  BKE_nlastrip_recalculate_bounds(data);
}

static void rna_NlaStrip_animated_influence_set(PointerRNA *ptr, bool value)
{
  NlaStrip *data = (NlaStrip *)ptr->data;

  if (value) {
    /* set the flag, then make sure a curve for this exists */
    data->flag |= NLASTRIP_FLAG_USR_INFLUENCE;
    BKE_nlastrip_validate_fcurves(data);
  }
  else {
    data->flag &= ~NLASTRIP_FLAG_USR_INFLUENCE;
  }
}

static void rna_NlaStrip_animated_time_set(PointerRNA *ptr, bool value)
{
  NlaStrip *data = (NlaStrip *)ptr->data;

  if (value) {
    /* set the flag, then make sure a curve for this exists */
    data->flag |= NLASTRIP_FLAG_USR_TIME;
    BKE_nlastrip_validate_fcurves(data);
  }
  else {
    data->flag &= ~NLASTRIP_FLAG_USR_TIME;
  }
}

static FCurve *rna_NlaStrip_fcurve_find(NlaStrip *strip,
                                        ReportList *reports,
                                        const char *data_path,
                                        int index)
{
  if (data_path[0] == '\0') {
    BKE_report(reports, RPT_ERROR, "F-Curve data path empty, invalid argument");
    return NULL;
  }

  /* Returns NULL if not found. */
  return BKE_fcurve_find(&strip->fcurves, data_path, index);
}

static NlaStrip *rna_NlaStrip_new(ID *id,
                                  NlaTrack *track,
                                  Main *bmain,
                                  bContext *C,
                                  ReportList *reports,
                                  const char *UNUSED(name),
                                  int start,
                                  bAction *action)
{
  NlaStrip *strip = BKE_nlastrip_new(action);

  if (strip == NULL) {
    BKE_report(reports, RPT_ERROR, "Unable to create new strip");
    return NULL;
  }

  strip->end += (start - strip->start);
  strip->start = start;

  if (BKE_nlastrips_add_strip(&track->strips, strip) == 0) {
    BKE_report(
        reports,
        RPT_ERROR,
        "Unable to add strip (the track does not have any space to accommodate this new strip)");
    BKE_nlastrip_free(NULL, strip, true);
    return NULL;
  }

  /* create dummy AnimData block so that BKE_nlastrip_validate_name()
   * can be used to ensure a valid name, as we don't have one here...
   * - only the nla_tracks list is needed there, which we aim to reverse engineer here...
   */
  {
    AnimData adt = {NULL};
    NlaTrack *nlt, *nlt_p;

    /* 'first' NLA track is found by going back up chain of given
     * track's parents until we fall off. */
    nlt_p = track;
    nlt = track;
    while ((nlt = nlt->prev) != NULL) {
      nlt_p = nlt;
    }
    adt.nla_tracks.first = nlt_p;

    /* do the same thing to find the last track */
    nlt_p = track;
    nlt = track;
    while ((nlt = nlt->next) != NULL) {
      nlt_p = nlt;
    }
    adt.nla_tracks.last = nlt_p;

    /* now we can just auto-name as usual */
    BKE_nlastrip_validate_name(&adt, strip);
  }

  WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_ADDED, NULL);

  DEG_relations_tag_update(bmain);
  DEG_id_tag_update_ex(bmain, id, ID_RECALC_ANIMATION | ID_RECALC_COPY_ON_WRITE);

  return strip;
}

static void rna_NlaStrip_remove(
    ID *id, NlaTrack *track, Main *bmain, bContext *C, ReportList *reports, PointerRNA *strip_ptr)
{
  NlaStrip *strip = strip_ptr->data;
  if (BLI_findindex(&track->strips, strip) == -1) {
    BKE_reportf(
        reports, RPT_ERROR, "NLA strip '%s' not found in track '%s'", strip->name, track->name);
    return;
  }

  BKE_nlastrip_free(&track->strips, strip, true);
  RNA_POINTER_INVALIDATE(strip_ptr);

  WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_REMOVED, NULL);

  DEG_relations_tag_update(bmain);
  DEG_id_tag_update_ex(bmain, id, ID_RECALC_ANIMATION | ID_RECALC_COPY_ON_WRITE);
}

/* Set the 'solo' setting for the given NLA-track, making sure that it is the only one
 * that has this status in its AnimData block.
 */
static void rna_NlaTrack_solo_set(PointerRNA *ptr, bool value)
{
  NlaTrack *data = (NlaTrack *)ptr->data;
  AnimData *adt = BKE_animdata_from_id(ptr->owner_id);
  NlaTrack *nt;

  if (data == NULL) {
    return;
  }

  /* firstly, make sure 'solo' flag for all tracks is disabled */
  for (nt = data; nt; nt = nt->next) {
    nt->flag &= ~NLATRACK_SOLO;
  }
  for (nt = data; nt; nt = nt->prev) {
    nt->flag &= ~NLATRACK_SOLO;
  }

  /* now, enable 'solo' for the given track if appropriate */
  if (value) {
    /* set solo status */
    data->flag |= NLATRACK_SOLO;

    /* set solo-status on AnimData */
    adt->flag |= ADT_NLA_SOLO_TRACK;
  }
  else {
    /* solo status was already cleared on track */

    /* clear solo-status on AnimData */
    adt->flag &= ~ADT_NLA_SOLO_TRACK;
  }
}

static void rna_nlastrip_blendXform_bone_name_set(PointerRNA *ptr, const char *value)
{
  NlaStripPreBlendTransform_BoneName *pbxform_bone_name = (NlaStripPreBlendTransform_BoneName *)
                                                              ptr->data;
  BLI_strncpy_utf8(pbxform_bone_name->name, value, sizeof(pbxform_bone_name->name));
}

static NlaStripPreBlendTransform *rna_NlaStrip_preblend_new(NlaStrip *strip, ReportList *reports)
{
  return BKE_nlastrip_new_preblend_transform(strip);
}

static void rna_NlaStrip_preblend_remove(NlaStrip *strip,
                                         ReportList *reports,
                                         PointerRNA *preblend)
{
  BKE_nlastrip_free_preblend_transform(strip, (NlaStripPreBlendTransform *)preblend->data);
}
static void rna_NlaStrip_preblend_remove_at(NlaStrip *strip,
                                            ReportList *reports,
                                            int preblend_index)
{
  BKE_nlastrip_free_preblend_transform_at(strip, preblend_index);
}

static NlaStripPreBlendTransform_BoneName *rna_preblend_bone_name_new(
    NlaStripPreBlendTransform *preblend, ReportList *reports)
{
  return BKE_preblend_transform_new_bone(preblend);
}

static void rna_preblend_bone_name_remove(NlaStripPreBlendTransform *preblend,
                                          ReportList *reports,
                                          PointerRNA *bone_name)
{
  BKE_preblend_transform_free_bone(preblend,
                                   (NlaStripPreBlendTransform_BoneName *)bone_name->data);
}
static void rna_preblend_bone_name_remove_at(NlaStripPreBlendTransform *preblend,
                                             ReportList *reports,
                                             int bone_name_index)
{
  BKE_preblend_transform_free_bone_at(preblend, bone_name_index);
}

#else

static void rna_def_strip_fcurves(BlenderRNA *brna, PropertyRNA *cprop)
{
  StructRNA *srna;

  FunctionRNA *func;
  PropertyRNA *parm;

  RNA_def_property_srna(cprop, "NlaStripFCurves");
  srna = RNA_def_struct(brna, "NlaStripFCurves", NULL);
  RNA_def_struct_sdna(srna, "NlaStrip");
  RNA_def_struct_ui_text(srna, "NLA-Strip F-Curves", "Collection of NLA strip F-Curves");

  /* Strip.fcurves.find(...) */
  func = RNA_def_function(srna, "find", "rna_NlaStrip_fcurve_find");
  RNA_def_function_ui_description(
      func,
      "Find an F-Curve. Note that this function performs a linear scan "
      "of all F-Curves in the NLA strip.");
  RNA_def_function_flag(func, FUNC_USE_REPORTS);
  parm = RNA_def_string(func, "data_path", NULL, 0, "Data Path", "F-Curve data path");
  RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
  RNA_def_int(func, "index", 0, 0, INT_MAX, "Index", "Array index", 0, INT_MAX);

  parm = RNA_def_pointer(
      func, "fcurve", "FCurve", "", "The found F-Curve, or None if it doesn't exist");
  RNA_def_function_return(func, parm);
}

static void rna_def_nlastrip_blendXforms(BlenderRNA *brna, PropertyRNA *cprop)
{
  StructRNA *srna;
  PropertyRNA *prop;

  FunctionRNA *func;
  PropertyRNA *parm;

  RNA_def_property_srna(cprop, "NlaStripPreBlendTransforms");
  srna = RNA_def_struct(brna, "NlaStripPreBlendTransforms", NULL);
  RNA_def_struct_sdna(srna, "NlaStrip");
  RNA_def_struct_ui_text(
      srna, "Nla Strip Preblend Transforms", "Collection of Preblend Transforms");

  /* add target */
  func = RNA_def_function(srna, "add", "rna_NlaStrip_preblend_new");
  RNA_def_function_flag(func, FUNC_USE_REPORTS);
  RNA_def_function_ui_description(func, "Add a new bone");
  /* return type */
  parm = RNA_def_pointer(func, "preblend", "NlaStripPreBlendTransform", "", "");
  RNA_def_function_return(func, parm);

  /* remove target */
  func = RNA_def_function(srna, "remove", "rna_NlaStrip_preblend_remove");
  RNA_def_function_flag(func, FUNC_USE_REPORTS);
  RNA_def_function_ui_description(func, "Remove an existing bone from the armature");

  /* target to remove*/
  parm = RNA_def_pointer(
      func, "preblend", "NlaStripPreBlendTransform", "", "NlaStripPreBlendTransform to remove");
  RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED | PARM_RNAPTR);
  RNA_def_parameter_clear_flags(parm, PROP_THICK_WRAP, 0);

  /* remove target */
  func = RNA_def_function(srna, "remove_at", "rna_NlaStrip_preblend_remove_at");
  RNA_def_function_flag(func, FUNC_USE_REPORTS);
  RNA_def_function_ui_description(func, "Remove an existing bone from the armature");

  /* target to remove*/
  parm = RNA_def_int(func, "preblend_index", 0, 0, INT_MAX, "preblend_index", "", 0, INT_MAX);
}

static void rna_def_nlastrip(BlenderRNA *brna)
{
  StructRNA *srna;
  PropertyRNA *prop;

  /* enum defs */
  static const EnumPropertyItem prop_type_items[] = {
      {NLASTRIP_TYPE_CLIP, "CLIP", 0, "Action Clip", "NLA Strip references some Action"},
      {NLASTRIP_TYPE_TRANSITION,
       "TRANSITION",
       0,
       "Transition",
       "NLA Strip 'transitions' between adjacent strips"},
      {NLASTRIP_TYPE_META, "META", 0, "Meta", "NLA Strip acts as a container for adjacent strips"},
      {NLASTRIP_TYPE_SOUND,
       "SOUND",
       0,
       "Sound Clip",
       "NLA Strip representing a sound event for speakers"},
      {0, NULL, 0, NULL, NULL},
  };

  /* struct definition */
  srna = RNA_def_struct(brna, "NlaStrip", NULL);
  RNA_def_struct_ui_text(srna, "NLA Strip", "A container referencing an existing Action");
  RNA_def_struct_path_func(srna, "rna_NlaStrip_path");
  RNA_def_struct_ui_icon(srna, ICON_NLA); /* XXX */

  RNA_define_lib_overridable(true);

  /* name property */
  prop = RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
  RNA_def_property_ui_text(prop, "Name", "");
  RNA_def_property_string_funcs(prop, NULL, NULL, "rna_NlaStrip_name_set");
  RNA_def_struct_name_property(srna, prop);
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA, NULL); /* this will do? */

  /* Enums */
  prop = RNA_def_property(srna, "type", PROP_ENUM, PROP_NONE);
  RNA_def_property_enum_sdna(prop, NULL, "type");
  RNA_def_property_clear_flag(
      prop, PROP_EDITABLE); /* XXX for now, not editable, since this is dangerous */
  RNA_def_property_enum_items(prop, prop_type_items);
  RNA_def_property_ui_text(prop, "Type", "Type of NLA Strip");
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_update");

  prop = RNA_def_property(srna, "extrapolation", PROP_ENUM, PROP_NONE);
  RNA_def_property_enum_sdna(prop, NULL, "extendmode");
  RNA_def_property_enum_items(prop, rna_enum_nla_mode_extend_items);
  RNA_def_property_ui_text(
      prop, "Extrapolation", "Action to take for gaps past the strip extents");
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_update");

  prop = RNA_def_property(srna, "blend_type", PROP_ENUM, PROP_NONE);
  RNA_def_property_enum_sdna(prop, NULL, "blendmode");
  RNA_def_property_enum_items(prop, rna_enum_nla_mode_blend_items);
  RNA_def_property_ui_text(
      prop, "Blending", "Method used for combining strip's result with accumulated result");
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_update");

  /* Strip extents */
  prop = RNA_def_property(srna, "frame_start", PROP_FLOAT, PROP_TIME);
  RNA_def_property_float_sdna(prop, NULL, "start");
  RNA_def_property_float_funcs(prop, NULL, "rna_NlaStrip_start_frame_set", NULL);
  RNA_def_property_ui_text(prop, "Start Frame", "");
  RNA_def_property_update(
      prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_transform_update");

  prop = RNA_def_property(srna, "frame_end", PROP_FLOAT, PROP_TIME);
  RNA_def_property_float_sdna(prop, NULL, "end");
  RNA_def_property_float_funcs(prop, NULL, "rna_NlaStrip_end_frame_set", NULL);
  RNA_def_property_ui_text(prop, "End Frame", "");
  RNA_def_property_update(
      prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_transform_update");

  /* Blending */
  prop = RNA_def_property(srna, "blend_in", PROP_FLOAT, PROP_NONE);
  RNA_def_property_float_sdna(prop, NULL, "blendin");
  RNA_def_property_float_funcs(prop, NULL, "rna_NlaStrip_blend_in_set", NULL);
  RNA_def_property_ui_text(
      prop, "Blend In", "Number of frames at start of strip to fade in influence");
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_update");

  prop = RNA_def_property(srna, "blend_out", PROP_FLOAT, PROP_NONE);
  RNA_def_property_float_sdna(prop, NULL, "blendout");
  RNA_def_property_float_funcs(prop, NULL, "rna_NlaStrip_blend_out_set", NULL);
  RNA_def_property_ui_text(prop, "Blend Out", "");
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_update");

  prop = RNA_def_property(srna, "use_auto_blend", PROP_BOOLEAN, PROP_NONE);
  RNA_def_property_boolean_sdna(prop, NULL, "flag", NLASTRIP_FLAG_AUTO_BLENDS);
  RNA_def_property_boolean_funcs(prop, NULL, "rna_NlaStrip_use_auto_blend_set");
  RNA_def_property_ui_text(prop,
                           "Auto Blend In/Out",
                           "Number of frames for Blending In/Out is automatically determined from "
                           "overlapping strips");
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_update");

  /* Action */
  prop = RNA_def_property(srna, "action", PROP_POINTER, PROP_NONE);
  RNA_def_property_pointer_sdna(prop, NULL, "act");
  RNA_def_property_pointer_funcs(prop, NULL, NULL, NULL, "rna_Action_id_poll");
  RNA_def_property_flag(prop, PROP_EDITABLE | PROP_ID_REFCOUNT);
  RNA_def_property_editable_func(prop, "rna_NlaStrip_action_editable");
  RNA_def_property_ui_text(prop, "Action", "Action referenced by this strip");
  RNA_def_property_update(
      prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_dependency_update");

  /* Action extents */
  prop = RNA_def_property(srna, "action_frame_start", PROP_FLOAT, PROP_TIME);
  RNA_def_property_float_sdna(prop, NULL, "actstart");
  RNA_def_property_float_funcs(prop, NULL, "rna_NlaStrip_action_start_frame_set", NULL);
  RNA_def_property_ui_text(prop, "Action Start Frame", "First frame from action to use");
  RNA_def_property_update(
      prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_transform_update");

  prop = RNA_def_property(srna, "action_frame_end", PROP_FLOAT, PROP_TIME);
  RNA_def_property_float_sdna(prop, NULL, "actend");
  RNA_def_property_float_funcs(prop, NULL, "rna_NlaStrip_action_end_frame_set", NULL);
  RNA_def_property_ui_text(prop, "Action End Frame", "Last frame from action to use");
  RNA_def_property_update(
      prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_transform_update");

  /* Action Reuse */
  prop = RNA_def_property(srna, "repeat", PROP_FLOAT, PROP_NONE);
  RNA_def_property_float_sdna(prop, NULL, "repeat");
  RNA_def_property_float_funcs(prop, NULL, "rna_NlaStrip_repeat_set", NULL);
  /* these limits have currently be chosen arbitrarily, but could be extended
   * (minimum should still be > 0 though) if needed... */
  RNA_def_property_range(prop, 0.1f, 1000.0f);
  RNA_def_property_ui_text(prop, "Repeat", "Number of times to repeat the action range");
  RNA_def_property_update(
      prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_transform_update");

  prop = RNA_def_property(srna, "scale", PROP_FLOAT, PROP_NONE);
  RNA_def_property_float_sdna(prop, NULL, "scale");
  RNA_def_property_float_funcs(prop, NULL, "rna_NlaStrip_scale_set", NULL);
  /* these limits can be extended, but beyond this, we can get some crazy+annoying bugs
   * due to numeric errors */
  RNA_def_property_range(prop, 0.0001f, 1000.0f);
  RNA_def_property_ui_text(prop, "Scale", "Scaling factor for action");
  RNA_def_property_update(
      prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_transform_update");

  /* Strip's F-Curves */
  prop = RNA_def_property(srna, "fcurves", PROP_COLLECTION, PROP_NONE);
  RNA_def_property_collection_sdna(prop, NULL, "fcurves", NULL);
  RNA_def_property_struct_type(prop, "FCurve");
  RNA_def_property_ui_text(
      prop, "F-Curves", "F-Curves for controlling the strip's influence and timing");
  rna_def_strip_fcurves(brna, prop);

  /* Strip's F-Modifiers */
  prop = RNA_def_property(srna, "modifiers", PROP_COLLECTION, PROP_NONE);
  RNA_def_property_struct_type(prop, "FModifier");
  RNA_def_property_ui_text(
      prop, "Modifiers", "Modifiers affecting all the F-Curves in the referenced Action");

  /* Strip's Sub-Strips (for Meta-Strips) */
  prop = RNA_def_property(srna, "strips", PROP_COLLECTION, PROP_NONE);
  RNA_def_property_struct_type(prop, "NlaStrip");
  RNA_def_property_ui_text(
      prop,
      "NLA Strips",
      "NLA Strips that this strip acts as a container for (if it is of type Meta)");

  /* Settings - Values necessary for evaluation */
  prop = RNA_def_property(srna, "influence", PROP_FLOAT, PROP_FACTOR);
  RNA_def_property_range(prop, 0.0f, 1.0f);
  RNA_def_property_ui_text(
      prop, "Influence", "Amount the strip contributes to the current result");
  /* XXX: Update temporarily disabled so that the property can be edited at all!
   * Even autokey only applies after the curves have been re-evaluated,
   * causing the unkeyed values to be lost. */
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA | NA_EDITED, /*"rna_NlaStrip_update"*/ NULL);

  prop = RNA_def_property(srna, "strip_time", PROP_FLOAT, PROP_TIME);
  RNA_def_property_ui_text(prop, "Strip Time", "Frame of referenced Action to evaluate");
  /* XXX: Update temporarily disabled so that the property can be edited at all!
   * Even autokey only applies after the curves have been re-evaluated,
   * causing the unkeyed values to be lost. */
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA | NA_EDITED, /*"rna_NlaStrip_update"*/ NULL);

  /* TODO: should the animated_influence/time settings be animatable themselves? */
  prop = RNA_def_property(srna, "use_animated_influence", PROP_BOOLEAN, PROP_NONE);
  RNA_def_property_boolean_sdna(prop, NULL, "flag", NLASTRIP_FLAG_USR_INFLUENCE);
  RNA_def_property_boolean_funcs(prop, NULL, "rna_NlaStrip_animated_influence_set");
  RNA_def_property_ui_text(
      prop,
      "Animated Influence",
      "Influence setting is controlled by an F-Curve rather than automatically determined");
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_update");

  prop = RNA_def_property(srna, "use_animated_time", PROP_BOOLEAN, PROP_NONE);
  RNA_def_property_boolean_sdna(prop, NULL, "flag", NLASTRIP_FLAG_USR_TIME);
  RNA_def_property_boolean_funcs(prop, NULL, "rna_NlaStrip_animated_time_set");
  RNA_def_property_ui_text(
      prop,
      "Animated Strip Time",
      "Strip time is controlled by an F-Curve rather than automatically determined");
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_update");

  prop = RNA_def_property(srna, "use_animated_time_cyclic", PROP_BOOLEAN, PROP_NONE);
  RNA_def_property_boolean_sdna(prop, NULL, "flag", NLASTRIP_FLAG_USR_TIME_CYCLIC);
  RNA_def_property_ui_text(
      prop, "Cyclic Strip Time", "Cycle the animated time within the action start & end");
  RNA_def_property_update(
      prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_transform_update");

  /* settings */
  prop = RNA_def_property(srna, "active", PROP_BOOLEAN, PROP_NONE);
  /* can be made editable by hooking it up to the necessary NLA API methods */
  RNA_def_property_clear_flag(prop, PROP_EDITABLE);
  RNA_def_property_boolean_sdna(prop, NULL, "flag", NLASTRIP_FLAG_ACTIVE);
  RNA_def_property_ui_text(prop, "Active", "NLA Strip is active");
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA, NULL); /* this will do? */

  prop = RNA_def_property(srna, "select", PROP_BOOLEAN, PROP_NONE);
  RNA_def_property_boolean_sdna(prop, NULL, "flag", NLASTRIP_FLAG_SELECT);
  RNA_def_property_ui_text(prop, "Select", "NLA Strip is selected");
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA, NULL); /* this will do? */

  prop = RNA_def_property(srna, "mute", PROP_BOOLEAN, PROP_NONE);
  RNA_def_property_boolean_sdna(prop, NULL, "flag", NLASTRIP_FLAG_MUTED);
  RNA_def_property_ui_icon(prop, ICON_CHECKBOX_HLT, -1);
  RNA_def_property_ui_text(prop, "Mute", "Disable NLA Strip evaluation");
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_update");

  prop = RNA_def_property(srna, "use_reverse", PROP_BOOLEAN, PROP_NONE);
  RNA_def_property_boolean_sdna(prop, NULL, "flag", NLASTRIP_FLAG_REVERSE);
  RNA_def_property_ui_text(prop,
                           "Reversed",
                           "NLA Strip is played back in reverse order (only when timing is "
                           "automatically determined)");
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_update");

  prop = RNA_def_property(srna, "use_sync_length", PROP_BOOLEAN, PROP_NONE);
  RNA_def_property_boolean_sdna(prop, NULL, "flag", NLASTRIP_FLAG_SYNC_LENGTH);
  RNA_def_property_ui_text(prop,
                           "Sync Action Length",
                           "Update range of frames referenced from action "
                           "after tweaking strip and its keyframes");
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_update");

  prop = RNA_def_property(srna, "use_alignment", PROP_BOOLEAN, PROP_NONE);
  RNA_def_property_boolean_sdna(prop, NULL, "flag", NLASTRIP_FLAG_ALIGNED);
  RNA_def_property_ui_text(
      prop, "Use Alignment", "Mark strip as aligned with other marked overlapping strips");

  prop = RNA_def_property(srna, "preblend_transforms", PROP_COLLECTION, PROP_NONE);
  RNA_def_property_collection_sdna(prop, NULL, "preblend_transforms", NULL);
  RNA_def_property_struct_type(prop, "NlaStripPreBlendTransform");
  RNA_def_property_ui_text(prop, "PreBlendTransforms", "");
  rna_def_nlastrip_blendXforms(
      brna, prop);  // GG: todo: test to ensure add/removal works before copying to bones

  RNA_define_lib_overridable(false);
}

static void rna_api_nlatrack_strips(BlenderRNA *brna, PropertyRNA *cprop)
{
  StructRNA *srna;
  PropertyRNA *parm;
  FunctionRNA *func;

  RNA_def_property_srna(cprop, "NlaStrips");
  srna = RNA_def_struct(brna, "NlaStrips", NULL);
  RNA_def_struct_sdna(srna, "NlaTrack");
  RNA_def_struct_ui_text(srna, "Nla Strips", "Collection of Nla Strips");

  func = RNA_def_function(srna, "new", "rna_NlaStrip_new");
  RNA_def_function_flag(func,
                        FUNC_USE_SELF_ID | FUNC_USE_MAIN | FUNC_USE_CONTEXT | FUNC_USE_REPORTS);
  RNA_def_function_ui_description(func, "Add a new Action-Clip strip to the track");
  parm = RNA_def_string(func, "name", "NlaStrip", 0, "", "Name for the NLA Strips");
  RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
  parm = RNA_def_int(func,
                     "start",
                     0,
                     INT_MIN,
                     INT_MAX,
                     "Start Frame",
                     "Start frame for this strip",
                     INT_MIN,
                     INT_MAX);
  RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
  parm = RNA_def_pointer(func, "action", "Action", "", "Action to assign to this strip");
  RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
  /* return type */
  parm = RNA_def_pointer(func, "strip", "NlaStrip", "", "New NLA Strip");
  RNA_def_function_return(func, parm);

  func = RNA_def_function(srna, "remove", "rna_NlaStrip_remove");
  RNA_def_function_flag(func,
                        FUNC_USE_SELF_ID | FUNC_USE_MAIN | FUNC_USE_CONTEXT | FUNC_USE_REPORTS);
  RNA_def_function_ui_description(func, "Remove a NLA Strip");
  parm = RNA_def_pointer(func, "strip", "NlaStrip", "", "NLA Strip to remove");
  RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED | PARM_RNAPTR);
  RNA_def_parameter_clear_flags(parm, PROP_THICK_WRAP, 0);
}

static void rna_def_nlatrack(BlenderRNA *brna)
{
  StructRNA *srna;
  PropertyRNA *prop;

  srna = RNA_def_struct(brna, "NlaTrack", NULL);
  RNA_def_struct_ui_text(
      srna, "NLA Track", "A animation layer containing Actions referenced as NLA strips");
  RNA_def_struct_ui_icon(srna, ICON_NLA);

  /* strips collection */
  prop = RNA_def_property(srna, "strips", PROP_COLLECTION, PROP_NONE);
  RNA_def_property_struct_type(prop, "NlaStrip");
  /* We do not support inserting or removing strips in overrides of tracks for now. */
  RNA_def_property_override_flag(prop, PROPOVERRIDE_OVERRIDABLE_LIBRARY);
  RNA_def_property_ui_text(prop, "NLA Strips", "NLA Strips on this NLA-track");

  rna_api_nlatrack_strips(brna, prop);

  RNA_define_lib_overridable(true);

  /* name property */
  prop = RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
  RNA_def_property_ui_text(prop, "Name", "");
  RNA_def_struct_name_property(srna, prop);
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA, NULL); /* this will do? */

  /* settings */
  prop = RNA_def_property(srna, "active", PROP_BOOLEAN, PROP_NONE);
  /* can be made editable by hooking it up to the necessary NLA API methods */
  RNA_def_property_clear_flag(prop, PROP_EDITABLE);
  RNA_def_property_boolean_sdna(prop, NULL, "flag", NLATRACK_ACTIVE);
  RNA_def_property_ui_text(prop, "Active", "NLA Track is active");
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA, NULL); /* this will do? */

  prop = RNA_def_property(srna, "is_solo", PROP_BOOLEAN, PROP_NONE);
  /* can be made editable by hooking it up to the necessary NLA API methods */
  RNA_def_property_boolean_sdna(prop, NULL, "flag", NLATRACK_SOLO);
  RNA_def_property_ui_text(
      prop,
      "Solo",
      "NLA Track is evaluated itself (i.e. active Action and all other NLA Tracks in the "
      "same AnimData block are disabled)");
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_update");
  RNA_def_property_boolean_funcs(prop, NULL, "rna_NlaTrack_solo_set");

  prop = RNA_def_property(srna, "select", PROP_BOOLEAN, PROP_NONE);
  RNA_def_property_boolean_sdna(prop, NULL, "flag", NLATRACK_SELECTED);
  RNA_def_property_ui_text(prop, "Select", "NLA Track is selected");
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA, NULL); /* this will do? */

  prop = RNA_def_property(srna, "mute", PROP_BOOLEAN, PROP_NONE);
  RNA_def_property_boolean_sdna(prop, NULL, "flag", NLATRACK_MUTED);
  RNA_def_property_ui_text(prop, "Muted", "Disable NLA Track evaluation");
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_update");

  prop = RNA_def_property(srna, "lock", PROP_BOOLEAN, PROP_NONE);
  RNA_def_property_boolean_sdna(prop, NULL, "flag", NLATRACK_PROTECTED);
  RNA_def_property_ui_text(prop, "Locked", "NLA Track is locked");
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA, NULL); /* this will do? */

  RNA_define_lib_overridable(false);
}
static void rna_def_preblend_bones(BlenderRNA *brna, PropertyRNA *cprop)
{
  StructRNA *srna;
  PropertyRNA *prop;

  FunctionRNA *func;
  PropertyRNA *parm;

  RNA_def_property_srna(cprop, "NlaStripPreBlendTransform_BoneNames");
  srna = RNA_def_struct(brna, "NlaStripPreBlendTransform_BoneNames", NULL);
  RNA_def_struct_sdna(srna, "NlaStripPreBlendTransform");
  RNA_def_struct_ui_text(srna,
                         "Nla Strip Preblend Transform Bone Names",
                         "Collection of Preblend Transform Bone Names");

  /* add target */
  func = RNA_def_function(srna, "add", "rna_preblend_bone_name_new");
  RNA_def_function_flag(func, FUNC_USE_REPORTS);
  RNA_def_function_ui_description(func, "Add a new bone");
  /* return type */
  parm = RNA_def_pointer(func, "bone_name", "NlaStripPreBlendTransform_BoneName", "", "");
  RNA_def_function_return(func, parm);

  /* remove target */
  func = RNA_def_function(srna, "remove", "rna_preblend_bone_name_remove");
  RNA_def_function_flag(func, FUNC_USE_REPORTS);
  RNA_def_function_ui_description(func, "Remove an existing bone from the armature");

  /* target to remove*/
  parm = RNA_def_pointer(func,
                         "bone_name",
                         "NlaStripPreBlendTransform_BoneName",
                         "",
                         "NlaStripPreBlendTransform to remove");
  RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED | PARM_RNAPTR);
  RNA_def_parameter_clear_flags(parm, PROP_THICK_WRAP, 0);

  /* remove target */
  func = RNA_def_function(srna, "remove_at", "rna_preblend_bone_name_remove_at");
  RNA_def_function_flag(func, FUNC_USE_REPORTS);
  RNA_def_function_ui_description(func, "Remove an existing bone from the armature");

  /* target to remove*/
  parm = RNA_def_int(func, "preblend_index", 0, 0, INT_MAX, "preblend_index", "", 0, INT_MAX);
}

static void rna_def_nlastrip_blendXform(BlenderRNA *brna)
{
  StructRNA *srna;
  PropertyRNA *prop;

  /* struct definition */
  srna = RNA_def_struct(brna, "NlaStripPreBlendTransform", NULL);
  RNA_def_struct_ui_text(srna, "NLA Strip Pre-Blend Transform", "");
  RNA_def_struct_path_func(srna, "rna_NlaStripPreBlendTransform_path");
  RNA_def_struct_ui_icon(srna, ICON_GIZMO); /* XXX */

  /* name property */
  /** Note: should not be animated */
  prop = RNA_def_property(srna, "location", PROP_FLOAT, PROP_TRANSLATION);
  RNA_def_property_float_sdna(prop, NULL, "location");
  RNA_def_property_ui_text(prop, "Location", "");
  RNA_def_property_ui_range(prop, -FLT_MAX, FLT_MAX, 1, RNA_TRANSLATION_PREC_DEFAULT);
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_update");

  prop = RNA_def_property(srna, "euler", PROP_FLOAT, PROP_EULER);
  RNA_def_property_float_sdna(prop, NULL, "euler");
  RNA_def_property_ui_text(prop, "Rotation", "");
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_update");

  prop = RNA_def_property(srna, "scale", PROP_FLOAT, PROP_XYZ);
  RNA_def_property_flag(prop, PROP_PROPORTIONAL);
  RNA_def_property_ui_range(prop, -FLT_MAX, FLT_MAX, 1, 3);
  RNA_def_property_ui_text(prop, "Scale", "");
  RNA_def_property_update(prop, NC_ANIMATION | ND_NLA | NA_EDITED, "rna_NlaStrip_update");

  prop = RNA_def_property(srna, "bones", PROP_COLLECTION, PROP_NONE);
  RNA_def_property_struct_type(prop, "Bone");
  RNA_def_property_ui_text(prop, "Bones", "Bones to apply pre-blend transform to before blending");
  rna_def_preblend_bones(brna, prop);
}

static void rna_def_nlastrip_blendXform_bone_name(BlenderRNA *brna)
{
  StructRNA *srna;
  PropertyRNA *prop;

  /* struct definition */
  srna = RNA_def_struct(brna, "NlaStripPreBlendTransform_BoneName", NULL);
  RNA_def_struct_ui_text(srna, "Bone Name", "");
  RNA_def_struct_path_func(srna, "rna_NlaStripPreBlendTransform_BoneName_path");
  RNA_def_struct_ui_icon(srna, ICON_GIZMO); /* XXX */

  prop = RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
  RNA_def_property_string_sdna(prop, NULL, "name");
  RNA_def_property_ui_text(prop, "Name", "Bone Name");
  RNA_def_struct_name_property(srna, prop);
  RNA_def_property_string_funcs(prop, NULL, NULL, "rna_nlastrip_blendXform_bone_name_set");
}
/* --------- */

void RNA_def_nla(BlenderRNA *brna)
{
  rna_def_nlatrack(brna);
  rna_def_nlastrip(brna);
  rna_def_nlastrip_blendXform(brna);
  rna_def_nlastrip_blendXform_bone_name(brna);
}

#endif