Updated Quicktime code so settings can be stored in the blendfile.
[blender.git] / source / blender / blenkernel / intern / scene.c
1 /*  scene.c
2  *  
3  * 
4  * $Id$
5  *
6  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version. The Blender
12  * Foundation also sells licenses for use in proprietary software under
13  * the Blender License.  See http://www.blender.org/BL/ for information
14  * about this.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26  * All rights reserved.
27  *
28  * The Original Code is: all of this file.
29  *
30  * Contributor(s): none yet.
31  *
32  * ***** END GPL/BL DUAL LICENSE BLOCK *****
33  */
34
35 #include <stdio.h>
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #ifndef WIN32 
42 #include <unistd.h>
43 #else
44 #include "BLI_winstuff.h"
45 #include <io.h>
46 #endif
47 #include "MEM_guardedalloc.h"
48
49 #include "nla.h"                                /* for __NLA : IMPORTANT Do not delete me yet! */
50 #ifdef __NLA                                    /* for __NLA : IMPORTANT Do not delete me yet! */
51 #include "DNA_armature_types.h" /* for __NLA : IMPORTANT Do not delete me yet! */
52 #include "BKE_armature.h"               /* for __NLA : IMPORTANT Do not delete me yet! */
53 #include "BKE_action.h"                 /* for __NLA : IMPORTANT Do not delete me yet! */
54 #endif                                                  /* for __NLA : IMPORTANT Do not delete me yet! */
55
56 #include "DNA_constraint_types.h"
57 #include "DNA_scene_types.h"
58 #include "DNA_object_types.h"
59 #include "DNA_scriptlink_types.h"
60 #include "DNA_meta_types.h"
61 #include "DNA_ika_types.h"
62 #include "DNA_lamp_types.h"
63 #include "DNA_material_types.h"
64 #include "DNA_mesh_types.h"
65 #include "DNA_group_types.h"
66 #include "DNA_curve_types.h"
67 #include "DNA_userdef_types.h"
68
69 #include "BLI_blenlib.h"
70
71 #include "BKE_bad_level_calls.h"
72 #include "BKE_utildefines.h"
73
74 #include "BKE_global.h"
75 #include "BKE_main.h"
76 #include "BKE_anim.h"
77
78 #include "BKE_library.h"
79
80 #include "BPY_extern.h"
81 #include "BKE_scene.h"
82 #include "BKE_world.h"
83 #include "BKE_ipo.h"
84 #include "BKE_ika.h"
85 #include "BKE_key.h"
86
87 void free_avicodecdata(AviCodecData *acd)
88 {
89         if (acd) {
90                 if (acd->lpFormat){
91                         MEM_freeN(acd->lpFormat);
92                         acd->lpFormat = NULL;
93                         acd->cbFormat = 0;
94                 }
95                 if (acd->lpParms){
96                         MEM_freeN(acd->lpParms);
97                         acd->lpParms = NULL;
98                         acd->cbParms = 0;
99                 }
100         }
101 }
102
103 void free_qtcodecdata(QuicktimeCodecData *qcd)
104 {
105         if (qcd) {
106                 if (qcd->cdParms){
107                         MEM_freeN(qcd->cdParms);
108                         qcd->cdParms = NULL;
109                         qcd->cdSize = 0;
110                 }
111         }
112 }
113
114 /* do not free scene itself */
115 void free_scene(Scene *sce)
116 {
117         Base *base;
118
119         base= sce->base.first;
120         while(base) {
121                 base->object->id.us--;
122                 base= base->next;
123         }
124         /* do not free objects! */
125
126         BLI_freelistN(&sce->base);
127         free_editing(sce->ed);
128         if(sce->radio) MEM_freeN(sce->radio);
129         if(sce->fcam) MEM_freeN(sce->fcam);
130         sce->radio= 0;
131         
132         BPY_free_scriptlink(&sce->scriptlink);
133         if (sce->r.avicodecdata) {
134                 free_avicodecdata(sce->r.avicodecdata);
135                 MEM_freeN(sce->r.avicodecdata);
136                 sce->r.avicodecdata = NULL;
137         }
138         if (sce->r.qtcodecdata) {
139                 free_qtcodecdata(sce->r.qtcodecdata);
140                 MEM_freeN(sce->r.qtcodecdata);
141                 sce->r.qtcodecdata = NULL;
142         }
143 }
144
145 Scene *add_scene(char *name)
146 {
147         Scene *sce;
148         
149         sce= alloc_libblock(&G.main->scene, ID_SCE, name);
150         sce->lay= 1;
151
152         sce->r.mode= R_GAMMA;
153         sce->r.cfra= 1;
154         sce->r.sfra= 1;
155         sce->r.efra= 250;
156         sce->r.xsch= 320;
157         sce->r.ysch= 256;
158         sce->r.xasp= 1;
159         sce->r.yasp= 1;
160         sce->r.xparts= 1;
161         sce->r.yparts= 1;
162         sce->r.size= 100;
163         sce->r.planes= 24;
164         sce->r.quality= 90;
165         sce->r.framapto= 100;
166         sce->r.images= 100;
167         sce->r.framelen= 1.0;
168         sce->r.frs_sec= 25;
169
170         sce->r.xplay= 640;
171         sce->r.yplay= 480;
172         sce->r.freqplay= 60;
173         sce->r.depth= 32;
174
175         if (sce->r.avicodecdata) {
176 printf("this is not good\n");
177         }
178 //      sce->r.imtype= R_TARGA;
179
180         sce->r.stereomode = 1;  // no stereo
181
182         strcpy(sce->r.backbuf, "//backbuf");
183         strcpy(sce->r.pic, U.renderdir);
184         strcpy(sce->r.ftype, "//ftype");
185         
186         BLI_init_rctf(&sce->r.safety, 0.1f, 0.9f, 0.1f, 0.9f);
187         sce->r.osa= 8;
188         
189         return sce;
190 }
191
192 int object_in_scene(Object *ob, Scene *sce)
193 {
194         Base *base;
195         
196         base= sce->base.first;
197         while(base) {
198                 if(base->object == ob) return 1;
199                 base= base->next;
200         }
201         return 0;
202 }
203
204 void sort_baselist(Scene *sce)
205 {
206         /* in order of parent and track */
207         ListBase tempbase, noparentbase, notyetbase;
208         Base *base, *test=NULL;
209         Object *par;
210         int doit, domore= 0, lastdomore=1;
211         
212         
213         /* keep same order when nothing has changed! */
214         
215         while(domore!=lastdomore) {
216
217                 lastdomore= domore;
218                 domore= 0;
219                 tempbase.first= tempbase.last= 0;
220                 noparentbase.first= noparentbase.last= 0;
221                 notyetbase.first= notyetbase.last= 0;
222                 
223                 while( (base= sce->base.first) ) {
224                         BLI_remlink(&sce->base, base);
225                         
226                         par= 0;
227                         if(base->object->type==OB_IKA) {
228                                 Ika *ika= base->object->data;
229                                 par= ika->parent;
230                         }
231
232                         if(par || base->object->parent || base->object->track) {
233                                 
234                                 doit= 0;
235                                 if(base->object->parent) doit++;
236                                 if(base->object->track) doit++;
237                                 
238                         /* Count constraints */
239                                 {
240                                         bConstraint *con;
241                                         for (con = base->object->constraints.first; con; con=con->next){
242                                                 switch (con->type){
243                                                 case CONSTRAINT_TYPE_KINEMATIC:
244                                                         {
245                                                                 bKinematicConstraint *data=con->data;
246                                                                 if (data->tar) doit++;
247                                                         }
248                                                         break;
249                                                 case CONSTRAINT_TYPE_TRACKTO:
250                                                         {
251                                                                 bTrackToConstraint *data=con->data;
252                                                                 if (data->tar) doit++;
253                                                         }
254                                                         break;
255                                                 case CONSTRAINT_TYPE_NULL:
256                                                         break;
257                                                 case CONSTRAINT_TYPE_ROTLIKE:
258                                                         {
259                                                                 bRotateLikeConstraint *data=con->data;
260                                                                 if (data->tar) doit++;
261                                                                 
262                                                         }
263                                                         break;
264                                                 case CONSTRAINT_TYPE_LOCLIKE:
265                                                         {
266                                                                 bLocateLikeConstraint *data=con->data;
267                                                                 if (data->tar) doit++;
268                                                         }
269                                                         break;
270                                                 case CONSTRAINT_TYPE_ACTION:
271                                                         {
272                                                                 bActionConstraint *data=con->data;
273                                                                 if (data->tar) doit++;
274                                                         }
275                                                         break;
276                                                 default:
277                                                         break;
278                                                 }
279                                         }
280                                 }
281                                 
282                                 if(par) doit++;
283                                 
284                                 test= tempbase.first;
285                                 while(test) {
286                                         
287                                         if(test->object==base->object->parent) doit--;
288                                         if(test->object==base->object->track) doit--;
289                                         if(test->object==par) doit--;
290                                         
291                                         /* Decrement constraints */
292                                         {
293                                                 bConstraint *con;
294                                                 for (con = base->object->constraints.first; con; con=con->next){
295                                                         switch (con->type){
296                                                         case CONSTRAINT_TYPE_KINEMATIC:
297                                                                 {
298                                                                         bKinematicConstraint *data=con->data;
299                                                                         if (test->object == data->tar && test->object!=base->object) doit--;
300                                                                 }
301                                                                 break;
302                                                         case CONSTRAINT_TYPE_TRACKTO:
303                                                                 {
304                                                                         bTrackToConstraint *data=con->data;
305                                                                         if (test->object == data->tar && test->object!=base->object) doit--;
306                                                                 }
307                                                                 break;
308                                                         case CONSTRAINT_TYPE_NULL:
309                                                                 break;
310                                                         case CONSTRAINT_TYPE_ROTLIKE:
311                                                                 {
312                                                                         bRotateLikeConstraint *data=con->data;
313                                                                         if (test->object == data->tar && test->object!=base->object) doit--;
314                                                                         
315                                                                 }
316                                                                 break;
317                                                         case CONSTRAINT_TYPE_LOCLIKE:
318                                                                 {
319                                                                         bLocateLikeConstraint *data=con->data;
320                                                                         if (test->object == data->tar && test->object!=base->object) doit--;
321                                                                 }
322                                                                 break;
323                                                         case CONSTRAINT_TYPE_ACTION:
324                                                                 {
325                                                                         bActionConstraint *data=con->data;
326                                                                         if (test->object == data->tar && test->object!=base->object) doit--;
327                                                                 }
328                                                                 break;
329                                                         default:
330                                                                 break;
331                                                         }
332                                                 }
333                                         }
334                                         
335                                         if(doit==0) break;
336                                         test= test->next;
337                                 }
338                                 
339                                 if(test) BLI_insertlink(&tempbase, test, base);
340                                 else {
341                                         BLI_addhead(&tempbase, base);
342                                         domore++;
343                                 }
344                                 
345                         }
346                         else BLI_addtail(&noparentbase, base);
347                         
348                 }
349                 sce->base= noparentbase;
350                 addlisttolist(&sce->base, &tempbase);
351                 addlisttolist(&sce->base, &notyetbase);
352
353         }
354 }
355
356
357 void set_scene_bg(Scene *sce)
358 {
359         Base *base;
360         Object *ob;
361         Group *group;
362         GroupObject *go;
363         int flag;
364         
365         G.scene= sce;
366         
367         /* deselect objects (for dataselect) */
368         ob= G.main->object.first;
369         while(ob) {
370                 ob->flag &= ~(SELECT|OB_FROMGROUP);
371                 ob= ob->id.next;
372         }
373
374         /* group flags again */
375         group= G.main->group.first;
376         while(group) {
377                 go= group->gobject.first;
378                 while(go) {
379                         if(go->ob) go->ob->flag |= OB_FROMGROUP;
380                         go= go->next;
381                 }
382                 group= group->id.next;
383         }
384
385         /* sort baselist */
386         sort_baselist(sce);
387
388         /* copy layers and flags from bases to objects */
389         base= G.scene->base.first;
390         while(base) {
391                 
392                 base->object->lay= base->lay;
393                 
394                 base->flag &= ~OB_FROMGROUP;
395                 flag= base->object->flag & OB_FROMGROUP;
396                 base->flag |= flag;
397                 
398                 base->object->ctime= -1234567.0;        /* force ipo to be calculated later */
399                 base= base->next;
400         }
401
402         do_all_ipos();  /* layers/materials */
403
404         BPY_do_all_scripts(SCRIPT_FRAMECHANGED);
405         do_all_keys();
406 #ifdef __NLA
407         do_all_actions();
408 #endif
409         do_all_ikas();
410
411
412 }
413
414 void set_scene_name(char *name)
415 {
416         Scene *sce;
417
418         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
419                 if (BLI_streq(name, sce->id.name+2)) {
420                         set_scene_bg(sce);
421                         return;
422                 }
423         }
424         
425         error("Can't find scene: %s", name);
426 }
427
428 /* used by metaballs
429  * doesnt return the original duplicated object, only dupli's
430  */
431 int next_object(int val, Base **base, Object **ob)
432 {
433         extern ListBase duplilist;
434         static Object *dupob;
435         static int fase;
436         int run_again=1;
437         
438         /* init */
439         if(val==0) {
440                 fase= F_START;
441                 dupob= 0;
442         }
443         else {
444                 
445                 /* run_again is set when a duplilist has been ended */
446                 while(run_again) {
447                         run_again= 0;
448                         
449                                 
450                                 
451                         /* the first base */
452                         if(fase==F_START) {
453                                 *base= G.scene->base.first;
454                                 if(*base) {
455                                         *ob= (*base)->object;
456                                         fase= F_SCENE;
457                                 }
458                                 else {
459                                     /* exception: empty scene */
460                                         if(G.scene->set && G.scene->set->base.first) {
461                                                 *base= G.scene->set->base.first;
462                                                 *ob= (*base)->object;
463                                                 fase= F_SET;
464                                         }
465                                 }
466                         }
467                         else {
468                                 if(*base && fase!=F_DUPLI) {
469                                         *base= (*base)->next;
470                                         if(*base) *ob= (*base)->object;
471                                         else {
472                                                 if(fase==F_SCENE) {
473                                                         /* scene is finished, now do the set */
474                                                         if(G.scene->set && G.scene->set->base.first) {
475                                                                 *base= G.scene->set->base.first;
476                                                                 *ob= (*base)->object;
477                                                                 fase= F_SET;
478                                                         }
479                                                 }
480                                         }
481                                 }
482                         }
483                         
484                         if(*base == 0) fase= F_START;
485                         else {
486                                 if(fase!=F_DUPLI) {
487                                         if( (*base)->object->transflag & OB_DUPLI) {
488                                                 
489                                                 make_duplilist(G.scene, (*base)->object);
490                                                 dupob= duplilist.first;
491                                                 
492                                         }
493                                 }
494                                 /* handle dupli's */
495                                 if(dupob) {
496                                         
497                                         *ob= dupob;
498                                         fase= F_DUPLI;
499                                         
500                                         dupob= dupob->id.next;
501                                 }
502                                 else if(fase==F_DUPLI) {
503                                         fase= F_SCENE;
504                                         free_duplilist();
505                                         run_again= 1;
506                                 }
507                                 
508                         }
509                 }
510         }
511         
512         return fase;
513 }
514
515 Object *scene_find_camera(Scene *sc)
516 {
517         Base *base;
518         
519         for (base= sc->base.first; base; base= base->next)
520                 if (base->object->type==OB_CAMERA)
521                         return base->object;
522
523         return NULL;
524 }
525
526
527 Base *scene_add_base(Scene *sce, Object *ob)
528 {
529         Base *b= MEM_callocN(sizeof(*b), "scene_add_base");
530         BLI_addhead(&sce->base, b);
531
532         b->object= ob;
533         b->flag= ob->flag;
534         b->lay= ob->lay;
535
536         return b;
537 }
538
539 void scene_deselect_all(Scene *sce)
540 {
541         Base *b;
542
543         for (b= sce->base.first; b; b= b->next) {
544                 b->flag&= ~SELECT;
545                 b->object->flag= b->flag;
546         }
547 }
548
549 void scene_select_base(Scene *sce, Base *selbase)
550 {
551         scene_deselect_all(sce);
552
553         selbase->flag |= SELECT;
554         selbase->object->flag= selbase->flag;
555
556         sce->basact= selbase;
557 }