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