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

nla.c « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d062a2ab14b48bd6a5f262ef2d3ce1715fc2595c (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
/**
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2009 Blender Foundation, Joshua Leung
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): Joshua Leung (full recode)
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <math.h>
#include <float.h>

#include "MEM_guardedalloc.h"

#include "BLI_blenlib.h"

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

#include "BKE_animsys.h"
#include "BKE_action.h"
#include "BKE_fcurve.h"
#include "BKE_nla.h"
#include "BKE_blender.h"
#include "BKE_library.h"
#include "BKE_object.h"
#include "BKE_utildefines.h"


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif


/* *************************************************** */
/* Data Management */

/* Freeing ------------------------------------------- */

/* Remove the given NLA strip from the NLA track it occupies, free the strip's data,
 * and the strip itself. 
 */
// TODO: with things like transitions, should these get freed too? Maybe better as a UI tool
void free_nlastrip (ListBase *strips, NlaStrip *strip)
{
	FModifier *fcm, *fmn;
	
	/* sanity checks */
	if (strip == NULL)
		return;
		
	/* remove reference to action */
	if (strip->act)
		strip->act->id.us--;
		
	/* free remapping info */
	//if (strip->remap)
	//	BKE_animremap_free();
	
	/* free own F-Curves */
	free_fcurves(&strip->fcurves);
	
	/* free F-Modifiers */
	for (fcm= strip->modifiers.first; fcm; fcm= fmn) {
		fmn= fcm->next;
		
		BLI_remlink(&strip->modifiers, fcm);
		fcurve_remove_modifier(NULL, fcm);
	}
	
	/* free the strip itself */
	if (strips)
		BLI_freelinkN(strips, strip);
	else
		MEM_freeN(strip);
}

/* Remove the given NLA track from the set of NLA tracks, free the track's data,
 * and the track itself.
 */
void free_nlatrack (ListBase *tracks, NlaTrack *nlt)
{
	NlaStrip *strip, *stripn;
	
	/* sanity checks */
	if (nlt == NULL)
		return;
		
	/* free strips */
	for (strip= nlt->strips.first; strip; strip= stripn) {
		stripn= strip->next;
		free_nlastrip(&nlt->strips, strip);
	}
	
	/* free NLA track itself now */
	if (tracks)
		BLI_freelinkN(tracks, nlt);
	else
		MEM_freeN(nlt);
}

/* Free the elements of type NLA Tracks provided in the given list, but do not free
 * the list itself since that is not free-standing
 */
void free_nladata (ListBase *tracks)
{
	NlaTrack *nlt, *nltn;
	
	/* sanity checks */
	if ELEM(NULL, tracks, tracks->first)
		return;
		
	/* free tracks one by one */
	for (nlt= tracks->first; nlt; nlt= nltn) {
		nltn= nlt->next;
		free_nlatrack(tracks, nlt);
	}
	
	/* clear the list's pointers to be safe */
	tracks->first= tracks->last= NULL;
}

/* Copying ------------------------------------------- */

/* Copy NLA strip */
NlaStrip *copy_nlastrip (NlaStrip *strip)
{
	NlaStrip *strip_d;
	
	/* sanity check */
	if (strip == NULL)
		return NULL;
		
	/* make a copy */
	strip_d= MEM_dupallocN(strip);
	strip_d->next= strip_d->prev= NULL;
	
	/* increase user-count of action */
	if (strip_d->act)
		strip_d->act->id.us++;
		
	/* copy F-Curves and modifiers */
	copy_fcurves(&strip_d->fcurves, &strip->fcurves);
	fcurve_copy_modifiers(&strip_d->modifiers, &strip->modifiers);
	
	/* return the strip */
	return strip_d;
}

/* Copy NLA Track */
NlaTrack *copy_nlatrack (NlaTrack *nlt)
{
	NlaStrip *strip, *strip_d;
	NlaTrack *nlt_d;
	
	/* sanity check */
	if (nlt == NULL)
		return NULL;
		
	/* make a copy */
	nlt_d= MEM_dupallocN(nlt);
	nlt_d->next= nlt_d->prev= NULL;
	
	/* make a copy of all the strips, one at a time */
	nlt_d->strips.first= nlt_d->strips.last= NULL;
	
	for (strip= nlt->strips.first; strip; strip= strip->next) {
		strip_d= copy_nlastrip(strip);
		BLI_addtail(&nlt_d->strips, strip_d);
	}
	
	/* return the copy */
	return nlt_d;
}

/* Copy all NLA data */
void copy_nladata (ListBase *dst, ListBase *src)
{
	NlaTrack *nlt, *nlt_d;
	
	/* sanity checks */
	if ELEM(NULL, dst, src)
		return;
		
	/* copy each NLA-track, one at a time */
	for (nlt= src->first; nlt; nlt= nlt->next) {
		/* make a copy, and add the copy to the destination list */
		nlt_d= copy_nlatrack(nlt);
		BLI_addtail(dst, nlt_d);
	}
}

/* Adding ------------------------------------------- */

/* Add a NLA Strip referencing the given Action, to the given NLA Track */
// TODO: any extra parameters to control how this is done?
NlaStrip *add_nlastrip (NlaTrack *nlt, bAction *act)
{
	NlaStrip *strip;
	
	/* sanity checks */
	if ELEM(NULL, nlt, act)
		return NULL;
		
	/* allocate new strip */
	strip= MEM_callocN(sizeof(NlaStrip), "NlaStrip");
	BLI_addtail(&nlt->strips, strip);
	
	/* generic settings 
	 *	- selected flag to highlight this to the user
	 *	- auto-blends to ensure that blend in/out values are automatically 
	 *	  determined by overlaps of strips
	 *	- (XXX) synchronisation of strip-length in accordance with changes to action-length
	 *	  is not done though, since this should only really happens in editmode for strips now
	 *	  though this decision is still subject to further review...
	 */
	strip->flag = NLASTRIP_FLAG_SELECT|NLASTRIP_FLAG_AUTO_BLENDS;
	
	/* assign the action reference */
	strip->act= act;
	id_us_plus(&act->id);
	
	/* determine initial range 
	 *	- strip length cannot be 0... ever...
	 */
	calc_action_range(strip->act, &strip->actstart, &strip->actend, 1);
	
	strip->start = strip->actstart;
	strip->end = (IS_EQ(strip->actstart, strip->actend)) ?  (strip->actstart + 1.0f): (strip->actend);
	
	/* strip should be referenced as-is */
	strip->scale= 1.0f;
	strip->repeat = 1.0f;
	
	/* return the new strip */
	return strip;
}

/* Add a NLA Track to the given AnimData */
NlaTrack *add_nlatrack (AnimData *adt)
{
	NlaTrack *nlt;
	
	/* sanity checks */
	if (adt == NULL)
		return NULL;
		
	/* allocate new track */
	nlt= MEM_callocN(sizeof(NlaTrack), "NlaTrack");
	
	/* set settings requiring the track to not be part of the stack yet */
	nlt->flag = NLATRACK_SELECTED;
	nlt->index= BLI_countlist(&adt->nla_tracks);
	
	/* add track to stack, and make it the active one */
	BLI_addtail(&adt->nla_tracks, nlt);
	BKE_nlatrack_set_active(&adt->nla_tracks, nlt);
	
	/* must have unique name, but we need to seed this */
	sprintf(nlt->name, "NlaTrack");
	BLI_uniquename(&adt->nla_tracks, nlt, "NlaTrack", '.', offsetof(NlaTrack, name), 64);
	
	/* return the new track */
	return nlt;
}

/* *************************************************** */
/* Basic Utilities */

/* NLA-Tracks ---------------------------------------- */

/* Find the active NLA-track for the given stack */
NlaTrack *BKE_nlatrack_find_active (ListBase *tracks)
{
	NlaTrack *nlt;
	
	/* sanity check */
	if ELEM(NULL, tracks, tracks->first)
		return NULL;
		
	/* try to find the first active track */
	for (nlt= tracks->first; nlt; nlt= nlt->next) {
		if (nlt->flag & NLATRACK_ACTIVE)
			return nlt;
	}
	
	/* none found */
	return NULL;
}

/* Make the given NLA-track the active one for the given stack. If no track is provided, 
 * this function can be used to simply deactivate all the NLA tracks in the given stack too.
 */
void BKE_nlatrack_set_active (ListBase *tracks, NlaTrack *nlt_a)
{
	NlaTrack *nlt;
	
	/* sanity check */
	if ELEM(NULL, tracks, tracks->first)
		return;
	
	/* deactive all the rest */
	for (nlt= tracks->first; nlt; nlt= nlt->next) 
		nlt->flag &= ~NLATRACK_ACTIVE;
		
	/* set the given one as the active one */
	if (nlt_a)
		nlt_a->flag |= NLATRACK_ACTIVE;
}

/* Check if there is any space in the last track to add the given strip */
short BKE_nlatrack_has_space (NlaTrack *nlt, float start, float end)
{
	NlaStrip *strip;
	
	/* sanity checks */
	if ((nlt == NULL) || IS_EQ(start, end))
		return 0;
	if (start > end) {
		puts("BKE_nlatrack_has_space error... start and end arguments swapped");
		SWAP(float, start, end);
	}
	
	/* loop over NLA strips checking for any overlaps with this area... */
	for (strip= nlt->strips.first; strip; strip= strip->next) {
		/* if start frame of strip is past the target end-frame, that means that
		 * we've gone past the window we need to check for, so things are fine
		 */
		if (strip->start > end)
			return 1;
		
		/* if the end of the strip is greater than either of the boundaries, the range
		 * must fall within the extents of the strip
		 */
		if ((strip->end > start) || (strip->end > end))
			return 0;
	}
	
	/* if we are still here, we haven't encountered any overlapping strips */
	return 1;
}

/* Rearrange the strips in the track so that they are always in order 
 * (usually only needed after a strip has been moved) 
 */
void BKE_nlatrack_sort_strips (NlaTrack *nlt)
{
	ListBase tmp = {NULL, NULL};
	NlaStrip *strip, *sstrip;
	
	/* sanity checks */
	if ELEM(NULL, nlt, nlt->strips.first)
		return;
		
	/* we simply perform insertion sort on this list, since it is assumed that per track,
	 * there are only likely to be at most 5-10 strips
	 */
	for (strip= nlt->strips.first; strip; strip= strip->next) {
		short not_added = 1;
		
		/* remove this strip from the list, and add it to the new list, searching from the end of 
		 * the list, assuming that the lists are in order 
		 */
		BLI_remlink(&nlt->strips, strip);
		
		for (sstrip= tmp.last; not_added && sstrip; sstrip= sstrip->prev) {
			/* check if add after */
			if (sstrip->end < strip->start) {
				BLI_insertlinkafter(&tmp, sstrip, strip);
				not_added= 0;
				break;
			}
		}
		
		/* add before first? */
		if (not_added)
			BLI_addhead(&tmp, strip);
	}
	
	/* reassign the start and end points of the strips */
	nlt->strips.first= tmp.first;
	nlt->strips.last= tmp.last;
}
 
/* Tools ------------------------------------------- */

/* For the given AnimData block, add the active action to the NLA
 * stack (i.e. 'push-down' action). The UI should only allow this 
 * for normal editing only (i.e. not in editmode for some strip's action),
 * so no checks for this are performed.
 */
// TODO: maybe we should have checks for this too...
void BKE_nla_action_pushdown (AnimData *adt)
{
	NlaTrack *nlt;
	NlaStrip *strip;
	
	/* sanity checks */
	// TODO: need to report the error for this
	if ELEM(NULL, adt, adt->action) 
		return;
		
	/* if the action is empty, we also shouldn't try to add to stack, 
	 * as that will cause us grief down the track
	 */
	// TODO: what about modifiers?
	if (action_has_motion(adt->action) == 0)
		return;
		
	/* add a new NLA track to house this action 
	 *	- we could investigate trying to fit the action into an appropriately
	 *	  sized gap in the existing tracks, however, this may result in unexpected 
	 *	  changes in blending behaviour...
	 */
	nlt= add_nlatrack(adt);
	if (nlt == NULL)
		return;
	
	/* add a new NLA strip to the track, which references the active action */
	strip= add_nlastrip(nlt, adt->action);
	
	/* clear reference to action now that we've pushed it onto the stack */
	if (strip) {
		adt->action->id.us--;
		adt->action= NULL;
	}
	
	// TODO: set any other flags necessary here...
}

/* *************************************************** */