2.50: svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r17853...
[blender-staging.git] / source / blender / blenkernel / intern / scene.c
1 /*  scene.c
2  *  
3  * 
4  * $Id$
5  *
6  * ***** BEGIN GPL 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.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #include <stdio.h>
33 #include <string.h>
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #ifndef WIN32 
40 #include <unistd.h>
41 #else
42 #include <io.h>
43 #endif
44 #include "MEM_guardedalloc.h"
45
46 #include "DNA_anim_types.h"
47 #include "DNA_armature_types.h" 
48 #include "DNA_color_types.h"
49 #include "DNA_constraint_types.h"
50 #include "DNA_curve_types.h"
51 #include "DNA_group_types.h"
52 #include "DNA_lamp_types.h"
53 #include "DNA_material_types.h"
54 #include "DNA_meta_types.h"
55 #include "DNA_object_types.h"
56 #include "DNA_scene_types.h"
57 #include "DNA_scriptlink_types.h"
58 #include "DNA_texture_types.h"
59 #include "DNA_userdef_types.h"
60
61 #include "BKE_action.h"                 
62 #include "BKE_anim.h"
63 #include "BKE_animsys.h"
64 #include "BKE_armature.h"               
65 #include "BKE_colortools.h"
66 #include "BKE_colortools.h"
67 #include "BKE_constraint.h"
68 #include "BKE_depsgraph.h"
69 #include "BKE_global.h"
70 #include "BKE_group.h"
71 #include "BKE_ipo.h"
72 #include "BKE_idprop.h"
73 #include "BKE_image.h"
74 #include "BKE_key.h"
75 #include "BKE_library.h"
76 #include "BKE_main.h"
77 #include "BKE_node.h"
78 #include "BKE_object.h"
79 #include "BKE_scene.h"
80 #include "BKE_sculpt.h"
81 #include "BKE_sequence.h"
82 #include "BKE_world.h"
83 #include "BKE_utildefines.h"
84
85 //XXX #include "BIF_previewrender.h"
86 //XXX #include "BIF_editseq.h"
87
88 #ifndef DISABLE_PYTHON
89 #include "BPY_extern.h"
90 #endif
91
92 #include "BLI_arithb.h"
93 #include "BLI_blenlib.h"
94
95 //XXX #include "nla.h"
96
97 #ifdef WIN32
98 #else
99 #include <sys/time.h>
100 #endif
101
102 void free_avicodecdata(AviCodecData *acd)
103 {
104         if (acd) {
105                 if (acd->lpFormat){
106                         MEM_freeN(acd->lpFormat);
107                         acd->lpFormat = NULL;
108                         acd->cbFormat = 0;
109                 }
110                 if (acd->lpParms){
111                         MEM_freeN(acd->lpParms);
112                         acd->lpParms = NULL;
113                         acd->cbParms = 0;
114                 }
115         }
116 }
117
118 void free_qtcodecdata(QuicktimeCodecData *qcd)
119 {
120         if (qcd) {
121                 if (qcd->cdParms){
122                         MEM_freeN(qcd->cdParms);
123                         qcd->cdParms = NULL;
124                         qcd->cdSize = 0;
125                 }
126         }
127 }
128
129 /* copy_scene moved to src/header_info.c... should be back */
130
131 /* do not free scene itself */
132 void free_scene(Scene *sce)
133 {
134         Base *base;
135
136         base= sce->base.first;
137         while(base) {
138                 base->object->id.us--;
139                 base= base->next;
140         }
141         /* do not free objects! */
142
143         BLI_freelistN(&sce->base);
144         seq_free_editing(sce->ed);
145         if(sce->radio) MEM_freeN(sce->radio);
146         sce->radio= 0;
147         
148 #ifndef DISABLE_PYTHON
149         BPY_free_scriptlink(&sce->scriptlink);
150 #endif
151
152         BKE_free_animdata((ID *)sce);
153         
154         if (sce->r.avicodecdata) {
155                 free_avicodecdata(sce->r.avicodecdata);
156                 MEM_freeN(sce->r.avicodecdata);
157                 sce->r.avicodecdata = NULL;
158         }
159         if (sce->r.qtcodecdata) {
160                 free_qtcodecdata(sce->r.qtcodecdata);
161                 MEM_freeN(sce->r.qtcodecdata);
162                 sce->r.qtcodecdata = NULL;
163         }
164         if (sce->r.ffcodecdata.properties) {
165                 IDP_FreeProperty(sce->r.ffcodecdata.properties);
166                 MEM_freeN(sce->r.ffcodecdata.properties);
167                 sce->r.ffcodecdata.properties = NULL;
168         }
169         
170         BLI_freelistN(&sce->markers);
171         BLI_freelistN(&sce->transform_spaces);
172         BLI_freelistN(&sce->r.layers);
173         
174         if(sce->toolsettings) {
175                 if(sce->toolsettings->vpaint)
176                         MEM_freeN(sce->toolsettings->vpaint);
177                 if(sce->toolsettings->wpaint)
178                         MEM_freeN(sce->toolsettings->wpaint);
179                 if(sce->toolsettings->sculpt) {
180                         sculptsession_free(sce->toolsettings->sculpt);
181                         MEM_freeN(sce->toolsettings->sculpt);
182                 }
183                 
184                 MEM_freeN(sce->toolsettings);
185                 sce->toolsettings = NULL;       
186         }
187         
188         if (sce->theDag) {
189                 free_forest(sce->theDag);
190                 MEM_freeN(sce->theDag);
191         }
192         
193         if(sce->nodetree) {
194                 ntreeFreeTree(sce->nodetree);
195                 MEM_freeN(sce->nodetree);
196         }
197 }
198
199 Scene *add_scene(char *name)
200 {
201         Scene *sce;
202         ParticleEditSettings *pset;
203         int a;
204
205         sce= alloc_libblock(&G.main->scene, ID_SCE, name);
206         sce->lay= 1;
207         sce->selectmode= SCE_SELECT_VERTEX;
208         sce->editbutsize= 0.1;
209         sce->autokey_mode= U.autokey_mode;
210         
211         sce->r.mode= R_GAMMA;
212         sce->r.cfra= 1;
213         sce->r.sfra= 1;
214         sce->r.efra= 250;
215         sce->r.xsch= 320;
216         sce->r.ysch= 256;
217         sce->r.xasp= 1;
218         sce->r.yasp= 1;
219         sce->r.xparts= 4;
220         sce->r.yparts= 4;
221         sce->r.size= 100;
222         sce->r.planes= 24;
223         sce->r.quality= 90;
224         sce->r.framapto= 100;
225         sce->r.images= 100;
226         sce->r.framelen= 1.0;
227         sce->r.frs_sec= 25;
228         sce->r.frs_sec_base= 1;
229         sce->r.ocres = 128;
230         
231         sce->r.bake_mode= 1;    /* prevent to include render stuff here */
232         sce->r.bake_filter= 2;
233         sce->r.bake_osa= 5;
234         sce->r.bake_flag= R_BAKE_CLEAR;
235         sce->r.bake_normal_space= R_BAKE_SPACE_TANGENT;
236         
237         sce->r.xplay= 640;
238         sce->r.yplay= 480;
239         sce->r.freqplay= 60;
240         sce->r.depth= 32;
241         
242         sce->r.threads= 1;
243         
244         sce->r.stereomode = 1;  // no stereo
245
246         sce->r.simplify_subsurf= 6;
247         sce->r.simplify_particles= 1.0f;
248         sce->r.simplify_shadowsamples= 16;
249         sce->r.simplify_aosss= 1.0f;
250
251         sce->r.cineonblack= 95;
252         sce->r.cineonwhite= 685;
253         sce->r.cineongamma= 1.7f;
254         
255         sce->toolsettings = MEM_callocN(sizeof(struct ToolSettings),"Tool Settings Struct");
256         sce->toolsettings->cornertype=1;
257         sce->toolsettings->degr = 90; 
258         sce->toolsettings->step = 9;
259         sce->toolsettings->turn = 1;                            
260         sce->toolsettings->extr_offs = 1; 
261         sce->toolsettings->doublimit = 0.001;
262         sce->toolsettings->segments = 32;
263         sce->toolsettings->rings = 32;
264         sce->toolsettings->vertices = 32;
265         sce->toolsettings->editbutflag = 1;
266         sce->toolsettings->uvcalc_radius = 1.0f;
267         sce->toolsettings->uvcalc_cubesize = 1.0f;
268         sce->toolsettings->uvcalc_mapdir = 1;
269         sce->toolsettings->uvcalc_mapalign = 1;
270         sce->toolsettings->unwrapper = 1;
271         sce->toolsettings->select_thresh= 0.01f;
272         sce->toolsettings->jointrilimit = 0.8f;
273
274         sce->toolsettings->skgen_resolution = 100;
275         sce->toolsettings->skgen_threshold_internal     = 0.01f;
276         sce->toolsettings->skgen_threshold_external     = 0.01f;
277         sce->toolsettings->skgen_angle_limit                    = 45.0f;
278         sce->toolsettings->skgen_length_ratio                   = 1.3f;
279         sce->toolsettings->skgen_length_limit                   = 1.5f;
280         sce->toolsettings->skgen_correlation_limit              = 0.98f;
281         sce->toolsettings->skgen_symmetry_limit                 = 0.1f;
282         sce->toolsettings->skgen_postpro = SKGEN_SMOOTH;
283         sce->toolsettings->skgen_postpro_passes = 1;
284         sce->toolsettings->skgen_options = SKGEN_FILTER_INTERNAL|SKGEN_FILTER_EXTERNAL|SKGEN_FILTER_SMART|SKGEN_HARMONIC|SKGEN_SUB_CORRELATION|SKGEN_STICK_TO_EMBEDDING;
285         sce->toolsettings->skgen_subdivisions[0] = SKGEN_SUB_CORRELATION;
286         sce->toolsettings->skgen_subdivisions[1] = SKGEN_SUB_LENGTH;
287         sce->toolsettings->skgen_subdivisions[2] = SKGEN_SUB_ANGLE;
288
289         pset= &sce->toolsettings->particle;
290         pset->flag= PE_KEEP_LENGTHS|PE_LOCK_FIRST|PE_DEFLECT_EMITTER;
291         pset->emitterdist= 0.25f;
292         pset->totrekey= 5;
293         pset->totaddkey= 5;
294         pset->brushtype= PE_BRUSH_NONE;
295         for(a=0; a<PE_TOT_BRUSH; a++) {
296                 pset->brush[a].strength= 50;
297                 pset->brush[a].size= 50;
298                 pset->brush[a].step= 10;
299         }
300         pset->brush[PE_BRUSH_CUT].strength= 100;
301         
302         sce->jumpframe = 10;
303         sce->audio.mixrate = 44100;
304
305         strcpy(sce->r.backbuf, "//backbuf");
306         strcpy(sce->r.pic, U.renderdir);
307
308         BLI_init_rctf(&sce->r.safety, 0.1f, 0.9f, 0.1f, 0.9f);
309         sce->r.osa= 8;
310
311         /* note; in header_info.c the scene copy happens..., if you add more to renderdata it has to be checked there */
312         scene_add_render_layer(sce);
313         
314         return sce;
315 }
316
317 Base *object_in_scene(Object *ob, Scene *sce)
318 {
319         Base *base;
320         
321         base= sce->base.first;
322         while(base) {
323                 if(base->object == ob) return base;
324                 base= base->next;
325         }
326         return NULL;
327 }
328
329 void set_scene_bg(Scene *scene)
330 {
331         Scene *sce;
332         Base *base;
333         Object *ob;
334         Group *group;
335         GroupObject *go;
336         int flag;
337         
338         /* check for cyclic sets, for reading old files but also for definite security (py?) */
339         scene_check_setscene(scene);
340         
341         /* deselect objects (for dataselect) */
342         for(ob= G.main->object.first; ob; ob= ob->id.next)
343                 ob->flag &= ~(SELECT|OB_FROMGROUP);
344
345         /* group flags again */
346         for(group= G.main->group.first; group; group= group->id.next) {
347                 go= group->gobject.first;
348                 while(go) {
349                         if(go->ob) go->ob->flag |= OB_FROMGROUP;
350                         go= go->next;
351                 }
352         }
353
354         /* sort baselist */
355         DAG_scene_sort(scene);
356         
357         /* ensure dags are built for sets */
358         for(sce= scene->set; sce; sce= sce->set)
359                 if(sce->theDag==NULL)
360                         DAG_scene_sort(sce);
361
362         /* copy layers and flags from bases to objects */
363         for(base= scene->base.first; base; base= base->next) {
364                 ob= base->object;
365                 ob->lay= base->lay;
366                 
367                 /* group patch... */
368                 base->flag &= ~(OB_FROMGROUP);
369                 flag= ob->flag & (OB_FROMGROUP);
370                 base->flag |= flag;
371                 
372                 /* not too nice... for recovering objects with lost data */
373                 if(ob->pose==NULL) base->flag &= ~OB_POSEMODE;
374                 ob->flag= base->flag;
375                 
376                 ob->ctime= -1234567.0;  /* force ipo to be calculated later */
377         }
378         /* no full animation update, this to enable render code to work (render code calls own animation updates) */
379         
380         /* do we need FRAMECHANGED in set_scene? */
381 //      if (G.f & G_DOSCRIPTLINKS) BPY_do_all_scripts(SCRIPT_FRAMECHANGED, 0);
382 }
383
384 /* called from creator.c */
385 void set_scene_name(char *name)
386 {
387         Scene *sce;
388
389         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
390                 if (BLI_streq(name, sce->id.name+2)) {
391                         set_scene_bg(sce);
392                         return;
393                 }
394         }
395         
396         //XXX error("Can't find scene: %s", name);
397 }
398
399 /* used by metaballs
400  * doesnt return the original duplicated object, only dupli's
401  */
402 int next_object(Scene *scene, int val, Base **base, Object **ob)
403 {
404         static ListBase *duplilist= NULL;
405         static DupliObject *dupob;
406         static int fase;
407         int run_again=1;
408         
409         /* init */
410         if(val==0) {
411                 fase= F_START;
412                 dupob= NULL;
413         }
414         else {
415
416                 /* run_again is set when a duplilist has been ended */
417                 while(run_again) {
418                         run_again= 0;
419
420                         /* the first base */
421                         if(fase==F_START) {
422                                 *base= scene->base.first;
423                                 if(*base) {
424                                         *ob= (*base)->object;
425                                         fase= F_SCENE;
426                                 }
427                                 else {
428                                     /* exception: empty scene */
429                                         if(scene->set && scene->set->base.first) {
430                                                 *base= scene->set->base.first;
431                                                 *ob= (*base)->object;
432                                                 fase= F_SET;
433                                         }
434                                 }
435                         }
436                         else {
437                                 if(*base && fase!=F_DUPLI) {
438                                         *base= (*base)->next;
439                                         if(*base) *ob= (*base)->object;
440                                         else {
441                                                 if(fase==F_SCENE) {
442                                                         /* scene is finished, now do the set */
443                                                         if(scene->set && scene->set->base.first) {
444                                                                 *base= scene->set->base.first;
445                                                                 *ob= (*base)->object;
446                                                                 fase= F_SET;
447                                                         }
448                                                 }
449                                         }
450                                 }
451                         }
452                         
453                         if(*base == NULL) fase= F_START;
454                         else {
455                                 if(fase!=F_DUPLI) {
456                                         if( (*base)->object->transflag & OB_DUPLI) {
457                                                 /* groups cannot be duplicated for mballs yet, 
458                                                 this enters eternal loop because of 
459                                                 makeDispListMBall getting called inside of group_duplilist */
460                                                 if((*base)->object->dup_group == NULL) {
461                                                         duplilist= object_duplilist(scene, (*base)->object);
462                                                         
463                                                         dupob= duplilist->first;
464
465                                                         if(!dupob)
466                                                                 free_object_duplilist(duplilist);
467                                                 }
468                                         }
469                                 }
470                                 /* handle dupli's */
471                                 if(dupob) {
472                                         
473                                         Mat4CpyMat4(dupob->ob->obmat, dupob->mat);
474                                         
475                                         (*base)->flag |= OB_FROMDUPLI;
476                                         *ob= dupob->ob;
477                                         fase= F_DUPLI;
478                                         
479                                         dupob= dupob->next;
480                                 }
481                                 else if(fase==F_DUPLI) {
482                                         fase= F_SCENE;
483                                         (*base)->flag &= ~OB_FROMDUPLI;
484                                         
485                                         for(dupob= duplilist->first; dupob; dupob= dupob->next) {
486                                                 Mat4CpyMat4(dupob->ob->obmat, dupob->omat);
487                                         }
488                                         
489                                         free_object_duplilist(duplilist);
490                                         duplilist= NULL;
491                                         run_again= 1;
492                                 }
493                         }
494                 }
495         }
496         
497         return fase;
498 }
499
500 Object *scene_find_camera(Scene *sc)
501 {
502         Base *base;
503         
504         for (base= sc->base.first; base; base= base->next)
505                 if (base->object->type==OB_CAMERA)
506                         return base->object;
507
508         return NULL;
509 }
510
511
512 Base *scene_add_base(Scene *sce, Object *ob)
513 {
514         Base *b= MEM_callocN(sizeof(*b), "scene_add_base");
515         BLI_addhead(&sce->base, b);
516
517         b->object= ob;
518         b->flag= ob->flag;
519         b->lay= ob->lay;
520
521         return b;
522 }
523
524 void scene_deselect_all(Scene *sce)
525 {
526         Base *b;
527
528         for (b= sce->base.first; b; b= b->next) {
529                 b->flag&= ~SELECT;
530                 b->object->flag= b->flag;
531         }
532 }
533
534 void scene_select_base(Scene *sce, Base *selbase)
535 {
536         scene_deselect_all(sce);
537
538         selbase->flag |= SELECT;
539         selbase->object->flag= selbase->flag;
540
541         sce->basact= selbase;
542 }
543
544 /* checks for cycle, returns 1 if it's all OK */
545 int scene_check_setscene(Scene *sce)
546 {
547         Scene *scene;
548         int a, totscene;
549         
550         if(sce->set==NULL) return 1;
551         
552         totscene= 0;
553         for(scene= G.main->scene.first; scene; scene= scene->id.next)
554                 totscene++;
555         
556         for(a=0, scene=sce; scene->set; scene=scene->set, a++) {
557                 /* more iterations than scenes means we have a cycle */
558                 if(a > totscene) {
559                         /* the tested scene gets zero'ed, that's typically current scene */
560                         sce->set= NULL;
561                         return 0;
562                 }
563         }
564
565         return 1;
566 }
567
568 static void scene_update(Scene *sce, unsigned int lay)
569 {
570         Base *base;
571         Object *ob;
572         
573         if(sce->theDag==NULL)
574                 DAG_scene_sort(sce);
575         
576         DAG_scene_update_flags(sce, lay);   // only stuff that moves or needs display still
577         
578         for(base= sce->base.first; base; base= base->next) {
579                 ob= base->object;
580                 
581                 object_handle_update(sce, ob);   // bke_object.h
582                 
583                 /* only update layer when an ipo */
584                         // XXX old animation system
585                 //if(ob->ipo && has_ipo_code(ob->ipo, OB_LAY) ) {
586                 //      base->lay= ob->lay;
587                 //}
588         }
589 }
590
591 /* This (evil) function is needed to cope with two legacy Blender rendering features
592  * mblur (motion blur that renders 'subframes' and blurs them together), and fields 
593  * rendering. Thus, the use of ugly globals from object.c
594  */
595 // BAD... EVIL... JUJU...!!!!
596 // XXX moved here temporarily
597 float frame_to_float (Scene *scene, int cfra)           /* see also bsystem_time in object.c */
598 {
599         extern float bluroffs;  /* bad stuff borrowed from object.c */
600         extern float fieldoffs;
601         float ctime;
602         
603         ctime= (float)cfra;
604         ctime+= bluroffs+fieldoffs;
605         ctime*= scene->r.framelen;
606         
607         return ctime;
608 }
609
610 /* applies changes right away, does all sets too */
611 void scene_update_for_newframe(Scene *sce, unsigned int lay)
612 {
613         Scene *scene= sce;
614         float ctime = frame_to_float(sce, sce->r.cfra); 
615         
616         /* clear animation overrides */
617         // XXX TODO...
618         
619         /* All 'standard' (i.e. without any dependencies) animation is handled here,
620          * with an 'local' to 'macro' order of evaluation. This should ensure that
621          * settings stored nestled within a hierarchy (i.e. settings in a Texture block
622          * can be overridden by settings from Scene, which owns the Texture through a hierarchy 
623          * such as Scene->World->MTex/Texture) can still get correctly overridden.
624          */
625         BKE_animsys_evaluate_all_animation(G.main, ctime);
626         
627         
628 #ifndef DISABLE_PYTHON
629         if (G.f & G_DOSCRIPTLINKS) BPY_do_all_scripts(SCRIPT_FRAMECHANGED, 0);
630 #endif
631         /* sets first, we allow per definition current scene to have dependencies on sets */
632         for(sce= sce->set; sce; sce= sce->set)
633                 scene_update(sce, lay);
634
635         scene_update(scene, lay);
636 }
637
638 /* return default layer, also used to patch old files */
639 void scene_add_render_layer(Scene *sce)
640 {
641         SceneRenderLayer *srl;
642         int tot= 1 + BLI_countlist(&sce->r.layers);
643         
644         srl= MEM_callocN(sizeof(SceneRenderLayer), "new render layer");
645         sprintf(srl->name, "%d RenderLayer", tot);
646         BLI_addtail(&sce->r.layers, srl);
647
648         /* note, this is also in render, pipeline.c, to make layer when scenedata doesnt have it */
649         srl->lay= (1<<20) -1;
650         srl->layflag= 0x7FFF;   /* solid ztra halo edge strand */
651         srl->passflag= SCE_PASS_COMBINED|SCE_PASS_Z;
652 }
653
654 void sculptsession_free(Sculpt *sculpt)
655 {
656         SculptSession *ss= sculpt->session;
657         if(ss) {
658                 if(ss->projverts)
659                         MEM_freeN(ss->projverts);
660
661                 if(ss->radialcontrol)
662                         MEM_freeN(ss->radialcontrol);
663
664                 if(ss->fmap)
665                         MEM_freeN(ss->fmap);
666
667                 if(ss->fmap_mem)
668                         MEM_freeN(ss->fmap_mem);
669
670                 if(ss->texcache)
671                         MEM_freeN(ss->texcache);
672                 MEM_freeN(ss);
673                 sculpt->session= NULL;
674         }
675 }
676
677 /* render simplification */
678
679 int get_render_subsurf_level(RenderData *r, int lvl)
680 {
681         if(G.rt == 1 && (r->mode & R_SIMPLIFY))
682                 return MIN2(r->simplify_subsurf, lvl);
683         else
684                 return lvl;
685 }
686
687 int get_render_child_particle_number(RenderData *r, int num)
688 {
689         if(G.rt == 1 && (r->mode & R_SIMPLIFY))
690                 return (int)(r->simplify_particles*num);
691         else
692                 return num;
693 }
694
695 int get_render_shadow_samples(RenderData *r, int samples)
696 {
697         if(G.rt == 1 && (r->mode & R_SIMPLIFY) && samples > 0)
698                 return MIN2(r->simplify_shadowsamples, samples);
699         else
700                 return samples;
701 }
702
703 float get_render_aosss_error(RenderData *r, float error)
704 {
705         if(G.rt == 1 && (r->mode & R_SIMPLIFY))
706                 return ((1.0f-r->simplify_aosss)*10.0f + 1.0f)*error;
707         else
708                 return error;
709 }
710