Fix [#30136] Adding Sequence Plugin fails.
[blender-staging.git] / source / blender / editors / space_sequencer / sequencer_add.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2003-2009, Campbell Barton
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/space_sequencer/sequencer_add.c
27  *  \ingroup spseq
28  */
29
30
31 #include <stdlib.h>
32 #include <math.h>
33 #include <string.h>
34
35 #ifndef WIN32
36 #include <unistd.h>
37 #else
38 #include <io.h>
39 #endif
40 #include <sys/types.h>
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_math.h"
46 #include "BLI_utildefines.h"
47
48 #include "DNA_scene_types.h"
49 #include "DNA_userdef_types.h"
50
51 #include "BKE_context.h"
52 #include "BKE_global.h"
53 #include "BKE_main.h"
54 #include "BKE_sequencer.h"
55 #include "BKE_report.h"
56
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "RNA_define.h"
62 #include "RNA_enum_types.h"
63
64 /* for menu/popup icons etc etc*/
65
66 #include "ED_screen.h"
67
68 #include "UI_view2d.h"
69
70 #include "BKE_sound.h"
71
72 #ifdef WITH_AUDASPACE
73 #  include "AUD_C-API.h"
74 #endif
75
76 /* own include */
77 #include "sequencer_intern.h"
78
79 /* Generic functions, reused by add strip operators */
80
81 /* avoid passing multiple args and be more verbose */
82 #define SEQPROP_STARTFRAME      (1<<0)
83 #define SEQPROP_ENDFRAME        (1<<1)
84 #define SEQPROP_NOPATHS         (1<<2)
85 #define SEQPROP_NOCHAN          (1<<3)
86
87 #define SELECT 1
88
89 static void sequencer_generic_props__internal(wmOperatorType *ot, int flag)
90 {
91         if(flag & SEQPROP_STARTFRAME)
92                 RNA_def_int(ot->srna, "frame_start", 0, INT_MIN, INT_MAX, "Start Frame", "Start frame of the sequence strip", INT_MIN, INT_MAX);
93         
94         if(flag & SEQPROP_ENDFRAME)
95                 RNA_def_int(ot->srna, "frame_end", 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 */
96         
97         RNA_def_int(ot->srna, "channel", 1, 1, MAXSEQ, "Channel", "Channel to place this strip into", 1, MAXSEQ);
98         
99         RNA_def_boolean(ot->srna, "replace_sel", 1, "Replace Selection", "replace the current selection");
100
101         RNA_def_boolean(ot->srna, "overlap", 0, "Allow Overlap", "Don't correct overlap on new sequence strips");
102 }
103
104 static void sequencer_generic_invoke_path__internal(bContext *C, wmOperator *op, const char *identifier)
105 {
106         if(RNA_struct_find_property(op->ptr, identifier)) {
107                 Scene *scene= CTX_data_scene(C);
108                 Sequence *last_seq= seq_active_get(scene);
109                 if(last_seq && last_seq->strip && SEQ_HAS_PATH(last_seq)) {
110                         char path[sizeof(last_seq->strip->dir)];
111                         BLI_strncpy(path, last_seq->strip->dir, sizeof(path));
112                         BLI_path_abs(path, G.main->name);
113                         RNA_string_set(op->ptr, identifier, path);
114                 }
115         }
116 }
117
118 static void sequencer_generic_invoke_xy__internal(bContext *C, wmOperator *op, wmEvent *event, int flag)
119 {
120         View2D *v2d= UI_view2d_fromcontext(C);
121         
122         float mval_v2d[2];
123         
124         UI_view2d_region_to_view(v2d, event->mval[0], event->mval[1], &mval_v2d[0], &mval_v2d[1]);
125
126         /* effect strips dont need a channel initialized from the mouse */
127         if(!(flag & SEQPROP_NOCHAN)) {
128                 RNA_int_set(op->ptr, "channel", (int)mval_v2d[1]+0.5f);
129         }
130
131         RNA_int_set(op->ptr, "frame_start", (int)mval_v2d[0]);
132         
133         if ((flag & SEQPROP_ENDFRAME) && RNA_struct_property_is_set(op->ptr, "frame_end")==0)
134                 RNA_int_set(op->ptr, "frame_end", (int)mval_v2d[0] + 25); // XXX arbitary but ok for now.
135
136         if (!(flag & SEQPROP_NOPATHS)) {
137                 sequencer_generic_invoke_path__internal(C, op, "filepath");
138                 sequencer_generic_invoke_path__internal(C, op, "directory");
139         }
140 }
141
142 static void seq_load_operator_info(SeqLoadInfo *seq_load, wmOperator *op)
143 {
144         int relative= RNA_struct_find_property(op->ptr, "relative_path") && RNA_boolean_get(op->ptr, "relative_path");
145         int is_file= -1;
146         memset(seq_load, 0, sizeof(SeqLoadInfo));
147
148         seq_load->start_frame=  RNA_int_get(op->ptr, "frame_start");
149         seq_load->end_frame=    seq_load->start_frame; /* un-set */
150
151         seq_load->channel=              RNA_int_get(op->ptr, "channel");
152         seq_load->len=                  1; // images only, if endframe isnt set!
153
154         if(RNA_struct_find_property(op->ptr, "filepath")) {
155                 RNA_string_get(op->ptr, "filepath", seq_load->path); /* full path, file is set by the caller */
156                 is_file= 1;
157         } else if (RNA_struct_find_property(op->ptr, "directory")) {
158                 RNA_string_get(op->ptr, "directory", seq_load->path); /* full path, file is set by the caller */
159                 is_file= 0;
160         }
161
162         if((is_file != -1) && relative)
163                 BLI_path_rel(seq_load->path, G.main->name);
164
165         
166         if (RNA_struct_find_property(op->ptr, "frame_end")) {
167                 seq_load->end_frame = RNA_int_get(op->ptr, "frame_end");
168         }
169
170         if (RNA_struct_find_property(op->ptr, "replace_sel") && RNA_boolean_get(op->ptr, "replace_sel"))
171                 seq_load->flag |= SEQ_LOAD_REPLACE_SEL;
172
173         if (RNA_struct_find_property(op->ptr, "cache") && RNA_boolean_get(op->ptr, "cache"))
174                 seq_load->flag |= SEQ_LOAD_SOUND_CACHE;
175
176         if (RNA_struct_find_property(op->ptr, "sound") && RNA_boolean_get(op->ptr, "sound"))
177                 seq_load->flag |= SEQ_LOAD_MOVIE_SOUND;
178
179         /* always use this for ops */
180         seq_load->flag |= SEQ_LOAD_FRAME_ADVANCE;
181
182
183         if(is_file==1) {
184                 BLI_strncpy(seq_load->name, BLI_path_basename(seq_load->path), sizeof(seq_load->name));
185         }
186         else if(RNA_struct_find_property(op->ptr, "files")) {
187                 /* used for image strip */
188                 /* best guess, first images name */
189                 RNA_BEGIN(op->ptr, itemptr, "files") {
190                         char *name= RNA_string_get_alloc(&itemptr, "name", NULL, 0);
191                         BLI_strncpy(seq_load->name, name, sizeof(seq_load->name));
192                         MEM_freeN(name);
193                         break;
194                 }
195                 RNA_END;
196         }
197 }
198
199 /* add scene operator */
200 static int sequencer_add_scene_strip_exec(bContext *C, wmOperator *op)
201 {
202         Scene *scene= CTX_data_scene(C);
203         Editing *ed= seq_give_editing(scene, TRUE);
204         
205         Scene *sce_seq;
206
207         Sequence *seq;  /* generic strip vars */
208         Strip *strip;
209         
210         int start_frame, channel; /* operator props */
211         
212         start_frame= RNA_int_get(op->ptr, "frame_start");
213         channel= RNA_int_get(op->ptr, "channel");
214         
215         sce_seq= BLI_findlink(&CTX_data_main(C)->scene, RNA_enum_get(op->ptr, "scene"));
216         
217         if (sce_seq==NULL) {
218                 BKE_report(op->reports, RPT_ERROR, "Scene not found");
219                 return OPERATOR_CANCELLED;
220         }
221         
222         seq = alloc_sequence(ed->seqbasep, start_frame, channel);
223         seq->type= SEQ_SCENE;
224         seq->blend_mode= SEQ_CROSS; /* so alpha adjustment fade to the strip below */
225
226         seq->scene= sce_seq;
227         seq->sfra= sce_seq->r.sfra;
228         
229         /* basic defaults */
230         seq->strip= strip= MEM_callocN(sizeof(Strip), "strip");
231         strip->len = seq->len = sce_seq->r.efra - sce_seq->r.sfra + 1;
232         strip->us= 1;
233         
234         strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
235         
236         BLI_strncpy(seq->name+2, sce_seq->id.name+2, sizeof(seq->name)-2);
237         seqbase_unique_name_recursive(&ed->seqbase, seq);
238
239         seq->scene_sound = sound_scene_add_scene_sound(scene, seq, start_frame, start_frame + strip->len, 0);
240
241         calc_sequence_disp(scene, seq);
242         sort_seq(scene);
243         
244         if (RNA_boolean_get(op->ptr, "replace_sel")) {
245                 deselect_all_seq(scene);
246                 seq_active_set(scene, seq);
247                 seq->flag |= SELECT;
248         }
249
250         if(RNA_boolean_get(op->ptr, "overlap") == FALSE) {
251                 if(seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
252         }
253
254         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
255         
256         return OPERATOR_FINISHED;
257 }
258
259
260 static int sequencer_add_scene_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
261 {
262         if(!ED_operator_sequencer_active(C)) {
263                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
264                 return OPERATOR_CANCELLED;
265         }
266
267         if(!RNA_struct_property_is_set(op->ptr, "scene"))
268                 return WM_enum_search_invoke(C, op, event);
269
270         sequencer_generic_invoke_xy__internal(C, op, event, 0);
271         return sequencer_add_scene_strip_exec(C, op);
272         // needs a menu
273         // return WM_menu_invoke(C, op, event);
274 }
275
276
277 void SEQUENCER_OT_scene_strip_add(struct wmOperatorType *ot)
278 {
279         PropertyRNA *prop;
280         
281         /* identifiers */
282         ot->name= "Add Scene Strip";
283         ot->idname= "SEQUENCER_OT_scene_strip_add";
284         ot->description= "Add a strip to the sequencer using a blender scene as a source";
285
286         /* api callbacks */
287         ot->invoke= sequencer_add_scene_strip_invoke;
288         ot->exec= sequencer_add_scene_strip_exec;
289
290         ot->poll= ED_operator_scene_editable;
291         
292         /* flags */
293         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
294         
295         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME);
296         prop= RNA_def_enum(ot->srna, "scene", DummyRNA_NULL_items, 0, "Scene", "");
297         RNA_def_enum_funcs(prop, RNA_scene_itemf);
298         ot->prop= prop;
299 }
300
301 static int sequencer_add_generic_strip_exec(bContext *C, wmOperator *op, SeqLoadFunc seq_load_func)
302 {
303         Scene *scene= CTX_data_scene(C); /* only for sound */
304         Editing *ed= seq_give_editing(scene, TRUE);
305         SeqLoadInfo seq_load;
306         Sequence *seq;
307         int tot_files;
308         const short overlap= RNA_boolean_get(op->ptr, "overlap");
309
310         seq_load_operator_info(&seq_load, op);
311
312         if (seq_load.flag & SEQ_LOAD_REPLACE_SEL)
313                 deselect_all_seq(scene);
314
315         tot_files= RNA_property_collection_length(op->ptr, RNA_struct_find_property(op->ptr, "files"));
316
317         if(tot_files) {
318                 /* multiple files */
319                 char dir_only[FILE_MAX];
320                 char file_only[FILE_MAX];
321
322                 BLI_split_dir_part(seq_load.path, dir_only, sizeof(dir_only));
323
324                 RNA_BEGIN(op->ptr, itemptr, "files") {
325                         RNA_string_get(&itemptr, "name", file_only);
326                         BLI_join_dirfile(seq_load.path, sizeof(seq_load.path), dir_only, file_only);
327
328                         seq = seq_load_func(C, ed->seqbasep, &seq_load);
329                         if (seq) {
330                                 if(overlap == FALSE) {
331                                         if(seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
332                                 }
333                         }
334                 }
335                 RNA_END;
336         }
337         else {
338                 /* single file */
339                 seq = seq_load_func(C, ed->seqbasep, &seq_load);
340                 if (seq) {
341                         if(overlap == FALSE) {
342                                 if(seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
343                         }
344                 }
345         }
346
347         if (seq_load.tot_success==0) {
348                 BKE_reportf(op->reports, RPT_ERROR, "File \"%s\" could not be loaded", seq_load.path);
349                 return OPERATOR_CANCELLED;
350         }
351
352         sort_seq(scene);
353         seq_update_muting(ed);
354
355         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
356
357         return OPERATOR_FINISHED;
358 }
359
360 /* add movie operator */
361 static int sequencer_add_movie_strip_exec(bContext *C, wmOperator *op)
362 {
363         return sequencer_add_generic_strip_exec(C, op, sequencer_add_movie_strip);
364 }
365
366
367 static int sequencer_add_movie_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
368 {
369
370         if(!ED_operator_sequencer_active(C)) {
371                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
372                 return OPERATOR_CANCELLED;
373         }
374
375         /* This is for drag and drop */
376         if(RNA_collection_length(op->ptr, "files") || RNA_struct_property_is_set(op->ptr, "filepath")) {
377                 sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_NOPATHS);
378                 return sequencer_add_movie_strip_exec(C, op);
379         }
380         
381         sequencer_generic_invoke_xy__internal(C, op, event, 0);
382         
383         WM_event_add_fileselect(C, op);
384         return OPERATOR_RUNNING_MODAL;
385
386         //return sequencer_add_movie_strip_exec(C, op);
387 }
388
389
390 void SEQUENCER_OT_movie_strip_add(struct wmOperatorType *ot)
391 {
392         
393         /* identifiers */
394         ot->name= "Add Movie Strip";
395         ot->idname= "SEQUENCER_OT_movie_strip_add";
396         ot->description= "Add a movie strip to the sequencer";
397
398         /* api callbacks */
399         ot->invoke= sequencer_add_movie_strip_invoke;
400         ot->exec= sequencer_add_movie_strip_exec;
401
402         ot->poll= ED_operator_scene_editable;
403         
404         /* flags */
405         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
406         
407         WM_operator_properties_filesel(ot, FOLDERFILE|MOVIEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH|WM_FILESEL_FILES, FILE_DEFAULTDISPLAY);
408         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME);
409         RNA_def_boolean(ot->srna, "sound", TRUE, "Sound", "Load sound with the movie");
410 }
411
412 /* add sound operator */
413
414 static int sequencer_add_sound_strip_exec(bContext *C, wmOperator *op)
415 {
416         return sequencer_add_generic_strip_exec(C, op, sequencer_add_sound_strip);
417 }
418
419 static int sequencer_add_sound_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
420 {
421
422         if(!ED_operator_sequencer_active(C)) {
423                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
424                 return OPERATOR_CANCELLED;
425         }
426         
427         /* This is for drag and drop */
428         if(RNA_collection_length(op->ptr, "files") || RNA_struct_property_is_set(op->ptr, "filepath")) {
429                 sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_NOPATHS);
430                 return sequencer_add_sound_strip_exec(C, op);
431         }
432         
433         sequencer_generic_invoke_xy__internal(C, op, event, 0);
434
435         WM_event_add_fileselect(C, op);
436         return OPERATOR_RUNNING_MODAL;
437
438         //return sequencer_add_sound_strip_exec(C, op);
439 }
440
441
442 void SEQUENCER_OT_sound_strip_add(struct wmOperatorType *ot)
443 {
444         
445         /* identifiers */
446         ot->name= "Add Sound Strip";
447         ot->idname= "SEQUENCER_OT_sound_strip_add";
448         ot->description= "Add a sound strip to the sequencer";
449
450         /* api callbacks */
451         ot->invoke= sequencer_add_sound_strip_invoke;
452         ot->exec= sequencer_add_sound_strip_exec;
453
454         ot->poll= ED_operator_scene_editable;
455         
456         /* flags */
457         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
458         
459         WM_operator_properties_filesel(ot, FOLDERFILE|SOUNDFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH|WM_FILESEL_FILES, FILE_DEFAULTDISPLAY);
460         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME);
461         RNA_def_boolean(ot->srna, "cache", FALSE, "Cache", "Cache the sound in memory");
462 }
463
464 /* add image operator */
465 static int sequencer_add_image_strip_exec(bContext *C, wmOperator *op)
466 {
467         /* cant use the generic function for this */
468
469         Scene *scene= CTX_data_scene(C); /* only for sound */
470         Editing *ed= seq_give_editing(scene, TRUE);
471         SeqLoadInfo seq_load;
472         Sequence *seq;
473
474         Strip *strip;
475         StripElem *se;
476
477         seq_load_operator_info(&seq_load, op);
478
479         /* images are unique in how they handle this - 1 per strip elem */
480         seq_load.len= RNA_property_collection_length(op->ptr, RNA_struct_find_property(op->ptr, "files"));
481
482         if(seq_load.len==0)
483                 return OPERATOR_CANCELLED;
484
485         if(seq_load.flag & SEQ_LOAD_REPLACE_SEL)
486                 deselect_all_seq(scene);
487
488
489         /* main adding function */
490         seq= sequencer_add_image_strip(C, ed->seqbasep, &seq_load);
491         strip= seq->strip;
492         se= strip->stripdata;
493
494         RNA_BEGIN(op->ptr, itemptr, "files") {
495                 char *filename= RNA_string_get_alloc(&itemptr, "name", NULL, 0);
496                 BLI_strncpy(se->name, filename, sizeof(se->name));
497                 MEM_freeN(filename);
498                 se++;
499         }
500         RNA_END;
501
502         if(seq_load.len == 1) {
503                 if(seq_load.start_frame < seq_load.end_frame) {
504                         seq->endstill= seq_load.end_frame - seq_load.start_frame;
505                 }
506         }
507         
508         calc_sequence_disp(scene, seq);
509
510         sort_seq(scene);
511
512         /* last active name */
513         strncpy(ed->act_imagedir, strip->dir, FILE_MAXDIR-1);
514
515         if(RNA_boolean_get(op->ptr, "overlap") == FALSE) {
516                 if(seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
517         }
518
519         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
520
521         return OPERATOR_FINISHED;
522 }
523
524 static int sequencer_add_image_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
525 {
526
527         if(!ED_operator_sequencer_active(C)) {
528                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
529                 return OPERATOR_CANCELLED;
530         }
531
532
533         /* drag drop has set the names */
534         if(RNA_collection_length(op->ptr, "files")) {
535                 sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_ENDFRAME|SEQPROP_NOPATHS);
536                 return sequencer_add_image_strip_exec(C, op);
537         }
538         
539         sequencer_generic_invoke_xy__internal(C, op, event, SEQPROP_ENDFRAME);
540
541         WM_event_add_fileselect(C, op);
542         return OPERATOR_RUNNING_MODAL;
543 }
544
545
546 void SEQUENCER_OT_image_strip_add(struct wmOperatorType *ot)
547 {
548         
549         /* identifiers */
550         ot->name= "Add Image Strip";
551         ot->idname= "SEQUENCER_OT_image_strip_add";
552         ot->description= "Add an image or image sequence to the sequencer";
553
554         /* api callbacks */
555         ot->invoke= sequencer_add_image_strip_invoke;
556         ot->exec= sequencer_add_image_strip_exec;
557
558         ot->poll= ED_operator_scene_editable;
559         
560         /* flags */
561         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
562         
563         WM_operator_properties_filesel(ot, FOLDERFILE|IMAGEFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_DIRECTORY|WM_FILESEL_RELPATH|WM_FILESEL_FILES, FILE_DEFAULTDISPLAY);
564         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME|SEQPROP_ENDFRAME);
565 }
566
567
568 /* add_effect_strip operator */
569 static int sequencer_add_effect_strip_exec(bContext *C, wmOperator *op)
570 {
571         Scene *scene= CTX_data_scene(C);
572         Editing *ed= seq_give_editing(scene, TRUE);
573
574         Sequence *seq;  /* generic strip vars */
575         Strip *strip;
576         struct SeqEffectHandle sh;
577
578         int start_frame, end_frame, channel, type; /* operator props */
579         
580         Sequence *seq1, *seq2, *seq3;
581         const char *error_msg;
582
583         start_frame= RNA_int_get(op->ptr, "frame_start");
584         end_frame= RNA_int_get(op->ptr, "frame_end");
585         channel= RNA_int_get(op->ptr, "channel");
586
587         type= RNA_enum_get(op->ptr, "type");
588         
589         // XXX move to invoke
590         if(!seq_effect_find_selected(scene, NULL, type, &seq1, &seq2, &seq3, &error_msg)) {
591                 BKE_report(op->reports, RPT_ERROR, error_msg);
592                 return OPERATOR_CANCELLED;
593         }
594
595         /* If seq1 is NULL and no error was rasied it means the seq is standalone
596          * (like color strips) and we need to check its start and end frames are valid */
597         if (seq1==NULL && end_frame <= start_frame) {
598                 BKE_report(op->reports, RPT_ERROR, "Start and end frame are not set");
599                 return OPERATOR_CANCELLED;
600         }
601
602         seq = alloc_sequence(ed->seqbasep, start_frame, channel);
603         seq->type= type;
604
605         BLI_strncpy(seq->name+2, give_seqname(seq), sizeof(seq->name)-2);
606         seqbase_unique_name_recursive(&ed->seqbase, seq);
607
608         sh = get_sequence_effect(seq);
609
610         seq->seq1= seq1;
611         seq->seq2= seq2;
612         seq->seq3= seq3;
613
614         sh.init(seq);
615
616         if (!seq1) { /* effect has no deps */
617                 seq->len= 1;
618                 seq_tx_set_final_right(seq, end_frame);
619         }
620
621         seq->flag |= SEQ_USE_EFFECT_DEFAULT_FADE;
622
623         calc_sequence(scene, seq);
624         
625         /* basic defaults */
626         seq->strip= strip= MEM_callocN(sizeof(Strip), "strip");
627         strip->len = seq->len;
628         strip->us= 1;
629         if(seq->len>0)
630                 strip->stripdata= MEM_callocN(seq->len*sizeof(StripElem), "stripelem");
631
632         if (seq->type==SEQ_PLUGIN) {
633                 char path[FILE_MAX];
634                 RNA_string_get(op->ptr, "filepath", path);
635
636                 sh.init_plugin(seq, path);
637
638                 if(seq->plugin==NULL) {
639                         BLI_remlink(ed->seqbasep, seq);
640                         seq_free_sequence(scene, seq);
641                         BKE_reportf(op->reports, RPT_ERROR, "Sequencer plugin \"%s\" could not load", path);
642                         return OPERATOR_CANCELLED;
643                 }
644         } else if (seq->type == SEQ_COLOR) {
645                 SolidColorVars *colvars= (SolidColorVars *)seq->effectdata;
646                 RNA_float_get_array(op->ptr, "color", colvars->col);
647                 seq->blend_mode= SEQ_CROSS; /* so alpha adjustment fade to the strip below */
648
649         } else if (seq->type == SEQ_ADJUSTMENT) {
650                 seq->blend_mode= SEQ_CROSS;
651         }
652
653         /* an unset channel is a special case where we automatically go above
654          * the other strips. */
655         if(!RNA_struct_property_is_set(op->ptr, "channel")) {
656                 if(seq->seq1) {
657                         int chan= MAX3( seq->seq1 ? seq->seq1->machine : 0,
658                                                         seq->seq2 ? seq->seq2->machine : 0,
659                                                         seq->seq3 ? seq->seq3->machine : 0);
660                         if(chan < MAXSEQ)
661                                 seq->machine= chan;
662                 }
663         }
664
665         if(RNA_boolean_get(op->ptr, "overlap") == FALSE) {
666                 if(seq_test_overlap(ed->seqbasep, seq)) shuffle_seq(ed->seqbasep, seq, scene);
667         }
668
669         update_changed_seq_and_deps(scene, seq, 1, 1); /* runs calc_sequence */
670
671
672         /* not sure if this is needed with update_changed_seq_and_deps.
673          * it was NOT called in blender 2.4x, but wont hurt */
674         sort_seq(scene); 
675
676         if (RNA_boolean_get(op->ptr, "replace_sel")) {
677                 deselect_all_seq(scene);
678                 seq_active_set(scene, seq);
679                 seq->flag |= SELECT;
680         }
681
682         WM_event_add_notifier(C, NC_SCENE|ND_SEQUENCER, scene);
683
684         return OPERATOR_FINISHED;
685 }
686
687
688 /* add color */
689 static int sequencer_add_effect_strip_invoke(bContext *C, wmOperator *op, wmEvent *event)
690 {
691         short is_type_set= RNA_struct_property_is_set(op->ptr, "type");
692         int type= -1;
693         int prop_flag= SEQPROP_ENDFRAME;
694
695         if(!ED_operator_sequencer_active(C)) {
696                 BKE_report(op->reports, RPT_ERROR, "Sequencer area not active");
697                 return OPERATOR_CANCELLED;
698         }
699
700         if(is_type_set) {
701                 type= RNA_enum_get(op->ptr, "type");
702
703                 /* when invoking an effect strip which uses inputs,
704                  * skip initialzing the channel from the mouse.
705                  * Instead leave the property unset so exec() initializes it to be
706                  * above the strips its applied to. */
707                 if(get_sequence_effect_num_inputs(type) != 0) {
708                         prop_flag |= SEQPROP_NOCHAN;
709                 }
710         }
711
712         sequencer_generic_invoke_xy__internal(C, op, event, prop_flag);
713
714         if (is_type_set && type==SEQ_PLUGIN) {
715                 /* only plugins need the file selector */
716                 WM_event_add_fileselect(C, op);
717                 return OPERATOR_RUNNING_MODAL;
718         }
719         else {
720                 return sequencer_add_effect_strip_exec(C, op);
721         }
722 }
723
724 void SEQUENCER_OT_effect_strip_add(struct wmOperatorType *ot)
725 {
726         /* identifiers */
727         ot->name= "Add Effect Strip";
728         ot->idname= "SEQUENCER_OT_effect_strip_add";
729         ot->description= "Add an effect to the sequencer, most are applied on top of existing strips";
730
731         /* api callbacks */
732         ot->invoke= sequencer_add_effect_strip_invoke;
733         ot->exec= sequencer_add_effect_strip_exec;
734
735         ot->poll= ED_operator_scene_editable;
736         
737         /* flags */
738         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
739         
740         WM_operator_properties_filesel(ot, 0, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY);
741         sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME|SEQPROP_ENDFRAME);
742         RNA_def_enum(ot->srna, "type", sequencer_prop_effect_types, SEQ_CROSS, "Type", "Sequencer effect type");
743         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);
744 }