53b4a09d89bc79b8c9a4e90ce72f7b60d992fb55
[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 "DNA_armature_types.h" 
50 #include "DNA_constraint_types.h"
51 #include "DNA_curve_types.h"
52 #include "DNA_group_types.h"
53 #include "DNA_lamp_types.h"
54 #include "DNA_material_types.h"
55 #include "DNA_meta_types.h"
56 #include "DNA_object_types.h"
57 #include "DNA_scene_types.h"
58 #include "DNA_scriptlink_types.h"
59 #include "DNA_userdef_types.h"
60
61 #include "BKE_action.h"                 
62 #include "BKE_anim.h"
63 #include "BKE_armature.h"               
64 #include "BKE_bad_level_calls.h"
65 #include "BKE_constraint.h"
66 #include "BKE_depsgraph.h"
67 #include "BKE_global.h"
68 #include "BKE_ipo.h"
69 #include "BKE_key.h"
70 #include "BKE_library.h"
71 #include "BKE_main.h"
72 #include "BKE_object.h"
73 #include "BKE_scene.h"
74 #include "BKE_world.h"
75 #include "BKE_utildefines.h"
76
77 #include "BPY_extern.h"
78
79 #include "BLI_blenlib.h"
80
81 #include "nla.h"
82
83 #ifdef WIN32
84 #else
85 #include <sys/time.h>
86 #endif
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         BLI_freelistN(&sce->markers);
145         
146         if(sce->toolsettings){
147                 MEM_freeN(sce->toolsettings);
148                 sce->toolsettings = NULL;       
149         }
150         
151         if (sce->theDag) {
152                 free_forest(sce->theDag);
153                 MEM_freeN(sce->theDag);
154         }
155 }
156
157 Scene *add_scene(char *name)
158 {
159         Scene *sce;
160
161         sce= alloc_libblock(&G.main->scene, ID_SCE, name);
162         sce->lay= 1;
163         sce->selectmode= SCE_SELECT_VERTEX;
164         sce->editbutsize= 0.1;
165         
166         sce->r.mode= R_GAMMA;
167         sce->r.cfra= 1;
168         sce->r.sfra= 1;
169         sce->r.efra= 250;
170         sce->r.xsch= 320;
171         sce->r.ysch= 256;
172         sce->r.xasp= 1;
173         sce->r.yasp= 1;
174         sce->r.xparts= 1;
175         sce->r.yparts= 1;
176         sce->r.size= 100;
177         sce->r.planes= 24;
178         sce->r.quality= 90;
179         sce->r.framapto= 100;
180         sce->r.images= 100;
181         sce->r.framelen= 1.0;
182         sce->r.frs_sec= 25;
183
184         sce->r.postgamma= 1.0;
185         sce->r.postsat= 1.0;
186         sce->r.postmul= 1.0;
187         
188         sce->r.focus= 0.9;
189         sce->r.zgamma= 1.0;
190         sce->r.zsigma= 4.0;
191         sce->r.zblur= 10.0;
192         sce->r.zmin= 0.8;
193         
194         sce->r.xplay= 640;
195         sce->r.yplay= 480;
196         sce->r.freqplay= 60;
197         sce->r.depth= 32;
198
199         sce->r.stereomode = 1;  // no stereo
200
201         sce->toolsettings = MEM_mallocN(sizeof(struct ToolSettings),"Tool Settings Struct");
202         sce->toolsettings->cornertype=1;
203         sce->toolsettings->degr = 90; 
204         sce->toolsettings->step = 9;
205         sce->toolsettings->turn = 1;                            
206         sce->toolsettings->extr_offs = 1; 
207         sce->toolsettings->doublimit = 0.001;
208         sce->toolsettings->segments = 32;
209         sce->toolsettings->rings = 32;
210         sce->toolsettings->vertices = 32;
211         sce->toolsettings->editbutflag = 1;
212
213         strcpy(sce->r.backbuf, "//backbuf");
214         strcpy(sce->r.pic, U.renderdir);
215         strcpy(sce->r.ftype, "//ftype");
216         
217         BLI_init_rctf(&sce->r.safety, 0.1f, 0.9f, 0.1f, 0.9f);
218         sce->r.osa= 8;
219         
220         return sce;
221 }
222
223 int object_in_scene(Object *ob, Scene *sce)
224 {
225         Base *base;
226         
227         base= sce->base.first;
228         while(base) {
229                 if(base->object == ob) return 1;
230                 base= base->next;
231         }
232         return 0;
233 }
234
235 void set_scene_bg(Scene *sce)
236 {
237         Base *base;
238         Object *ob;
239         Group *group;
240         GroupObject *go;
241         int flag;
242         
243         G.scene= sce;
244         
245         /* deselect objects (for dataselect) */
246         ob= G.main->object.first;
247         while(ob) {
248                 ob->flag &= ~(SELECT|OB_FROMGROUP);
249                 ob= ob->id.next;
250         }
251
252         /* group flags again */
253         group= G.main->group.first;
254         while(group) {
255                 go= group->gobject.first;
256                 while(go) {
257                         if(go->ob) go->ob->flag |= OB_FROMGROUP;
258                         go= go->next;
259                 }
260                 group= group->id.next;
261         }
262
263         /* sort baselist */
264         DAG_scene_sort(sce);
265
266         /* copy layers and flags from bases to objects */
267         base= G.scene->base.first;
268         while(base) {
269                 ob= base->object;
270                 ob->lay= base->lay;
271                 
272                 /* group patch... */
273                 base->flag &= ~(OB_FROMGROUP);
274                 flag= ob->flag & (OB_FROMGROUP);
275                 base->flag |= flag;
276                 
277                 ob->flag= base->flag;
278                 ob->recalc= OB_RECALC;
279                 
280                 ob->ctime= -1234567.0;  /* force ipo to be calculated later */
281                 base= base->next;
282         }
283         // full update
284         scene_update_for_newframe(sce, sce->lay);
285         
286         /* do we need FRAMECHANGED in set_scene? */
287 //      if (G.f & G_DOSCRIPTLINKS) BPY_do_all_scripts(SCRIPT_FRAMECHANGED);
288 }
289
290 void set_scene_name(char *name)
291 {
292         Scene *sce;
293
294         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
295                 if (BLI_streq(name, sce->id.name+2)) {
296                         set_scene_bg(sce);
297                         return;
298                 }
299         }
300         
301         error("Can't find scene: %s", name);
302 }
303
304 /* used by metaballs
305  * doesnt return the original duplicated object, only dupli's
306  */
307 int next_object(int val, Base **base, Object **ob)
308 {
309         extern ListBase duplilist;
310         static Object *dupob;
311         static int fase;
312         int run_again=1;
313         
314         /* init */
315         if(val==0) {
316                 fase= F_START;
317                 dupob= 0;
318         }
319         else {
320
321                 /* run_again is set when a duplilist has been ended */
322                 while(run_again) {
323                         run_again= 0;
324                         
325                                 
326
327                         /* the first base */
328                         if(fase==F_START) {
329                                 *base= G.scene->base.first;
330                                 if(*base) {
331                                         *ob= (*base)->object;
332                                         fase= F_SCENE;
333                                 }
334                                 else {
335                                     /* exception: empty scene */
336                                         if(G.scene->set && G.scene->set->base.first) {
337                                                 *base= G.scene->set->base.first;
338                                                 *ob= (*base)->object;
339                                                 fase= F_SET;
340                                         }
341                                 }
342                         }
343                         else {
344                                 if(*base && fase!=F_DUPLI) {
345                                         *base= (*base)->next;
346                                         if(*base) *ob= (*base)->object;
347                                         else {
348                                                 if(fase==F_SCENE) {
349                                                         /* scene is finished, now do the set */
350                                                         if(G.scene->set && G.scene->set->base.first) {
351                                                                 *base= G.scene->set->base.first;
352                                                                 *ob= (*base)->object;
353                                                                 fase= F_SET;
354                                                         }
355                                                 }
356                                         }
357                                 }
358                         }
359                         
360                         if(*base == 0) fase= F_START;
361                         else {
362                                 if(fase!=F_DUPLI) {
363                                         if( (*base)->object->transflag & OB_DUPLI) {
364                                                 
365                                                 make_duplilist(G.scene, (*base)->object);
366                                                 dupob= duplilist.first;
367                                                 
368                                         }
369                                 }
370                                 /* handle dupli's */
371                                 if(dupob) {
372                                         
373                                         *ob= dupob;
374                                         fase= F_DUPLI;
375                                         
376                                         dupob= dupob->id.next;
377                                 }
378                                 else if(fase==F_DUPLI) {
379                                         fase= F_SCENE;
380                                         free_duplilist();
381                                         run_again= 1;
382                                 }
383                                 
384                         }
385                 }
386         }
387         
388         return fase;
389 }
390
391 Object *scene_find_camera(Scene *sc)
392 {
393         Base *base;
394         
395         for (base= sc->base.first; base; base= base->next)
396                 if (base->object->type==OB_CAMERA)
397                         return base->object;
398
399         return NULL;
400 }
401
402
403 Base *scene_add_base(Scene *sce, Object *ob)
404 {
405         Base *b= MEM_callocN(sizeof(*b), "scene_add_base");
406         BLI_addhead(&sce->base, b);
407
408         b->object= ob;
409         b->flag= ob->flag;
410         b->lay= ob->lay;
411
412         return b;
413 }
414
415 void scene_deselect_all(Scene *sce)
416 {
417         Base *b;
418
419         for (b= sce->base.first; b; b= b->next) {
420                 b->flag&= ~SELECT;
421                 b->object->flag= b->flag;
422         }
423 }
424
425 void scene_select_base(Scene *sce, Base *selbase)
426 {
427         scene_deselect_all(sce);
428
429         selbase->flag |= SELECT;
430         selbase->object->flag= selbase->flag;
431
432         sce->basact= selbase;
433 }
434
435 /* applies changes right away */
436 void scene_update_for_newframe(Scene *sce, unsigned int lay)
437 {
438         Base *base;
439         Object *ob;
440         int setcount=0;
441         
442         /* object ipos are calculated in where_is_object */
443         do_all_data_ipos();
444         
445         if (G.f & G_DOSCRIPTLINKS) BPY_do_all_scripts(SCRIPT_FRAMECHANGED);
446         
447         /* for time being; sets otherwise can be cyclic */
448         while(sce && setcount<2) {
449                 if(sce->theDag==NULL)
450                         DAG_scene_sort(sce);
451                 
452                 DAG_scene_update_flags(sce, lay);   // only stuff that moves
453                 
454                 for(base= sce->base.first; base; base= base->next) {
455                         ob= base->object;
456                         
457                         object_handle_update(ob);   // bke_object.h
458                         
459                         /* only update layer when an ipo */
460                         if(ob->ipo && has_ipo_code(ob->ipo, OB_LAY) ) {
461                                 base->lay= ob->lay;
462                         }
463                 }
464                 sce= sce->set;
465                 setcount++;
466         }
467 }