Orange; more render & compo stuff!
[blender-staging.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_group.h"
69 #include "BKE_ipo.h"
70 #include "BKE_key.h"
71 #include "BKE_library.h"
72 #include "BKE_main.h"
73 #include "BKE_node.h"
74 #include "BKE_object.h"
75 #include "BKE_scene.h"
76 #include "BKE_world.h"
77 #include "BKE_utildefines.h"
78
79 #include "BPY_extern.h"
80 #include "BLI_arithb.h"
81 #include "BLI_blenlib.h"
82
83 #include "nla.h"
84
85 #ifdef WIN32
86 #else
87 #include <sys/time.h>
88 #endif
89
90 void free_avicodecdata(AviCodecData *acd)
91 {
92         if (acd) {
93                 if (acd->lpFormat){
94                         MEM_freeN(acd->lpFormat);
95                         acd->lpFormat = NULL;
96                         acd->cbFormat = 0;
97                 }
98                 if (acd->lpParms){
99                         MEM_freeN(acd->lpParms);
100                         acd->lpParms = NULL;
101                         acd->cbParms = 0;
102                 }
103         }
104 }
105
106 void free_qtcodecdata(QuicktimeCodecData *qcd)
107 {
108         if (qcd) {
109                 if (qcd->cdParms){
110                         MEM_freeN(qcd->cdParms);
111                         qcd->cdParms = NULL;
112                         qcd->cdSize = 0;
113                 }
114         }
115 }
116
117 /* copy_scene moved to src/header_info.c... should be back */
118
119 /* do not free scene itself */
120 void free_scene(Scene *sce)
121 {
122         Base *base;
123
124         base= sce->base.first;
125         while(base) {
126                 base->object->id.us--;
127                 base= base->next;
128         }
129         /* do not free objects! */
130
131         BLI_freelistN(&sce->base);
132         free_editing(sce->ed);
133         if(sce->radio) MEM_freeN(sce->radio);
134         sce->radio= 0;
135         
136         BPY_free_scriptlink(&sce->scriptlink);
137         if (sce->r.avicodecdata) {
138                 free_avicodecdata(sce->r.avicodecdata);
139                 MEM_freeN(sce->r.avicodecdata);
140                 sce->r.avicodecdata = NULL;
141         }
142         if (sce->r.qtcodecdata) {
143                 free_qtcodecdata(sce->r.qtcodecdata);
144                 MEM_freeN(sce->r.qtcodecdata);
145                 sce->r.qtcodecdata = NULL;
146         }
147         
148         BLI_freelistN(&sce->markers);
149         BLI_freelistN(&sce->r.layers);
150         
151         if(sce->toolsettings){
152                 MEM_freeN(sce->toolsettings);
153                 sce->toolsettings = NULL;       
154         }
155         
156         if (sce->theDag) {
157                 free_forest(sce->theDag);
158                 MEM_freeN(sce->theDag);
159         }
160         
161         if(sce->nodetree) {
162                 ntreeFreeTree(sce->nodetree);
163                 MEM_freeN(sce->nodetree);
164         }
165 }
166
167 Scene *add_scene(char *name)
168 {
169         Scene *sce;
170
171         sce= alloc_libblock(&G.main->scene, ID_SCE, name);
172         sce->lay= 1;
173         sce->selectmode= SCE_SELECT_VERTEX;
174         sce->editbutsize= 0.1;
175         
176         sce->r.mode= R_GAMMA;
177         sce->r.cfra= 1;
178         sce->r.sfra= 1;
179         sce->r.efra= 250;
180         sce->r.xsch= 320;
181         sce->r.ysch= 256;
182         sce->r.xasp= 1;
183         sce->r.yasp= 1;
184         sce->r.xparts= 4;
185         sce->r.yparts= 4;
186         sce->r.size= 100;
187         sce->r.planes= 24;
188         sce->r.quality= 90;
189         sce->r.framapto= 100;
190         sce->r.images= 100;
191         sce->r.framelen= 1.0;
192         sce->r.frs_sec= 25;
193
194         sce->r.postgamma= 1.0;
195         sce->r.postsat= 1.0;
196         sce->r.postmul= 1.0;
197         
198         sce->r.focus= 0.9;
199         sce->r.zgamma= 1.0;
200         sce->r.zsigma= 4.0;
201         sce->r.zblur= 10.0;
202         sce->r.zmin= 0.8;
203         
204         sce->r.xplay= 640;
205         sce->r.yplay= 480;
206         sce->r.freqplay= 60;
207         sce->r.depth= 32;
208
209         sce->r.stereomode = 1;  // no stereo
210
211         sce->toolsettings = MEM_mallocN(sizeof(struct ToolSettings),"Tool Settings Struct");
212         sce->toolsettings->cornertype=1;
213         sce->toolsettings->degr = 90; 
214         sce->toolsettings->step = 9;
215         sce->toolsettings->turn = 1;                            
216         sce->toolsettings->extr_offs = 1; 
217         sce->toolsettings->doublimit = 0.001;
218         sce->toolsettings->segments = 32;
219         sce->toolsettings->rings = 32;
220         sce->toolsettings->vertices = 32;
221         sce->toolsettings->editbutflag = 1;
222
223         strcpy(sce->r.backbuf, "//backbuf");
224         strcpy(sce->r.pic, U.renderdir);
225         strcpy(sce->r.ftype, "//ftype");
226         
227         BLI_init_rctf(&sce->r.safety, 0.1f, 0.9f, 0.1f, 0.9f);
228         sce->r.osa= 8;
229         
230         scene_add_render_layer(sce);
231         
232         return sce;
233 }
234
235 Base *object_in_scene(Object *ob, Scene *sce)
236 {
237         Base *base;
238         
239         base= sce->base.first;
240         while(base) {
241                 if(base->object == ob) return base;
242                 base= base->next;
243         }
244         return NULL;
245 }
246
247 void set_scene_bg(Scene *sce)
248 {
249         Base *base;
250         Object *ob;
251         Group *group;
252         GroupObject *go;
253         int flag;
254         
255         G.scene= sce;
256         
257         /* deselect objects (for dataselect) */
258         ob= G.main->object.first;
259         while(ob) {
260                 ob->flag &= ~(SELECT|OB_FROMGROUP);
261                 ob= ob->id.next;
262         }
263
264         /* group flags again */
265         group= G.main->group.first;
266         while(group) {
267                 go= group->gobject.first;
268                 while(go) {
269                         if(go->ob) go->ob->flag |= OB_FROMGROUP;
270                         go= go->next;
271                 }
272                 group= group->id.next;
273         }
274
275         /* sort baselist */
276         DAG_scene_sort(sce);
277
278         /* copy layers and flags from bases to objects */
279         base= G.scene->base.first;
280         while(base) {
281                 ob= base->object;
282                 ob->lay= base->lay;
283                 
284                 /* group patch... */
285                 base->flag &= ~(OB_FROMGROUP);
286                 flag= ob->flag & (OB_FROMGROUP);
287                 base->flag |= flag;
288                 
289                 ob->flag= base->flag;
290                 
291                 ob->ctime= -1234567.0;  /* force ipo to be calculated later */
292                 base= base->next;
293         }
294         // full animation update
295         scene_update_for_newframe(sce, sce->lay);
296         
297         /* do we need FRAMECHANGED in set_scene? */
298 //      if (G.f & G_DOSCRIPTLINKS) BPY_do_all_scripts(SCRIPT_FRAMECHANGED);
299 }
300
301 void set_scene_name(char *name)
302 {
303         Scene *sce;
304
305         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
306                 if (BLI_streq(name, sce->id.name+2)) {
307                         set_scene_bg(sce);
308                         return;
309                 }
310         }
311         
312         error("Can't find scene: %s", name);
313 }
314
315 /* used by metaballs
316  * doesnt return the original duplicated object, only dupli's
317  */
318 int next_object(int val, Base **base, Object **ob)
319 {
320         static ListBase *duplilist= NULL;
321         static DupliObject *dupob;
322         static int fase;
323         int run_again=1;
324         
325         /* init */
326         if(val==0) {
327                 fase= F_START;
328                 dupob= NULL;
329         }
330         else {
331
332                 /* run_again is set when a duplilist has been ended */
333                 while(run_again) {
334                         run_again= 0;
335
336                         /* the first base */
337                         if(fase==F_START) {
338                                 *base= G.scene->base.first;
339                                 if(*base) {
340                                         *ob= (*base)->object;
341                                         fase= F_SCENE;
342                                 }
343                                 else {
344                                     /* exception: empty scene */
345                                         if(G.scene->set && G.scene->set->base.first) {
346                                                 *base= G.scene->set->base.first;
347                                                 *ob= (*base)->object;
348                                                 fase= F_SET;
349                                         }
350                                 }
351                         }
352                         else {
353                                 if(*base && fase!=F_DUPLI) {
354                                         *base= (*base)->next;
355                                         if(*base) *ob= (*base)->object;
356                                         else {
357                                                 if(fase==F_SCENE) {
358                                                         /* scene is finished, now do the set */
359                                                         if(G.scene->set && G.scene->set->base.first) {
360                                                                 *base= G.scene->set->base.first;
361                                                                 *ob= (*base)->object;
362                                                                 fase= F_SET;
363                                                         }
364                                                 }
365                                         }
366                                 }
367                         }
368                         
369                         if(*base == NULL) fase= F_START;
370                         else {
371                                 if(fase!=F_DUPLI) {
372                                         if( (*base)->object->transflag & OB_DUPLI) {
373                                                 
374                                                 duplilist= object_duplilist(G.scene, (*base)->object);
375                                                 
376                                                 dupob= duplilist->first;
377                                                 
378                                         }
379                                 }
380                                 /* handle dupli's */
381                                 if(dupob) {
382                                         
383                                         Mat4CpyMat4(dupob->ob->obmat, dupob->mat);
384                                         
385                                         (*base)->flag |= OB_FROMDUPLI;
386                                         *ob= dupob->ob;
387                                         fase= F_DUPLI;
388                                         
389                                         dupob= dupob->next;
390                                 }
391                                 else if(fase==F_DUPLI) {
392                                         fase= F_SCENE;
393                                         (*base)->flag &= ~OB_FROMDUPLI;
394                                         
395                                         for(dupob= duplilist->first; dupob; dupob= dupob->next) {
396                                                 Mat4CpyMat4(dupob->ob->obmat, dupob->omat);
397                                         }
398                                         
399                                         BLI_freelistN(duplilist);
400                                         duplilist= NULL;
401                                         run_again= 1;
402                                 }
403                         }
404                 }
405         }
406         
407         return fase;
408 }
409
410 Object *scene_find_camera(Scene *sc)
411 {
412         Base *base;
413         
414         for (base= sc->base.first; base; base= base->next)
415                 if (base->object->type==OB_CAMERA)
416                         return base->object;
417
418         return NULL;
419 }
420
421
422 Base *scene_add_base(Scene *sce, Object *ob)
423 {
424         Base *b= MEM_callocN(sizeof(*b), "scene_add_base");
425         BLI_addhead(&sce->base, b);
426
427         b->object= ob;
428         b->flag= ob->flag;
429         b->lay= ob->lay;
430
431         return b;
432 }
433
434 void scene_deselect_all(Scene *sce)
435 {
436         Base *b;
437
438         for (b= sce->base.first; b; b= b->next) {
439                 b->flag&= ~SELECT;
440                 b->object->flag= b->flag;
441         }
442 }
443
444 void scene_select_base(Scene *sce, Base *selbase)
445 {
446         scene_deselect_all(sce);
447
448         selbase->flag |= SELECT;
449         selbase->object->flag= selbase->flag;
450
451         sce->basact= selbase;
452 }
453
454 /* applies changes right away */
455 void scene_update_for_newframe(Scene *sce, unsigned int lay)
456 {
457         Base *base;
458         Object *ob;
459         int setcount=0;
460         
461         /* object ipos are calculated in where_is_object */
462         do_all_data_ipos();
463         
464         if (G.f & G_DOSCRIPTLINKS) BPY_do_all_scripts(SCRIPT_FRAMECHANGED);
465         
466         /* for time being; sets otherwise can be cyclic */
467         while(sce && setcount<2) {
468                 if(sce->theDag==NULL)
469                         DAG_scene_sort(sce);
470                 
471                 DAG_scene_update_flags(sce, lay);   // only stuff that moves
472                 
473                 for(base= sce->base.first; base; base= base->next) {
474                         ob= base->object;
475                         
476                         object_handle_update(ob);   // bke_object.h
477                         
478                         /* only update layer when an ipo */
479                         if(ob->ipo && has_ipo_code(ob->ipo, OB_LAY) ) {
480                                 base->lay= ob->lay;
481                         }
482                 }
483                 sce= sce->set;
484                 setcount++;
485         }
486 }
487
488 /* return default layer, also used to patch old files */
489 void scene_add_render_layer(Scene *sce)
490 {
491         SceneRenderLayer *srl;
492         int tot= 1 + BLI_countlist(&sce->r.layers);
493         
494         srl= MEM_callocN(sizeof(SceneRenderLayer), "new render layer");
495         sprintf(srl->name, "%d RenderLayer", tot);
496         BLI_addtail(&sce->r.layers, srl);
497         
498         srl->lay= (1<<20) -1;
499         srl->layflag= 0x7FFF;   /* solid ztra halo strand */
500         srl->passflag= SCE_PASS_COMBINED|SCE_PASS_Z;
501 }
502