The zblur plugin (aka as DoF) integrated in render. Compared to patch
[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 #include <string.h>
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #ifndef WIN32 
43 #include <unistd.h>
44 #else
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_group_types.h"
65 #include "DNA_curve_types.h"
66 #include "DNA_userdef_types.h"
67
68 #include "BLI_blenlib.h"
69
70 #include "BKE_bad_level_calls.h"
71 #include "BKE_utildefines.h"
72
73 #include "BKE_global.h"
74 #include "BKE_main.h"
75 #include "BKE_anim.h"
76 #include "BKE_constraint.h"
77
78 #include "BKE_library.h"
79
80 #include "BKE_scene.h"
81 #include "BKE_world.h"
82 #include "BKE_ipo.h"
83 #include "BKE_ika.h"
84 #include "BKE_key.h"
85
86 #include "BPY_extern.h"
87
88 void free_avicodecdata(AviCodecData *acd)
89 {
90         if (acd) {
91                 if (acd->lpFormat){
92                         MEM_freeN(acd->lpFormat);
93                         acd->lpFormat = NULL;
94                         acd->cbFormat = 0;
95                 }
96                 if (acd->lpParms){
97                         MEM_freeN(acd->lpParms);
98                         acd->lpParms = NULL;
99                         acd->cbParms = 0;
100                 }
101         }
102 }
103
104 void free_qtcodecdata(QuicktimeCodecData *qcd)
105 {
106         if (qcd) {
107                 if (qcd->cdParms){
108                         MEM_freeN(qcd->cdParms);
109                         qcd->cdParms = NULL;
110                         qcd->cdSize = 0;
111                 }
112         }
113 }
114
115 /* do not free scene itself */
116 void free_scene(Scene *sce)
117 {
118         Base *base;
119
120         base= sce->base.first;
121         while(base) {
122                 base->object->id.us--;
123                 base= base->next;
124         }
125         /* do not free objects! */
126
127         BLI_freelistN(&sce->base);
128         free_editing(sce->ed);
129         if(sce->radio) MEM_freeN(sce->radio);
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         sce->selectmode= SCE_SELECT_VERTEX;
152         
153         sce->r.mode= R_GAMMA;
154         sce->r.cfra= 1;
155         sce->r.sfra= 1;
156         sce->r.efra= 250;
157         sce->r.xsch= 320;
158         sce->r.ysch= 256;
159         sce->r.xasp= 1;
160         sce->r.yasp= 1;
161         sce->r.xparts= 1;
162         sce->r.yparts= 1;
163         sce->r.size= 100;
164         sce->r.planes= 24;
165         sce->r.quality= 90;
166         sce->r.framapto= 100;
167         sce->r.images= 100;
168         sce->r.framelen= 1.0;
169         sce->r.frs_sec= 25;
170
171         sce->r.postgamma= 1.0;
172         sce->r.posthue= 1.0;
173         sce->r.postmul= 1.0;
174         
175         sce->r.focus= 0.9;
176         sce->r.zgamma= 1.0;
177         sce->r.zsigma= 4.0;
178         sce->r.zblur= 10.0;
179         sce->r.zmin= 0.8;
180         
181         sce->r.xplay= 640;
182         sce->r.yplay= 480;
183         sce->r.freqplay= 60;
184         sce->r.depth= 32;
185
186         sce->r.stereomode = 1;  // no stereo
187
188         strcpy(sce->r.backbuf, "//backbuf");
189         strcpy(sce->r.pic, U.renderdir);
190         strcpy(sce->r.ftype, "//ftype");
191         
192         BLI_init_rctf(&sce->r.safety, 0.1f, 0.9f, 0.1f, 0.9f);
193         sce->r.osa= 8;
194         
195         return sce;
196 }
197
198 int object_in_scene(Object *ob, Scene *sce)
199 {
200         Base *base;
201         
202         base= sce->base.first;
203         while(base) {
204                 if(base->object == ob) return 1;
205                 base= base->next;
206         }
207         return 0;
208 }
209
210 void sort_baselist(Scene *sce)
211 {
212         /* in order of parent and track */
213         ListBase tempbase, noparentbase, notyetbase;
214         Base *base, *test=NULL;
215         Object *par;
216         int doit, domore= 0, lastdomore=1;
217         
218         
219         /* keep same order when nothing has changed! */
220         
221         while(domore!=lastdomore) {
222
223                 lastdomore= domore;
224                 domore= 0;
225                 tempbase.first= tempbase.last= 0;
226                 noparentbase.first= noparentbase.last= 0;
227                 notyetbase.first= notyetbase.last= 0;
228                 
229                 while( (base= sce->base.first) ) {
230                         BLI_remlink(&sce->base, base);
231                         
232                         par= 0;
233                         if(base->object->type==OB_IKA) {
234                                 Ika *ika= base->object->data;
235                                 par= ika->parent;
236                         }
237
238                         if(par || base->object->parent || base->object->track) {
239                                 
240                                 doit= 0;
241                                 if(base->object->parent) doit++;
242                                 if(base->object->track) doit++;
243                                 
244                         /* Count constraints */
245                                 {
246                                         bConstraint *con;
247                                         for (con = base->object->constraints.first; con; con=con->next){
248                                                 if (constraint_has_target(con))
249                                                         doit++;
250                                         }
251                                 }
252                                 
253                                 if(par) doit++;
254                                 
255                                 test= tempbase.first;
256                                 while(test) {
257                                         
258                                         if(test->object==base->object->parent) doit--;
259                                         if(test->object==base->object->track) doit--;
260                                         if(test->object==par) doit--;
261                                         
262                                         /* Decrement constraints */
263                                         {
264                                                 bConstraint *con;
265                                                 for (con = base->object->constraints.first; con; con=con->next){
266                                                         if (test->object == get_constraint_target(con) && test->object!=base->object)
267                                                                 doit--;
268                                                 }
269                                         }
270                                         
271                                         if(doit==0) break;
272                                         test= test->next;
273                                 }
274                                 
275                                 if(test) BLI_insertlink(&tempbase, test, base);
276                                 else {
277                                         BLI_addhead(&tempbase, base);
278                                         domore++;
279                                 }
280                                 
281                         }
282                         else BLI_addtail(&noparentbase, base);
283                         
284                 }
285                 sce->base= noparentbase;
286                 addlisttolist(&sce->base, &tempbase);
287                 addlisttolist(&sce->base, &notyetbase);
288
289         }
290 }
291
292
293 void set_scene_bg(Scene *sce)
294 {
295         Base *base;
296         Object *ob;
297         Group *group;
298         GroupObject *go;
299         int flag;
300         
301         G.scene= sce;
302         
303         /* deselect objects (for dataselect) */
304         ob= G.main->object.first;
305         while(ob) {
306                 ob->flag &= ~(SELECT|OB_FROMGROUP);
307                 ob= ob->id.next;
308         }
309
310         /* group flags again */
311         group= G.main->group.first;
312         while(group) {
313                 go= group->gobject.first;
314                 while(go) {
315                         if(go->ob) go->ob->flag |= OB_FROMGROUP;
316                         go= go->next;
317                 }
318                 group= group->id.next;
319         }
320
321         /* sort baselist */
322         sort_baselist(sce);
323
324         /* copy layers and flags from bases to objects */
325         base= G.scene->base.first;
326         while(base) {
327                 
328                 base->object->lay= base->lay;
329                 
330                 /* group patch... */
331                 base->flag &= ~OB_FROMGROUP;
332                 flag= base->object->flag & OB_FROMGROUP;
333                 base->flag |= flag;
334                 
335                 base->object->flag= base->flag;
336                 
337                 base->object->ctime= -1234567.0;        /* force ipo to be calculated later */
338                 base= base->next;
339         }
340
341         do_all_ipos();  /* layers/materials */
342
343         BPY_do_all_scripts(SCRIPT_FRAMECHANGED);
344         do_all_keys();
345 #ifdef __NLA
346         do_all_actions(NULL);
347         rebuild_all_armature_displists();
348 #endif
349         do_all_ikas();
350
351
352 }
353
354 void set_scene_name(char *name)
355 {
356         Scene *sce;
357
358         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
359                 if (BLI_streq(name, sce->id.name+2)) {
360                         set_scene_bg(sce);
361                         return;
362                 }
363         }
364         
365         error("Can't find scene: %s", name);
366 }
367
368 /* used by metaballs
369  * doesnt return the original duplicated object, only dupli's
370  */
371 int next_object(int val, Base **base, Object **ob)
372 {
373         extern ListBase duplilist;
374         static Object *dupob;
375         static int fase;
376         int run_again=1;
377         
378         /* init */
379         if(val==0) {
380                 fase= F_START;
381                 dupob= 0;
382         }
383         else {
384
385                 /* run_again is set when a duplilist has been ended */
386                 while(run_again) {
387                         run_again= 0;
388                         
389                                 
390
391                         /* the first base */
392                         if(fase==F_START) {
393                                 *base= G.scene->base.first;
394                                 if(*base) {
395                                         *ob= (*base)->object;
396                                         fase= F_SCENE;
397                                 }
398                                 else {
399                                     /* exception: empty scene */
400                                         if(G.scene->set && G.scene->set->base.first) {
401                                                 *base= G.scene->set->base.first;
402                                                 *ob= (*base)->object;
403                                                 fase= F_SET;
404                                         }
405                                 }
406                         }
407                         else {
408                                 if(*base && fase!=F_DUPLI) {
409                                         *base= (*base)->next;
410                                         if(*base) *ob= (*base)->object;
411                                         else {
412                                                 if(fase==F_SCENE) {
413                                                         /* scene is finished, now do the set */
414                                                         if(G.scene->set && G.scene->set->base.first) {
415                                                                 *base= G.scene->set->base.first;
416                                                                 *ob= (*base)->object;
417                                                                 fase= F_SET;
418                                                         }
419                                                 }
420                                         }
421                                 }
422                         }
423                         
424                         if(*base == 0) fase= F_START;
425                         else {
426                                 if(fase!=F_DUPLI) {
427                                         if( (*base)->object->transflag & OB_DUPLI) {
428                                                 
429                                                 make_duplilist(G.scene, (*base)->object);
430                                                 dupob= duplilist.first;
431                                                 
432                                         }
433                                 }
434                                 /* handle dupli's */
435                                 if(dupob) {
436                                         
437                                         *ob= dupob;
438                                         fase= F_DUPLI;
439                                         
440                                         dupob= dupob->id.next;
441                                 }
442                                 else if(fase==F_DUPLI) {
443                                         fase= F_SCENE;
444                                         free_duplilist();
445                                         run_again= 1;
446                                 }
447                                 
448                         }
449                 }
450         }
451         
452         return fase;
453 }
454
455 Object *scene_find_camera(Scene *sc)
456 {
457         Base *base;
458         
459         for (base= sc->base.first; base; base= base->next)
460                 if (base->object->type==OB_CAMERA)
461                         return base->object;
462
463         return NULL;
464 }
465
466
467 Base *scene_add_base(Scene *sce, Object *ob)
468 {
469         Base *b= MEM_callocN(sizeof(*b), "scene_add_base");
470         BLI_addhead(&sce->base, b);
471
472         b->object= ob;
473         b->flag= ob->flag;
474         b->lay= ob->lay;
475
476         return b;
477 }
478
479 void scene_deselect_all(Scene *sce)
480 {
481         Base *b;
482
483         for (b= sce->base.first; b; b= b->next) {
484                 b->flag&= ~SELECT;
485                 b->object->flag= b->flag;
486         }
487 }
488
489 void scene_select_base(Scene *sce, Base *selbase)
490 {
491         scene_deselect_all(sce);
492
493         selbase->flag |= SELECT;
494         selbase->object->flag= selbase->flag;
495
496         sce->basact= selbase;
497 }