6 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
22 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23 * All rights reserved.
25 * The Original Code is: all of this file.
27 * Contributor(s): none yet.
29 * ***** END GPL LICENSE BLOCK *****
44 #include "MEM_guardedalloc.h"
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"
61 #include "BKE_action.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"
72 #include "BKE_idprop.h"
73 #include "BKE_image.h"
75 #include "BKE_library.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"
85 //XXX #include "BIF_previewrender.h"
86 //XXX #include "BIF_editseq.h"
88 #ifndef DISABLE_PYTHON
89 #include "BPY_extern.h"
92 #include "BLI_arithb.h"
93 #include "BLI_blenlib.h"
95 //XXX #include "nla.h"
102 void free_avicodecdata(AviCodecData *acd)
106 MEM_freeN(acd->lpFormat);
107 acd->lpFormat = NULL;
111 MEM_freeN(acd->lpParms);
118 void free_qtcodecdata(QuicktimeCodecData *qcd)
122 MEM_freeN(qcd->cdParms);
129 /* copy_scene moved to src/header_info.c... should be back */
131 /* do not free scene itself */
132 void free_scene(Scene *sce)
136 base= sce->base.first;
138 base->object->id.us--;
141 /* do not free objects! */
143 BLI_freelistN(&sce->base);
144 seq_free_editing(sce->ed);
146 #ifndef DISABLE_PYTHON
147 BPY_free_scriptlink(&sce->scriptlink);
150 BKE_free_animdata((ID *)sce);
151 BKE_keyingsets_free(&sce->keyingsets);
153 if (sce->r.avicodecdata) {
154 free_avicodecdata(sce->r.avicodecdata);
155 MEM_freeN(sce->r.avicodecdata);
156 sce->r.avicodecdata = NULL;
158 if (sce->r.qtcodecdata) {
159 free_qtcodecdata(sce->r.qtcodecdata);
160 MEM_freeN(sce->r.qtcodecdata);
161 sce->r.qtcodecdata = NULL;
163 if (sce->r.ffcodecdata.properties) {
164 IDP_FreeProperty(sce->r.ffcodecdata.properties);
165 MEM_freeN(sce->r.ffcodecdata.properties);
166 sce->r.ffcodecdata.properties = NULL;
169 BLI_freelistN(&sce->markers);
170 BLI_freelistN(&sce->transform_spaces);
171 BLI_freelistN(&sce->r.layers);
173 if(sce->toolsettings) {
174 if(sce->toolsettings->vpaint)
175 MEM_freeN(sce->toolsettings->vpaint);
176 if(sce->toolsettings->wpaint)
177 MEM_freeN(sce->toolsettings->wpaint);
178 if(sce->toolsettings->sculpt) {
179 sculptsession_free(sce->toolsettings->sculpt);
180 MEM_freeN(sce->toolsettings->sculpt);
183 MEM_freeN(sce->toolsettings);
184 sce->toolsettings = NULL;
188 free_forest(sce->theDag);
189 MEM_freeN(sce->theDag);
193 ntreeFreeTree(sce->nodetree);
194 MEM_freeN(sce->nodetree);
198 Scene *add_scene(char *name)
201 ParticleEditSettings *pset;
204 sce= alloc_libblock(&G.main->scene, ID_SCE, name);
207 sce->r.mode= R_GAMMA|R_OSA|R_SHADOW|R_SSS|R_ENVMAP|R_RAYTRACE;
220 sce->r.framapto= 100;
222 sce->r.framelen= 1.0;
224 sce->r.frs_sec_base= 1;
227 sce->r.bake_mode= 1; /* prevent to include render stuff here */
228 sce->r.bake_filter= 8;
230 sce->r.bake_flag= R_BAKE_CLEAR;
231 sce->r.bake_normal_space= R_BAKE_SPACE_TANGENT;
238 sce->r.scemode= R_DOCOMP|R_DOSEQ|R_EXTENSION;
239 sce->r.stamp= R_STAMP_TIME|R_STAMP_FRAME|R_STAMP_DATE|R_STAMP_SCENE|R_STAMP_CAMERA;
243 sce->r.stereomode = 1; // no stereo
244 sce->r.domeangle = 180;
247 sce->r.domeresbuf = 1.0f;
250 sce->r.simplify_subsurf= 6;
251 sce->r.simplify_particles= 1.0f;
252 sce->r.simplify_shadowsamples= 16;
253 sce->r.simplify_aosss= 1.0f;
255 sce->r.cineonblack= 95;
256 sce->r.cineonwhite= 685;
257 sce->r.cineongamma= 1.7f;
259 sce->toolsettings = MEM_callocN(sizeof(struct ToolSettings),"Tool Settings Struct");
260 sce->toolsettings->cornertype=1;
261 sce->toolsettings->degr = 90;
262 sce->toolsettings->step = 9;
263 sce->toolsettings->turn = 1;
264 sce->toolsettings->extr_offs = 1;
265 sce->toolsettings->doublimit = 0.001;
266 sce->toolsettings->segments = 32;
267 sce->toolsettings->rings = 32;
268 sce->toolsettings->vertices = 32;
269 sce->toolsettings->editbutflag = 1;
270 sce->toolsettings->uvcalc_radius = 1.0f;
271 sce->toolsettings->uvcalc_cubesize = 1.0f;
272 sce->toolsettings->uvcalc_mapdir = 1;
273 sce->toolsettings->uvcalc_mapalign = 1;
274 sce->toolsettings->unwrapper = 1;
275 sce->toolsettings->select_thresh= 0.01f;
276 sce->toolsettings->jointrilimit = 0.8f;
278 sce->toolsettings->selectmode= SCE_SELECT_VERTEX;
279 sce->toolsettings->normalsize= 0.1;
280 sce->toolsettings->autokey_mode= U.autokey_mode;
282 sce->toolsettings->skgen_resolution = 100;
283 sce->toolsettings->skgen_threshold_internal = 0.01f;
284 sce->toolsettings->skgen_threshold_external = 0.01f;
285 sce->toolsettings->skgen_angle_limit = 45.0f;
286 sce->toolsettings->skgen_length_ratio = 1.3f;
287 sce->toolsettings->skgen_length_limit = 1.5f;
288 sce->toolsettings->skgen_correlation_limit = 0.98f;
289 sce->toolsettings->skgen_symmetry_limit = 0.1f;
290 sce->toolsettings->skgen_postpro = SKGEN_SMOOTH;
291 sce->toolsettings->skgen_postpro_passes = 1;
292 sce->toolsettings->skgen_options = SKGEN_FILTER_INTERNAL|SKGEN_FILTER_EXTERNAL|SKGEN_FILTER_SMART|SKGEN_HARMONIC|SKGEN_SUB_CORRELATION|SKGEN_STICK_TO_EMBEDDING;
293 sce->toolsettings->skgen_subdivisions[0] = SKGEN_SUB_CORRELATION;
294 sce->toolsettings->skgen_subdivisions[1] = SKGEN_SUB_LENGTH;
295 sce->toolsettings->skgen_subdivisions[2] = SKGEN_SUB_ANGLE;
297 sce->toolsettings->proportional_size = 1.0f;
299 pset= &sce->toolsettings->particle;
300 pset->flag= PE_KEEP_LENGTHS|PE_LOCK_FIRST|PE_DEFLECT_EMITTER;
301 pset->emitterdist= 0.25f;
304 pset->brushtype= PE_BRUSH_NONE;
305 for(a=0; a<PE_TOT_BRUSH; a++) {
306 pset->brush[a].strength= 50;
307 pset->brush[a].size= 50;
308 pset->brush[a].step= 10;
310 pset->brush[PE_BRUSH_CUT].strength= 100;
313 sce->audio.mixrate = 44100;
315 strcpy(sce->r.backbuf, "//backbuf");
316 strcpy(sce->r.pic, U.renderdir);
318 BLI_init_rctf(&sce->r.safety, 0.1f, 0.9f, 0.1f, 0.9f);
321 /* note; in header_info.c the scene copy happens..., if you add more to renderdata it has to be checked there */
322 scene_add_render_layer(sce);
327 Base *object_in_scene(Object *ob, Scene *sce)
331 base= sce->base.first;
333 if(base->object == ob) return base;
339 void set_scene_bg(Scene *scene)
348 /* check for cyclic sets, for reading old files but also for definite security (py?) */
349 scene_check_setscene(scene);
351 /* deselect objects (for dataselect) */
352 for(ob= G.main->object.first; ob; ob= ob->id.next)
353 ob->flag &= ~(SELECT|OB_FROMGROUP);
355 /* group flags again */
356 for(group= G.main->group.first; group; group= group->id.next) {
357 go= group->gobject.first;
359 if(go->ob) go->ob->flag |= OB_FROMGROUP;
365 DAG_scene_sort(scene);
367 /* ensure dags are built for sets */
368 for(sce= scene->set; sce; sce= sce->set)
369 if(sce->theDag==NULL)
372 /* copy layers and flags from bases to objects */
373 for(base= scene->base.first; base; base= base->next) {
378 base->flag &= ~(OB_FROMGROUP);
379 flag= ob->flag & (OB_FROMGROUP);
382 /* not too nice... for recovering objects with lost data */
383 if(ob->pose==NULL) base->flag &= ~OB_POSEMODE;
384 ob->flag= base->flag;
386 ob->ctime= -1234567.0; /* force ipo to be calculated later */
388 /* no full animation update, this to enable render code to work (render code calls own animation updates) */
390 /* do we need FRAMECHANGED in set_scene? */
391 // if (G.f & G_DOSCRIPTLINKS) BPY_do_all_scripts(SCRIPT_FRAMECHANGED, 0);
394 /* called from creator.c */
395 void set_scene_name(char *name)
399 for (sce= G.main->scene.first; sce; sce= sce->id.next) {
400 if (BLI_streq(name, sce->id.name+2)) {
406 //XXX error("Can't find scene: %s", name);
410 * doesnt return the original duplicated object, only dupli's
412 int next_object(Scene *scene, int val, Base **base, Object **ob)
414 static ListBase *duplilist= NULL;
415 static DupliObject *dupob;
416 static int fase= F_START, in_next_object= 0;
424 /* XXX particle systems with metas+dupligroups call this recursively */
427 printf("ERROR: MetaBall generation called recursively, not supported\n");
435 /* run_again is set when a duplilist has been ended */
441 *base= scene->base.first;
443 *ob= (*base)->object;
447 /* exception: empty scene */
448 if(scene->set && scene->set->base.first) {
449 *base= scene->set->base.first;
450 *ob= (*base)->object;
456 if(*base && fase!=F_DUPLI) {
457 *base= (*base)->next;
458 if(*base) *ob= (*base)->object;
461 /* scene is finished, now do the set */
462 if(scene->set && scene->set->base.first) {
463 *base= scene->set->base.first;
464 *ob= (*base)->object;
472 if(*base == NULL) fase= F_START;
475 if( (*base)->object->transflag & OB_DUPLI) {
476 /* groups cannot be duplicated for mballs yet,
477 this enters eternal loop because of
478 makeDispListMBall getting called inside of group_duplilist */
479 if((*base)->object->dup_group == NULL) {
480 duplilist= object_duplilist(scene, (*base)->object);
482 dupob= duplilist->first;
485 free_object_duplilist(duplilist);
492 Mat4CpyMat4(dupob->ob->obmat, dupob->mat);
494 (*base)->flag |= OB_FROMDUPLI;
500 else if(fase==F_DUPLI) {
502 (*base)->flag &= ~OB_FROMDUPLI;
504 for(dupob= duplilist->first; dupob; dupob= dupob->next) {
505 Mat4CpyMat4(dupob->ob->obmat, dupob->omat);
508 free_object_duplilist(duplilist);
516 /* reset recursion test */
522 Object *scene_find_camera(Scene *sc)
526 for (base= sc->base.first; base; base= base->next)
527 if (base->object->type==OB_CAMERA)
534 Base *scene_add_base(Scene *sce, Object *ob)
536 Base *b= MEM_callocN(sizeof(*b), "scene_add_base");
537 BLI_addhead(&sce->base, b);
546 void scene_deselect_all(Scene *sce)
550 for (b= sce->base.first; b; b= b->next) {
552 b->object->flag= b->flag;
556 void scene_select_base(Scene *sce, Base *selbase)
558 scene_deselect_all(sce);
560 selbase->flag |= SELECT;
561 selbase->object->flag= selbase->flag;
563 sce->basact= selbase;
566 /* checks for cycle, returns 1 if it's all OK */
567 int scene_check_setscene(Scene *sce)
572 if(sce->set==NULL) return 1;
575 for(scene= G.main->scene.first; scene; scene= scene->id.next)
578 for(a=0, scene=sce; scene->set; scene=scene->set, a++) {
579 /* more iterations than scenes means we have a cycle */
581 /* the tested scene gets zero'ed, that's typically current scene */
590 /* This (evil) function is needed to cope with two legacy Blender rendering features
591 * mblur (motion blur that renders 'subframes' and blurs them together), and fields
592 * rendering. Thus, the use of ugly globals from object.c
594 // BAD... EVIL... JUJU...!!!!
595 // XXX moved here temporarily
596 float frame_to_float (Scene *scene, int cfra) /* see also bsystem_time in object.c */
598 extern float bluroffs; /* bad stuff borrowed from object.c */
599 extern float fieldoffs;
603 ctime+= bluroffs+fieldoffs;
604 ctime*= scene->r.framelen;
609 static void scene_update(Scene *sce, unsigned int lay)
613 float ctime = frame_to_float(sce, sce->r.cfra);
615 if(sce->theDag==NULL)
618 DAG_scene_update_flags(sce, lay); // only stuff that moves or needs display still
620 /* All 'standard' (i.e. without any dependencies) animation is handled here,
621 * with an 'local' to 'macro' order of evaluation. This should ensure that
622 * settings stored nestled within a hierarchy (i.e. settings in a Texture block
623 * can be overridden by settings from Scene, which owns the Texture through a hierarchy
624 * such as Scene->World->MTex/Texture) can still get correctly overridden.
626 BKE_animsys_evaluate_all_animation(G.main, ctime);
628 for(base= sce->base.first; base; base= base->next) {
631 object_handle_update(sce, ob); // bke_object.h
633 /* only update layer when an ipo */
634 // XXX old animation system
635 //if(ob->ipo && has_ipo_code(ob->ipo, OB_LAY) ) {
636 // base->lay= ob->lay;
642 /* applies changes right away, does all sets too */
643 void scene_update_for_newframe(Scene *sce, unsigned int lay)
647 /* clear animation overrides */
650 #ifndef DISABLE_PYTHON
651 if (G.f & G_DOSCRIPTLINKS) BPY_do_all_scripts(SCRIPT_FRAMECHANGED, 0);
653 /* sets first, we allow per definition current scene to have dependencies on sets */
654 for(sce= sce->set; sce; sce= sce->set)
655 scene_update(sce, lay);
657 scene_update(scene, lay);
660 /* return default layer, also used to patch old files */
661 void scene_add_render_layer(Scene *sce)
663 SceneRenderLayer *srl;
664 int tot= 1 + BLI_countlist(&sce->r.layers);
666 srl= MEM_callocN(sizeof(SceneRenderLayer), "new render layer");
667 sprintf(srl->name, "%d RenderLayer", tot);
668 BLI_addtail(&sce->r.layers, srl);
670 /* note, this is also in render, pipeline.c, to make layer when scenedata doesnt have it */
671 srl->lay= (1<<20) -1;
672 srl->layflag= 0x7FFF; /* solid ztra halo edge strand */
673 srl->passflag= SCE_PASS_COMBINED|SCE_PASS_Z;
676 void sculptsession_free(Sculpt *sculpt)
678 SculptSession *ss= sculpt->session;
681 MEM_freeN(ss->projverts);
687 MEM_freeN(ss->fmap_mem);
690 MEM_freeN(ss->texcache);
692 sculpt->session= NULL;
696 /* render simplification */
698 int get_render_subsurf_level(RenderData *r, int lvl)
700 if(G.rt == 1 && (r->mode & R_SIMPLIFY))
701 return MIN2(r->simplify_subsurf, lvl);
706 int get_render_child_particle_number(RenderData *r, int num)
708 if(G.rt == 1 && (r->mode & R_SIMPLIFY))
709 return (int)(r->simplify_particles*num);
714 int get_render_shadow_samples(RenderData *r, int samples)
716 if(G.rt == 1 && (r->mode & R_SIMPLIFY) && samples > 0)
717 return MIN2(r->simplify_shadowsamples, samples);
722 float get_render_aosss_error(RenderData *r, float error)
724 if(G.rt == 1 && (r->mode & R_SIMPLIFY))
725 return ((1.0f-r->simplify_aosss)*10.0f + 1.0f)*error;
730 void free_dome_warp_text(struct Text *txt)
734 scene = G.main->scene.first;
736 if (scene->r.dometext == txt)
737 scene->r.dometext = NULL;
738 scene = scene->id.next;