Sequencer unique names for duplicates & metas and name new strips when the operator...
[blender-staging.git] / source / blender / editors / space_sequencer / sequencer_add.c
1 /**
2  *
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * Contributor(s): Blender Foundation, 2003-2009, Campbell Barton
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 #include <stdlib.h>
28 #include <math.h>
29 #include <string.h>
30
31 #ifndef WIN32
32 #include <unistd.h>
33 #else
34 #include <io.h>
35 #endif
36 #include <sys/types.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_math.h"
42 #include "BLI_storage_types.h"
43
44 #include "IMB_imbuf_types.h"
45 #include "IMB_imbuf.h"
46
47 #include "DNA_ipo_types.h"
48 #include "DNA_curve_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_space_types.h"
52 #include "DNA_sequence_types.h"
53 #include "DNA_view2d_types.h"
54 #include "DNA_userdef_types.h"
55 #include "DNA_sound_types.h"
56
57 #include "BKE_context.h"
58 #include "BKE_global.h"
59 #include "BKE_image.h"
60 #include "BKE_library.h"
61 #include "BKE_main.h"
62 #include "BKE_plugin_types.h"
63 #include "BKE_sequence.h"
64 #include "BKE_scene.h"
65 #include "BKE_utildefines.h"
66 #include "BKE_report.h"
67
68 #include "BIF_gl.h"
69 #include "BIF_glutil.h"
70
71 #include "WM_api.h"
72 #include "WM_types.h"
73
74 #include "RNA_access.h"
75 #include "RNA_define.h"
76 #include "RNA_enum_types.h"
77
78 /* for menu/popup icons etc etc*/
79 #include "UI_interface.h"
80 #include "UI_resources.h"
81
82 #include "ED_anim_api.h"
83 #include "ED_space_api.h"
84 #include "ED_types.h"
85 #include "ED_screen.h"
86 #include "ED_util.h"
87 #include "ED_fileselect.h"
88
89 #include "UI_interface.h"
90 #include "UI_resources.h"
91 #include "UI_view2d.h"
92
93 #include "BKE_sound.h"
94 #include "AUD_C-API.h"
95
96 /* own include */
97 #include "sequencer_intern.h"
98
99 /* Generic functions, reused by add strip operators */
100
101 /* avoid passing multiple args and be more verbose */
102 #define SEQPROP_STARTFRAME      1<<0
103 #define SEQPROP_ENDFRAME        1<<1
104 #define SEQPROP_FILES           1<<2
105
106 static void sequencer_generic_props__internal(wmOperatorType *ot, int flag)
107 {
108         RNA_def_string(ot->srna, "name", "", MAX_ID_NAME-2, "Name", "Name of the new sequence strip");
109
110         if(flag & SEQPROP_STARTFRAME)
111                 RNA_def_int(ot->srna, "start_frame", 0, INT_MIN, INT_MAX, "Start Frame", "Start frame of the sequence strip", INT_MIN, INT_MAX);
112         
113         if(flag & SEQPROP_ENDFRAME)
114                 RNA_def_int(ot->srna, "end_frame", 0, INT_MIN, INT_MAX, "End Frame", "End frame for the color strip", INT_MIN, INT_MAX); /* not useual since most strips have a fixed length */
115         
116         RNA_def_int(ot->srna, "channel", 1, 1, MAXSEQ, "Channel", "Channel to place this strip into", 1, MAXSEQ);
117         
118         RNA_def_boolean(ot->srna, "replace_sel", 1, "Replace Selection", "replace the current selection");
119
120         if(flag & SEQPROP_FILES)
121                 RNA_def_collection_runtime(ot->srna, "files", &RNA_OperatorFileListElement, "Files", "");
122 }
123
124 static void sequencer_generic_invoke_xy__internal(bContext *C, wmOperator *op, wmEvent *event, int flag)
125 {
126         ARegion *ar= CTX_wm_region(C);
127         View2D *v2d= UI_view2d_fromcontext(C);
128         
129         short mval[2];  
130         float mval_v2d[2];
131         
132
133         mval[0]= event->x - ar->winrct.xmin;
134         mval[1]= event->y - ar->winrct.ymin;
135         
136         UI_view2d_region_to_view(v2d, mval[0], mval[1], &mval_v2d[0], &mval_v2d[1]);
137         
138         RNA_int_set(op->ptr, "channel", (int)mval_v2d[1]+0.5f);
139         RNA_int_set(op->ptr, "start_frame", (int)mval_v2d[0]);
140         
141         if ((flag & SEQPROP_ENDFRAME) && RNA_property_is_set(op->ptr, "end_frame")==0)
142                 RNA_int_set(op->ptr, "end_frame", (int)mval_v2d[0] + 25); // XXX arbitary but ok for now.
143         
144 }
145
146 static void seq_load_operator_info(SeqLoadInfo *seq_load, wmOperator *op)
147 {
148         memset(seq_load, 0, sizeof(SeqLoadInfo));
149
150         seq_load->start_frame=  RNA_int_get(op->ptr, "start_frame");
151         seq_load->channel=              RNA_int_get(op->ptr, "channel");
152         seq_load->len=                  1; // images only!
153
154         RNA_string_get(op->ptr, "name", seq_load->name+2);
155
156         RNA_string_get(op->ptr, "path", seq_load->path); /* full path, file is set by the caller */
157
158         if (RNA_struct_find_property(op->ptr, "replace_sel") && RNA_boolean_get(op->ptr, "replace_sel"))
159                 seq_load->flag |= SEQ_LOAD_REPLACE_SEL;
160
161         if (RNA_struct_find_property(op->ptr, "cache") && RNA_boolean_get(op->ptr, "cache"))
162                 seq_load->flag |= SEQ_LOAD_SOUND_CACHE;
163
164         if (RNA_struct_find_property(op->ptr, "sound") && RNA_boolean_get(op->ptr, "sound"))
165                 seq_load->flag |= SEQ_LOAD_MOVIE_SOUND;
166
167         /* always use this for ops */
168         seq_load->flag |= SEQ_LOAD_FRAME_ADVANCE;
169 }
170
171 /* add scene operator */
172 static int sequencer_add_scene_strip_exec(bContext *C, wmOperator *op)
173 {
174         Scene *scene= CTX_data_scene(C);
175         Editing *ed= seq_give_editing(scene, TRUE);
176         
177         Scene *sce_seq;
178
179         Sequence *seq;  /* generic strip vars */
180         Strip *strip;
181         StripElem *se;
182         
183         int start_frame, channel; /* operator props */
184         
185         start_frame= RNA_int_get(op->ptr, "start_frame");
186         channel= RNA_int_get(op->ptr, "channel");
187         
188         sce_seq= BLI_findlink(&CTX_data_main(C)->scene, RNA_enum_get(op->ptr, "type"));
189         
190         if (sce_seq==NULL) {
191                 BKE_report(op->reports, RPT_ERROR, "Scene not found");
192                 return OPERATOR_CANCELLED;
193         }
194         
195         seq = alloc_sequence(ed->seqbasep, start_frame, channel);
196         
197         seq->type= SEQ_SCENE;
198         seq->scene= sce_seq;
199         
200         /* basic defaults */
201         seq->strip= strip= MEM_callocN(sizeof(Strip), "strip");
202         strip->len = seq->len = sce_seq->r.efra - sce_seq->r.sfra + 1;
203         strip->us= 1;
204         
205         strip->stripdata= se= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
206         
207         if(RNA_property_is_set(op->ptr, "name"))
208                 RNA_string_get(op->ptr, "name", seq->name+2);
209         else
210                 strcpy(seq->name+2, sce_seq->id.name+2);
211         
212         calc_sequence_disp(seq);
213         sort_seq(scene);
214         
215         if (RNA_boolean_get(op->ptr, "replace_sel")) {
216                 deselect_all_seq(scene);
217                 active_seq_set(scene, seq);
218                 seq->flag |= SELECT;
219         }
220         
221         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
222         
223         return OPERATOR_FINISHED;
224 }
225
226
227 static int sequencer_add_scene_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
228 {
229         sequencer_generic_invoke_xy__internal(C, op, event, 0);
230         return sequencer_add_scene_strip_exec(C, op);
231         // needs a menu
232         // return WM_menu_invoke(C, op, event);
233 }
234
235
236 void SEQUENCER_OT_scene_strip_add(struct wmOperatorType *ot)
237 {
238         PropertyRNA *prop;
239         
240         /* identifiers */
241         ot->name= "Add Scene Strip";
242         ot->idname= "SEQUENCER_OT_scene_strip_add";
243         ot->description= "Add a strip to the sequencer using a blender scene as a source";
244
245         /* api callbacks */
246         ot->invoke= sequencer_add_scene_strip_invoke;
247         ot->exec= sequencer_add_scene_strip_exec;
248
249         ot->poll= ED_operator_sequencer_active;
250         
251         /* flags */
252         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
253         
254         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME);
255         prop= RNA_def_enum(ot->srna, "type", DummyRNA_NULL_items, 0, "Type", "");
256         RNA_def_enum_funcs(prop, RNA_scene_itemf);
257 }
258
259 static int sequencer_add_generic_strip_exec(bContext *C, wmOperator *op, SeqLoadFunc seq_load_func)
260 {
261         Scene *scene= CTX_data_scene(C); /* only for sound */
262         Editing *ed= seq_give_editing(scene, TRUE);
263         SeqLoadInfo seq_load;
264         Sequence *seq;
265         int tot_files;
266
267         seq_load_operator_info(&seq_load, op);
268
269         if (seq_load.flag & SEQ_LOAD_REPLACE_SEL)
270                 deselect_all_seq(scene);
271
272         tot_files= RNA_property_collection_length(op->ptr, RNA_struct_find_property(op->ptr, "files"));
273
274         if(tot_files) {
275                 /* multiple files */
276                 char dir_only[FILE_MAX];
277                 char file_only[FILE_MAX];
278
279                 BLI_split_dirfile_basic(seq_load.path, dir_only, NULL);
280
281                 RNA_BEGIN(op->ptr, itemptr, "files") {
282                         RNA_string_get(&itemptr, "name", file_only);
283                         BLI_join_dirfile(seq_load.path, dir_only, file_only);
284
285                         seq= seq_load_func(C, ed->seqbasep, &seq_load);
286                 }
287                 RNA_END;
288         }
289         else {
290                 /* single file */
291                 seq= seq_load_func(C, ed->seqbasep, &seq_load);
292         }
293
294         if (seq_load.tot_success==0) {
295                 BKE_reportf(op->reports, RPT_ERROR, "File \"%s\" could not be loaded", seq_load.path);
296                 return OPERATOR_CANCELLED;
297         }
298
299         sort_seq(scene);
300         seq_update_muting(ed);
301
302         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
303
304         return OPERATOR_FINISHED;
305 }
306
307 /* add movie operator */
308 static int sequencer_add_movie_strip_exec(bContext *C, wmOperator *op)
309 {
310         return sequencer_add_generic_strip_exec(C, op, sequencer_add_movie_strip);
311 }
312
313
314 static int sequencer_add_movie_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
315 {       
316         sequencer_generic_invoke_xy__internal(C, op, event, 0);
317         return WM_operator_filesel(C, op, event);
318         //return sequencer_add_movie_strip_exec(C, op);
319 }
320
321
322 void SEQUENCER_OT_movie_strip_add(struct wmOperatorType *ot)
323 {
324         
325         /* identifiers */
326         ot->name= "Add Movie Strip";
327         ot->idname= "SEQUENCER_OT_movie_strip_add";
328         ot->description= "Add a movie strip to the sequencer";
329
330         /* api callbacks */
331         ot->invoke= sequencer_add_movie_strip_invoke;
332         ot->exec= sequencer_add_movie_strip_exec;
333
334         ot->poll= ED_operator_sequencer_active;
335         
336         /* flags */
337         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
338         
339         WM_operator_properties_filesel(ot, FOLDERFILE|MOVIEFILE, FILE_SPECIAL);
340         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME|SEQPROP_FILES);
341         RNA_def_boolean(ot->srna, "sound", TRUE, "Sound", "Load sound with the movie");
342 }
343
344 /* add sound operator */
345
346 static int sequencer_add_sound_strip_exec(bContext *C, wmOperator *op)
347 {
348         return sequencer_add_generic_strip_exec(C, op, sequencer_add_sound_strip);
349 }
350
351 static int sequencer_add_sound_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
352 {       
353         sequencer_generic_invoke_xy__internal(C, op, event, 0);
354         return WM_operator_filesel(C, op, event);
355         //return sequencer_add_sound_strip_exec(C, op);
356 }
357
358
359 void SEQUENCER_OT_sound_strip_add(struct wmOperatorType *ot)
360 {
361         
362         /* identifiers */
363         ot->name= "Add Sound Strip";
364         ot->idname= "SEQUENCER_OT_sound_strip_add";
365         ot->description= "Add a sound strip to the sequencer";
366
367         /* api callbacks */
368         ot->invoke= sequencer_add_sound_strip_invoke;
369         ot->exec= sequencer_add_sound_strip_exec;
370
371         ot->poll= ED_operator_sequencer_active;
372         
373         /* flags */
374         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
375         
376         WM_operator_properties_filesel(ot, FOLDERFILE|SOUNDFILE, FILE_SPECIAL);
377         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME|SEQPROP_FILES);
378         RNA_def_boolean(ot->srna, "cache", FALSE, "Cache", "Cache the sound in memory.");
379 }
380
381 /* add image operator */
382 static int sequencer_add_image_strip_exec(bContext *C, wmOperator *op)
383 {
384         /* cant use the generic function for this */
385
386         Scene *scene= CTX_data_scene(C); /* only for sound */
387         Editing *ed= seq_give_editing(scene, TRUE);
388         SeqLoadInfo seq_load;
389         Sequence *seq;
390
391         Strip *strip;
392         StripElem *se;
393
394         seq_load_operator_info(&seq_load, op);
395
396         /* images are unique in how they handle this - 1 per strip elem */
397         seq_load.len= RNA_property_collection_length(op->ptr, RNA_struct_find_property(op->ptr, "files"));
398
399         if(seq_load.len==0)
400                 seq_load.len= 1;
401
402         if(seq_load.flag & SEQ_LOAD_REPLACE_SEL)
403                 deselect_all_seq(scene);
404
405         
406         /* main adding function */
407         seq= sequencer_add_image_strip(C, ed->seqbasep, &seq_load);
408         strip= seq->strip;
409         se= strip->stripdata;
410
411         if(seq_load.len > 1) {
412                 RNA_BEGIN(op->ptr, itemptr, "files") {
413                         RNA_string_get(&itemptr, "name", se->name);
414                         se++;
415                 }
416                 RNA_END;
417         }
418         else {
419                 BLI_split_dirfile_basic(seq_load.path, NULL, se->name);
420         }
421         
422         calc_sequence_disp(seq);
423
424         sort_seq(scene);
425
426         /* last active name */
427         strncpy(ed->act_imagedir, strip->dir, FILE_MAXDIR-1);
428         
429         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
430
431         return OPERATOR_FINISHED;
432 }
433
434 static int sequencer_add_image_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
435 {
436         sequencer_generic_invoke_xy__internal(C, op, event, 0);
437         return WM_operator_filesel(C, op, event);       
438         //return sequencer_add_image_strip_exec(C, op);
439 }
440
441
442 void SEQUENCER_OT_image_strip_add(struct wmOperatorType *ot)
443 {
444         
445         /* identifiers */
446         ot->name= "Add Image Strip";
447         ot->idname= "SEQUENCER_OT_image_strip_add";
448         ot->description= "Add an image or image sequence to the sequencer";
449
450         /* api callbacks */
451         ot->invoke= sequencer_add_image_strip_invoke;
452         ot->exec= sequencer_add_image_strip_exec;
453
454         ot->poll= ED_operator_sequencer_active;
455         
456         /* flags */
457         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
458         
459         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE, FILE_SPECIAL);
460         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME|SEQPROP_FILES);
461 }
462
463
464 /* add_effect_strip operator */
465 static int sequencer_add_effect_strip_exec(bContext *C, wmOperator *op)
466 {
467         Scene *scene= CTX_data_scene(C);
468         Editing *ed= seq_give_editing(scene, TRUE);
469
470         Sequence *seq;  /* generic strip vars */
471         Strip *strip;
472         StripElem *se;
473         struct SeqEffectHandle sh;
474
475         int start_frame, end_frame, channel, type; /* operator props */
476         
477         Sequence *seq1, *seq2, *seq3;
478         char *error_msg;
479
480         start_frame= RNA_int_get(op->ptr, "start_frame");
481         end_frame= RNA_int_get(op->ptr, "end_frame");
482         channel= RNA_int_get(op->ptr, "channel");
483
484         type= RNA_enum_get(op->ptr, "type");
485         
486         // XXX We need unique names and move to invoke
487         if(!seq_effect_find_selected(scene, NULL, type, &seq1, &seq2, &seq3, &error_msg)) {
488                 BKE_report(op->reports, RPT_ERROR, error_msg);
489                 return OPERATOR_CANCELLED;
490         }
491
492         /* If seq1 is NULL and no error was rasied it means the seq is standalone
493          * (like color strips) and we need to check its start and end frames are valid */
494         if (seq1==NULL && end_frame <= start_frame) {
495                 BKE_report(op->reports, RPT_ERROR, "Start and end frame are not set");
496                 return OPERATOR_CANCELLED;
497         }
498
499         seq = alloc_sequence(ed->seqbasep, start_frame, channel);
500         seq->type= type;
501
502         if(RNA_property_is_set(op->ptr, "name"))
503                 RNA_string_get(op->ptr, "name", seq->name+2);
504         else
505                 strcpy(seq->name+2, give_seqname(seq));
506
507         seqUniqueName(ed->seqbasep, seq);
508
509         sh = get_sequence_effect(seq);
510
511         seq->seq1= seq1;
512         seq->seq2= seq2;
513         seq->seq3= seq3;
514
515         sh.init(seq);
516
517         if (!seq1) { /* effect has no deps */
518                 seq->len= 1;
519                 seq_tx_set_final_right(seq, end_frame);
520         }
521
522         seq->flag |= SEQ_USE_EFFECT_DEFAULT_FADE;
523
524         calc_sequence(seq);
525         
526         /* basic defaults */
527         seq->strip= strip= MEM_callocN(sizeof(Strip), "strip");
528         strip->len = seq->len;
529         strip->us= 1;
530         if(seq->len>0)
531                 strip->stripdata= se= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
532
533         if (seq->type==SEQ_PLUGIN) {
534                 char path[FILE_MAX];
535                 RNA_string_get(op->ptr, "path", path);
536
537                 sh.init_plugin(seq, path);
538
539                 if(seq->plugin==NULL) {
540                         BLI_remlink(ed->seqbasep, seq);
541                         seq_free_sequence(scene, seq);
542                         BKE_reportf(op->reports, RPT_ERROR, "Sequencer plugin \"%s\" could not load.", path);
543                         return OPERATOR_CANCELLED;
544                 }
545         }
546         else if (seq->type==SEQ_COLOR) {
547                 SolidColorVars *colvars= (SolidColorVars *)seq->effectdata;
548                 RNA_float_get_array(op->ptr, "color", colvars->col);
549         }
550
551         if(seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq);
552
553         update_changed_seq_and_deps(scene, seq, 1, 1); /* runs calc_sequence */
554
555
556         /* not sure if this is needed with update_changed_seq_and_deps.
557          * it was NOT called in blender 2.4x, but wont hurt */
558         sort_seq(scene); 
559
560         if (RNA_boolean_get(op->ptr, "replace_sel")) {
561                 deselect_all_seq(scene);
562                 active_seq_set(scene, seq);
563                 seq->flag |= SELECT;
564         }
565
566         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
567
568         return OPERATOR_FINISHED;
569 }
570
571
572 /* add color */
573 static int sequencer_add_effect_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
574 {
575         sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_ENDFRAME);
576
577         if (RNA_property_is_set(op->ptr, "type") && RNA_enum_get(op->ptr, "type")==SEQ_PLUGIN) {
578                 /* only plugins need the file selector */
579                 return WM_operator_filesel(C, op, event);
580         }
581         else {
582                 return sequencer_add_effect_strip_exec(C, op);
583         }
584 }
585
586 void SEQUENCER_OT_effect_strip_add(struct wmOperatorType *ot)
587 {
588         /* identifiers */
589         ot->name= "Add Effect Strip";
590         ot->idname= "SEQUENCER_OT_effect_strip_add";
591         ot->description= "Add an effect to the sequencer, most are applied on top of existing strips";
592
593         /* api callbacks */
594         ot->invoke= sequencer_add_effect_strip_invoke;
595         ot->exec= sequencer_add_effect_strip_exec;
596
597         ot->poll= ED_operator_sequencer_active;
598         
599         /* flags */
600         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
601         
602         WM_operator_properties_filesel(ot, 0, FILE_SPECIAL);
603         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME|SEQPROP_ENDFRAME);
604         RNA_def_enum(ot->srna, "type", sequencer_prop_effect_types, SEQ_CROSS, "Type", "Sequencer effect type");
605         RNA_def_float_vector(ot->srna, "color", 3, NULL, 0.0f, 1.0f, "Color", "Initialize the strip with this color (only used when type='COLOR')", 0.0f, 1.0f);
606 }