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_armature_types.h"
47 #include "DNA_color_types.h"
48 #include "DNA_constraint_types.h"
49 #include "DNA_curve_types.h"
50 #include "DNA_group_types.h"
51 #include "DNA_lamp_types.h"
52 #include "DNA_material_types.h"
53 #include "DNA_meta_types.h"
54 #include "DNA_object_types.h"
55 #include "DNA_scene_types.h"
56 #include "DNA_scriptlink_types.h"
57 #include "DNA_texture_types.h"
58 #include "DNA_userdef_types.h"
60 #include "BKE_action.h"
62 #include "BKE_armature.h"
63 #include "BKE_bad_level_calls.h"
64 #include "BKE_colortools.h"
65 #include "BKE_constraint.h"
66 #include "BKE_depsgraph.h"
67 #include "BKE_global.h"
68 #include "BKE_group.h"
70 #include "BKE_idprop.h"
71 #include "BKE_image.h"
73 #include "BKE_library.h"
76 #include "BKE_object.h"
77 #include "BKE_scene.h"
78 #include "BKE_sculpt.h"
79 #include "BKE_world.h"
80 #include "BKE_utildefines.h"
82 #include "BIF_previewrender.h"
83 #include "BIF_editseq.h"
85 #ifndef DISABLE_PYTHON
86 #include "BPY_extern.h"
89 #include "BLI_arithb.h"
90 #include "BLI_blenlib.h"
99 void free_avicodecdata(AviCodecData *acd)
103 MEM_freeN(acd->lpFormat);
104 acd->lpFormat = NULL;
108 MEM_freeN(acd->lpParms);
115 void free_qtcodecdata(QuicktimeCodecData *qcd)
119 MEM_freeN(qcd->cdParms);
126 /* copy_scene moved to src/header_info.c... should be back */
128 /* do not free scene itself */
129 void free_scene(Scene *sce)
133 base= sce->base.first;
135 base->object->id.us--;
138 /* do not free objects! */
140 BLI_freelistN(&sce->base);
141 free_editing(sce->ed);
142 if(sce->radio) MEM_freeN(sce->radio);
145 #ifndef DISABLE_PYTHON
146 BPY_free_scriptlink(&sce->scriptlink);
149 if (sce->r.avicodecdata) {
150 free_avicodecdata(sce->r.avicodecdata);
151 MEM_freeN(sce->r.avicodecdata);
152 sce->r.avicodecdata = NULL;
154 if (sce->r.qtcodecdata) {
155 free_qtcodecdata(sce->r.qtcodecdata);
156 MEM_freeN(sce->r.qtcodecdata);
157 sce->r.qtcodecdata = NULL;
159 if (sce->r.ffcodecdata.properties) {
160 IDP_FreeProperty(sce->r.ffcodecdata.properties);
161 MEM_freeN(sce->r.ffcodecdata.properties);
162 sce->r.ffcodecdata.properties = NULL;
165 BLI_freelistN(&sce->markers);
166 BLI_freelistN(&sce->transform_spaces);
167 BLI_freelistN(&sce->r.layers);
169 if(sce->toolsettings){
170 MEM_freeN(sce->toolsettings);
171 sce->toolsettings = NULL;
175 free_forest(sce->theDag);
176 MEM_freeN(sce->theDag);
180 ntreeFreeTree(sce->nodetree);
181 MEM_freeN(sce->nodetree);
184 sculptdata_free(sce);
187 Scene *add_scene(char *name)
190 ParticleEditSettings *pset;
193 sce= alloc_libblock(&G.main->scene, ID_SCE, name);
195 sce->selectmode= SCE_SELECT_VERTEX;
196 sce->editbutsize= 0.1;
197 sce->autokey_mode= U.autokey_mode;
199 sce->r.mode= R_GAMMA;
212 sce->r.framapto= 100;
214 sce->r.framelen= 1.0;
216 sce->r.frs_sec_base= 1;
219 sce->r.bake_mode= 1; /* prevent to include render stuff here */
220 sce->r.bake_filter= 2;
222 sce->r.bake_flag= R_BAKE_CLEAR;
223 sce->r.bake_normal_space= R_BAKE_SPACE_TANGENT;
232 sce->r.stereomode = 1; // no stereo
233 sce->r.domeangle = 180;
235 sce->r.domesize = 1.0f;
237 sce->r.domeresbuf = 1.0f;
239 sce->r.simplify_subsurf= 6;
240 sce->r.simplify_particles= 1.0f;
241 sce->r.simplify_shadowsamples= 16;
242 sce->r.simplify_aosss= 1.0f;
244 sce->r.cineonblack= 95;
245 sce->r.cineonwhite= 685;
246 sce->r.cineongamma= 1.7f;
248 sce->toolsettings = MEM_callocN(sizeof(struct ToolSettings),"Tool Settings Struct");
249 sce->toolsettings->cornertype=1;
250 sce->toolsettings->degr = 90;
251 sce->toolsettings->step = 9;
252 sce->toolsettings->turn = 1;
253 sce->toolsettings->extr_offs = 1;
254 sce->toolsettings->doublimit = 0.001;
255 sce->toolsettings->segments = 32;
256 sce->toolsettings->rings = 32;
257 sce->toolsettings->vertices = 32;
258 sce->toolsettings->editbutflag = 1;
259 sce->toolsettings->uvcalc_radius = 1.0f;
260 sce->toolsettings->uvcalc_cubesize = 1.0f;
261 sce->toolsettings->uvcalc_mapdir = 1;
262 sce->toolsettings->uvcalc_mapalign = 1;
263 sce->toolsettings->unwrapper = 1;
264 sce->toolsettings->select_thresh= 0.01f;
265 sce->toolsettings->jointrilimit = 0.8f;
267 sce->toolsettings->skgen_resolution = 100;
268 sce->toolsettings->skgen_threshold_internal = 0.01f;
269 sce->toolsettings->skgen_threshold_external = 0.01f;
270 sce->toolsettings->skgen_angle_limit = 45.0f;
271 sce->toolsettings->skgen_length_ratio = 1.3f;
272 sce->toolsettings->skgen_length_limit = 1.5f;
273 sce->toolsettings->skgen_correlation_limit = 0.98f;
274 sce->toolsettings->skgen_symmetry_limit = 0.1f;
275 sce->toolsettings->skgen_postpro = SKGEN_SMOOTH;
276 sce->toolsettings->skgen_postpro_passes = 1;
277 sce->toolsettings->skgen_options = SKGEN_FILTER_INTERNAL|SKGEN_FILTER_EXTERNAL|SKGEN_FILTER_SMART|SKGEN_HARMONIC|SKGEN_SUB_CORRELATION|SKGEN_STICK_TO_EMBEDDING;
278 sce->toolsettings->skgen_subdivisions[0] = SKGEN_SUB_CORRELATION;
279 sce->toolsettings->skgen_subdivisions[1] = SKGEN_SUB_LENGTH;
280 sce->toolsettings->skgen_subdivisions[2] = SKGEN_SUB_ANGLE;
282 pset= &sce->toolsettings->particle;
283 pset->flag= PE_KEEP_LENGTHS|PE_LOCK_FIRST|PE_DEFLECT_EMITTER;
284 pset->emitterdist= 0.25f;
287 pset->brushtype= PE_BRUSH_NONE;
288 for(a=0; a<PE_TOT_BRUSH; a++) {
289 pset->brush[a].strength= 50;
290 pset->brush[a].size= 50;
291 pset->brush[a].step= 10;
293 pset->brush[PE_BRUSH_CUT].strength= 100;
296 sce->audio.mixrate = 44100;
298 strcpy(sce->r.backbuf, "//backbuf");
299 strcpy(sce->r.pic, U.renderdir);
301 BLI_init_rctf(&sce->r.safety, 0.1f, 0.9f, 0.1f, 0.9f);
304 sculptdata_init(sce);
306 /* note; in header_info.c the scene copy happens..., if you add more to renderdata it has to be checked there */
307 scene_add_render_layer(sce);
312 Base *object_in_scene(Object *ob, Scene *sce)
316 base= sce->base.first;
318 if(base->object == ob) return base;
324 void set_scene_bg(Scene *sce)
332 // Note: this here is defined in editseq.c (BIF_editseq.h), NOT in blenkernel!
337 /* check for cyclic sets, for reading old files but also for definite security (py?) */
338 scene_check_setscene(G.scene);
340 /* deselect objects (for dataselect) */
341 for(ob= G.main->object.first; ob; ob= ob->id.next)
342 ob->flag &= ~(SELECT|OB_FROMGROUP);
344 /* group flags again */
345 for(group= G.main->group.first; group; group= group->id.next) {
346 go= group->gobject.first;
348 if(go->ob) go->ob->flag |= OB_FROMGROUP;
356 /* ensure dags are built for sets */
357 for(sce= sce->set; sce; sce= sce->set)
358 if(sce->theDag==NULL)
361 /* copy layers and flags from bases to objects */
362 for(base= G.scene->base.first; base; base= base->next) {
367 base->flag &= ~(OB_FROMGROUP);
368 flag= ob->flag & (OB_FROMGROUP);
371 /* not too nice... for recovering objects with lost data */
372 if(ob->pose==NULL) base->flag &= ~OB_POSEMODE;
373 ob->flag= base->flag;
375 ob->ctime= -1234567.0; /* force ipo to be calculated later */
377 /* no full animation update, this to enable render code to work (render code calls own animation updates) */
379 /* do we need FRAMECHANGED in set_scene? */
380 // if (G.f & G_DOSCRIPTLINKS) BPY_do_all_scripts(SCRIPT_FRAMECHANGED, 0);
383 /* called from creator.c */
384 void set_scene_name(char *name)
388 for (sce= G.main->scene.first; sce; sce= sce->id.next) {
389 if (BLI_streq(name, sce->id.name+2)) {
395 error("Can't find scene: %s", name);
399 * doesnt return the original duplicated object, only dupli's
401 int next_object(int val, Base **base, Object **ob)
403 static ListBase *duplilist= NULL;
404 static DupliObject *dupob;
415 /* run_again is set when a duplilist has been ended */
421 *base= G.scene->base.first;
423 *ob= (*base)->object;
427 /* exception: empty scene */
428 if(G.scene->set && G.scene->set->base.first) {
429 *base= G.scene->set->base.first;
430 *ob= (*base)->object;
436 if(*base && fase!=F_DUPLI) {
437 *base= (*base)->next;
438 if(*base) *ob= (*base)->object;
441 /* scene is finished, now do the set */
442 if(G.scene->set && G.scene->set->base.first) {
443 *base= G.scene->set->base.first;
444 *ob= (*base)->object;
452 if(*base == NULL) fase= F_START;
455 if( (*base)->object->transflag & OB_DUPLI) {
456 /* groups cannot be duplicated for mballs yet,
457 this enters eternal loop because of
458 makeDispListMBall getting called inside of group_duplilist */
459 if((*base)->object->dup_group == NULL) {
460 duplilist= object_duplilist(G.scene, (*base)->object);
462 dupob= duplilist->first;
465 free_object_duplilist(duplilist);
472 Mat4CpyMat4(dupob->ob->obmat, dupob->mat);
474 (*base)->flag |= OB_FROMDUPLI;
480 else if(fase==F_DUPLI) {
482 (*base)->flag &= ~OB_FROMDUPLI;
484 for(dupob= duplilist->first; dupob; dupob= dupob->next) {
485 Mat4CpyMat4(dupob->ob->obmat, dupob->omat);
488 free_object_duplilist(duplilist);
499 Object *scene_find_camera(Scene *sc)
503 for (base= sc->base.first; base; base= base->next)
504 if (base->object->type==OB_CAMERA)
511 Base *scene_add_base(Scene *sce, Object *ob)
513 Base *b= MEM_callocN(sizeof(*b), "scene_add_base");
514 BLI_addhead(&sce->base, b);
523 void scene_deselect_all(Scene *sce)
527 for (b= sce->base.first; b; b= b->next) {
529 b->object->flag= b->flag;
533 void scene_select_base(Scene *sce, Base *selbase)
535 scene_deselect_all(sce);
537 selbase->flag |= SELECT;
538 selbase->object->flag= selbase->flag;
540 sce->basact= selbase;
543 /* checks for cycle, returns 1 if it's all OK */
544 int scene_check_setscene(Scene *sce)
549 if(sce->set==NULL) return 1;
552 for(scene= G.main->scene.first; scene; scene= scene->id.next)
555 for(a=0, scene=sce; scene->set; scene=scene->set, a++) {
556 /* more iterations than scenes means we have a cycle */
558 /* the tested scene gets zero'ed, that's typically current scene */
567 static void scene_update(Scene *sce, unsigned int lay)
572 if(sce->theDag==NULL)
575 DAG_scene_update_flags(sce, lay); // only stuff that moves or needs display still
577 for(base= sce->base.first; base; base= base->next) {
580 object_handle_update(ob); // bke_object.h
582 /* only update layer when an ipo */
583 if(ob->ipo && has_ipo_code(ob->ipo, OB_LAY) ) {
589 /* applies changes right away, does all sets too */
590 void scene_update_for_newframe(Scene *sce, unsigned int lay)
594 /* clears all BONE_UNKEYED flags for every pose's pchans */
595 framechange_poses_clear_unkeyed();
597 /* object ipos are calculated in where_is_object */
599 #ifndef DISABLE_PYTHON
600 if (G.f & G_DOSCRIPTLINKS) BPY_do_all_scripts(SCRIPT_FRAMECHANGED, 0);
602 /* sets first, we allow per definition current scene to have dependencies on sets */
603 for(sce= sce->set; sce; sce= sce->set)
604 scene_update(sce, lay);
606 scene_update(scene, lay);
609 /* return default layer, also used to patch old files */
610 void scene_add_render_layer(Scene *sce)
612 SceneRenderLayer *srl;
613 int tot= 1 + BLI_countlist(&sce->r.layers);
615 srl= MEM_callocN(sizeof(SceneRenderLayer), "new render layer");
616 sprintf(srl->name, "%d RenderLayer", tot);
617 BLI_addtail(&sce->r.layers, srl);
619 /* note, this is also in render, pipeline.c, to make layer when scenedata doesnt have it */
620 srl->lay= (1<<20) -1;
621 srl->layflag= 0x7FFF; /* solid ztra halo edge strand */
622 srl->passflag= SCE_PASS_COMBINED|SCE_PASS_Z;
625 /* Initialize 'permanent' sculpt data that is saved with file kept after
626 switching out of sculptmode. */
627 void sculptdata_init(Scene *sce)
634 sd= &sce->sculptdata;
637 curvemapping_free(sd->cumap);
641 memset(sd, 0, sizeof(SculptData));
643 sd->drawbrush.size = sd->smoothbrush.size = sd->pinchbrush.size =
644 sd->inflatebrush.size = sd->grabbrush.size =
645 sd->layerbrush.size = sd->flattenbrush.size = 50;
646 sd->drawbrush.strength = sd->smoothbrush.strength =
647 sd->pinchbrush.strength = sd->inflatebrush.strength =
648 sd->grabbrush.strength = sd->layerbrush.strength =
649 sd->flattenbrush.strength = 25;
650 sd->drawbrush.dir = sd->pinchbrush.dir = sd->inflatebrush.dir = sd->layerbrush.dir= 1;
651 sd->drawbrush.flag = sd->smoothbrush.flag =
652 sd->pinchbrush.flag = sd->inflatebrush.flag =
653 sd->layerbrush.flag = sd->flattenbrush.flag = 0;
654 sd->drawbrush.view= 0;
655 sd->brush_type= DRAW_BRUSH;
660 sd->texrept= SCULPTREPT_DRAG;
661 sd->flags= SCULPT_DRAW_BRUSH;
663 sd->tablet_strength=10;
665 sculpt_reset_curve(sd);
668 void sculptdata_free(Scene *sce)
670 SculptData *sd= &sce->sculptdata;
673 sculptsession_free(sce);
675 for(a=0; a<MAX_MTEX; a++) {
676 MTex *mtex= sd->mtex[a];
678 if(mtex->tex) mtex->tex->id.us--;
683 curvemapping_free(sd->cumap);
687 void sculpt_vertexusers_free(SculptSession *ss)
689 if(ss && ss->vertex_users){
690 MEM_freeN(ss->vertex_users);
691 MEM_freeN(ss->vertex_users_mem);
692 ss->vertex_users= NULL;
693 ss->vertex_users_mem= NULL;
694 ss->vertex_users_size= 0;
698 void sculptsession_free(Scene *sce)
700 SculptSession *ss= sce->sculptdata.session;
703 MEM_freeN(ss->projverts);
707 if(ss->radialcontrol)
708 MEM_freeN(ss->radialcontrol);
710 sculpt_vertexusers_free(ss);
712 MEM_freeN(ss->texcache);
714 sce->sculptdata.session= NULL;
718 /* Default curve approximates 0.5 * (cos(pi * x) + 1), with 0 <= x <= 1 */
719 void sculpt_reset_curve(SculptData *sd)
724 sd->cumap = curvemapping_add(1, 0, 0, 1, 1);
729 MEM_freeN(cm->curve);
730 cm->curve= MEM_callocN(6*sizeof(CurveMapPoint), "curve points");
731 cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
736 cm->curve[1].y= 0.97553;
738 cm->curve[2].y= 0.79389;
740 cm->curve[3].y= 0.02447;
742 cm->curve[4].y= 0.20611;
746 curvemapping_changed(sd->cumap, 0);
749 /* render simplification */
751 int get_render_subsurf_level(RenderData *r, int lvl)
753 if(G.rt == 1 && (r->mode & R_SIMPLIFY))
754 return MIN2(r->simplify_subsurf, lvl);
759 int get_render_child_particle_number(RenderData *r, int num)
761 if(G.rt == 1 && (r->mode & R_SIMPLIFY))
762 return (int)(r->simplify_particles*num);
767 int get_render_shadow_samples(RenderData *r, int samples)
769 if(G.rt == 1 && (r->mode & R_SIMPLIFY) && samples > 0)
770 return MIN2(r->simplify_shadowsamples, samples);
775 float get_render_aosss_error(RenderData *r, float error)
777 if(G.rt == 1 && (r->mode & R_SIMPLIFY))
778 return ((1.0f-r->simplify_aosss)*10.0f + 1.0f)*error;
783 void free_dome_warp_text(struct Text *txt)
787 scene = G.main->scene.first;
789 if (scene->r.dometext == txt)
790 scene->r.dometext = NULL;
791 scene = scene->id.next;