44de11db7bbc163720c32ad7647e8f967e55fbea
[blender.git] / source / blender / blenkernel / intern / scene.c
1
2 /*  scene.c
3  *  
4  * 
5  * $Id$
6  *
7  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version. The Blender
13  * Foundation also sells licenses for use in proprietary software under
14  * the Blender License.  See http://www.blender.org/BL/ for information
15  * about this.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software Foundation,
24  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  *
26  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
27  * All rights reserved.
28  *
29  * The Original Code is: all of this file.
30  *
31  * Contributor(s): none yet.
32  *
33  * ***** END GPL/BL DUAL LICENSE BLOCK *****
34  */
35
36 #include <stdio.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 "BLI_winstuff.h"
46 #include <io.h>
47 #endif
48 #include "MEM_guardedalloc.h"
49
50 #include "nla.h"                                /* for __NLA : IMPORTANT Do not delete me yet! */
51 #ifdef __NLA                                    /* for __NLA : IMPORTANT Do not delete me yet! */
52 #include "DNA_armature_types.h" /* for __NLA : IMPORTANT Do not delete me yet! */
53 #include "BKE_armature.h"               /* for __NLA : IMPORTANT Do not delete me yet! */
54 #include "BKE_action.h"                 /* for __NLA : IMPORTANT Do not delete me yet! */
55 #endif                                                  /* for __NLA : IMPORTANT Do not delete me yet! */
56
57 #include "DNA_constraint_types.h"
58 #include "DNA_scene_types.h"
59 #include "DNA_object_types.h"
60 #include "DNA_scriptlink_types.h"
61 #include "DNA_meta_types.h"
62 #include "DNA_ika_types.h"
63 #include "DNA_lamp_types.h"
64 #include "DNA_material_types.h"
65 #include "DNA_mesh_types.h"
66 #include "DNA_group_types.h"
67 #include "DNA_curve_types.h"
68 #include "DNA_userdef_types.h"
69
70 #include "BLI_blenlib.h"
71
72 #include "BKE_bad_level_calls.h"
73 #include "BKE_utildefines.h"
74
75 #include "BKE_global.h"
76 #include "BKE_main.h"
77 #include "BKE_anim.h"
78
79 #include "BKE_library.h"
80
81 #include "BPY_extern.h"
82 #include "BKE_scene.h"
83 #include "BKE_world.h"
84 #include "BKE_ipo.h"
85 #include "BKE_ika.h"
86 #include "BKE_key.h"
87
88 void free_avicodecdata(AviCodecData *acd)
89 {
90         if (acd) {
91                 if (acd->lpFormat){
92                         MEM_freeN(acd->lpFormat);
93                         acd->lpFormat = NULL;
94                         acd->cbFormat = 0;
95                 }
96                 if (acd->lpParms){
97                         MEM_freeN(acd->lpParms);
98                         acd->lpParms = NULL;
99                         acd->cbParms = 0;
100                 }
101         }
102 }
103
104 /* do not free scene itself */
105 void free_scene(Scene *sce)
106 {
107         Base *base;
108
109         base= sce->base.first;
110         while(base) {
111                 base->object->id.us--;
112                 base= base->next;
113         }
114         /* do not free objects! */
115
116         BLI_freelistN(&sce->base);
117         free_editing(sce->ed);
118         if(sce->radio) MEM_freeN(sce->radio);
119         if(sce->fcam) MEM_freeN(sce->fcam);
120         sce->radio= 0;
121         
122         BPY_free_scriptlink(&sce->scriptlink);
123         if (sce->r.avicodecdata) {
124                 free_avicodecdata(sce->r.avicodecdata);
125                 MEM_freeN(sce->r.avicodecdata);
126                 sce->r.avicodecdata = NULL;
127         }
128 }
129
130 Scene *add_scene(char *name)
131 {
132         Scene *sce;
133         
134         sce= alloc_libblock(&G.main->scene, ID_SCE, name);
135         sce->lay= 1;
136
137         sce->r.mode= R_GAMMA;
138         sce->r.cfra= 1;
139         sce->r.sfra= 1;
140         sce->r.efra= 250;
141         sce->r.xsch= 320;
142         sce->r.ysch= 256;
143         sce->r.xasp= 1;
144         sce->r.yasp= 1;
145         sce->r.xparts= 1;
146         sce->r.yparts= 1;
147         sce->r.size= 100;
148         sce->r.planes= 24;
149         sce->r.quality= 90;
150         sce->r.framapto= 100;
151         sce->r.images= 100;
152         sce->r.framelen= 1.0;
153         sce->r.frs_sec= 25;
154
155         sce->r.xplay= 640;
156         sce->r.yplay= 480;
157         sce->r.freqplay= 60;
158         sce->r.depth= 32;
159
160         if (sce->r.avicodecdata) {
161 printf("this is not good\n");
162         }
163 //      sce->r.imtype= R_TARGA;
164
165         sce->r.stereomode = 1;  // no stereo
166
167         strcpy(sce->r.backbuf, "//backbuf");
168         strcpy(sce->r.pic, U.renderdir);
169         strcpy(sce->r.ftype, "//ftype");
170         
171         BLI_init_rctf(&sce->r.safety, 0.1f, 0.9f, 0.1f, 0.9f);
172         sce->r.osa= 8;
173         
174         return sce;
175 }
176
177 int object_in_scene(Object *ob, Scene *sce)
178 {
179         Base *base;
180         
181         base= sce->base.first;
182         while(base) {
183                 if(base->object == ob) return 1;
184                 base= base->next;
185         }
186         return 0;
187 }
188
189 void sort_baselist(Scene *sce)
190 {
191         /* in order of parent and track */
192         ListBase tempbase, noparentbase, notyetbase;
193         Base *base, *test=NULL;
194         Object *par;
195         int doit, domore= 0, lastdomore=1;
196         
197         
198         /* keep same order when nothing has changed! */
199         
200         while(domore!=lastdomore) {
201
202                 lastdomore= domore;
203                 domore= 0;
204                 tempbase.first= tempbase.last= 0;
205                 noparentbase.first= noparentbase.last= 0;
206                 notyetbase.first= notyetbase.last= 0;
207                 
208                 while( (base= sce->base.first) ) {
209                         BLI_remlink(&sce->base, base);
210                         
211                         par= 0;
212                         if(base->object->type==OB_IKA) {
213                                 Ika *ika= base->object->data;
214                                 par= ika->parent;
215                         }
216
217                         if(par || base->object->parent || base->object->track) {
218                                 
219                                 doit= 0;
220                                 if(base->object->parent) doit++;
221                                 if(base->object->track) doit++;
222                                 
223                         /* Count constraints */
224                                 {
225                                         bConstraint *con;
226                                         for (con = base->object->constraints.first; con; con=con->next){
227                                                 switch (con->type){
228                                                 case CONSTRAINT_TYPE_KINEMATIC:
229                                                         {
230                                                                 bKinematicConstraint *data=con->data;
231                                                                 if (data->tar) doit++;
232                                                         }
233                                                         break;
234                                                 case CONSTRAINT_TYPE_TRACKTO:
235                                                         {
236                                                                 bTrackToConstraint *data=con->data;
237                                                                 if (data->tar) doit++;
238                                                         }
239                                                         break;
240                                                 case CONSTRAINT_TYPE_NULL:
241                                                         break;
242                                                 case CONSTRAINT_TYPE_ROTLIKE:
243                                                         {
244                                                                 bRotateLikeConstraint *data=con->data;
245                                                                 if (data->tar) doit++;
246                                                                 
247                                                         }
248                                                         break;
249                                                 case CONSTRAINT_TYPE_LOCLIKE:
250                                                         {
251                                                                 bLocateLikeConstraint *data=con->data;
252                                                                 if (data->tar) doit++;
253                                                         }
254                                                         break;
255                                                 case CONSTRAINT_TYPE_ACTION:
256                                                         {
257                                                                 bActionConstraint *data=con->data;
258                                                                 if (data->tar) doit++;
259                                                         }
260                                                         break;
261                                                 default:
262                                                         break;
263                                                 }
264                                         }
265                                 }
266                                 
267                                 if(par) doit++;
268                                 
269                                 test= tempbase.first;
270                                 while(test) {
271                                         
272                                         if(test->object==base->object->parent) doit--;
273                                         if(test->object==base->object->track) doit--;
274                                         if(test->object==par) doit--;
275                                         
276                                         /* Decrement constraints */
277                                         {
278                                                 bConstraint *con;
279                                                 for (con = base->object->constraints.first; con; con=con->next){
280                                                         switch (con->type){
281                                                         case CONSTRAINT_TYPE_KINEMATIC:
282                                                                 {
283                                                                         bKinematicConstraint *data=con->data;
284                                                                         if (test->object == data->tar && test->object!=base->object) doit--;
285                                                                 }
286                                                                 break;
287                                                         case CONSTRAINT_TYPE_TRACKTO:
288                                                                 {
289                                                                         bTrackToConstraint *data=con->data;
290                                                                         if (test->object == data->tar && test->object!=base->object) doit--;
291                                                                 }
292                                                                 break;
293                                                         case CONSTRAINT_TYPE_NULL:
294                                                                 break;
295                                                         case CONSTRAINT_TYPE_ROTLIKE:
296                                                                 {
297                                                                         bRotateLikeConstraint *data=con->data;
298                                                                         if (test->object == data->tar && test->object!=base->object) doit--;
299                                                                         
300                                                                 }
301                                                                 break;
302                                                         case CONSTRAINT_TYPE_LOCLIKE:
303                                                                 {
304                                                                         bLocateLikeConstraint *data=con->data;
305                                                                         if (test->object == data->tar && test->object!=base->object) doit--;
306                                                                 }
307                                                                 break;
308                                                         case CONSTRAINT_TYPE_ACTION:
309                                                                 {
310                                                                         bActionConstraint *data=con->data;
311                                                                         if (test->object == data->tar && test->object!=base->object) doit--;
312                                                                 }
313                                                                 break;
314                                                         default:
315                                                                 break;
316                                                         }
317                                                 }
318                                         }
319                                         
320                                         if(doit==0) break;
321                                         test= test->next;
322                                 }
323                                 
324                                 if(test) BLI_insertlink(&tempbase, test, base);
325                                 else {
326                                         BLI_addhead(&tempbase, base);
327                                         domore++;
328                                 }
329                                 
330                         }
331                         else BLI_addtail(&noparentbase, base);
332                         
333                 }
334                 sce->base= noparentbase;
335                 addlisttolist(&sce->base, &tempbase);
336                 addlisttolist(&sce->base, &notyetbase);
337
338         }
339 }
340
341
342 void set_scene_bg(Scene *sce)
343 {
344         Base *base;
345         Object *ob;
346         Group *group;
347         GroupObject *go;
348         int flag;
349         
350         G.scene= sce;
351         
352         /* deselect objects (for dataselect) */
353         ob= G.main->object.first;
354         while(ob) {
355                 ob->flag &= ~(SELECT|OB_FROMGROUP);
356                 ob= ob->id.next;
357         }
358
359         /* group flags again */
360         group= G.main->group.first;
361         while(group) {
362                 go= group->gobject.first;
363                 while(go) {
364                         if(go->ob) go->ob->flag |= OB_FROMGROUP;
365                         go= go->next;
366                 }
367                 group= group->id.next;
368         }
369
370         /* sort baselist */
371         sort_baselist(sce);
372
373         /* copy layers and flags from bases to objects */
374         base= G.scene->base.first;
375         while(base) {
376                 
377                 base->object->lay= base->lay;
378                 
379                 base->flag &= ~OB_FROMGROUP;
380                 flag= base->object->flag & OB_FROMGROUP;
381                 base->flag |= flag;
382                 
383                 base->object->ctime= -1234567.0;        /* force ipo to be calculated later */
384                 base= base->next;
385         }
386
387         do_all_ipos();  /* layers/materials */
388
389         BPY_do_all_scripts(SCRIPT_FRAMECHANGED);
390         do_all_keys();
391 #ifdef __NLA
392         do_all_actions();
393 #endif
394         do_all_ikas();
395
396
397 }
398
399 void set_scene_name(char *name)
400 {
401         Scene *sce;
402
403         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
404                 if (BLI_streq(name, sce->id.name+2)) {
405                         set_scene_bg(sce);
406                         return;
407                 }
408         }
409         
410         error("Can't find scene: %s", name);
411 }
412
413 /* used by metaballs
414  * doesnt return the original duplicated object, only dupli's
415  */
416 int next_object(int val, Base **base, Object **ob)
417 {
418         extern ListBase duplilist;
419         static Object *dupob;
420         static int fase;
421         int run_again=1;
422         
423         /* init */
424         if(val==0) {
425                 fase= F_START;
426                 dupob= 0;
427         }
428         else {
429                 
430                 /* run_again is set when a duplilist has been ended */
431                 while(run_again) {
432                         run_again= 0;
433                         
434                                 
435                                 
436                         /* the first base */
437                         if(fase==F_START) {
438                                 *base= G.scene->base.first;
439                                 if(*base) {
440                                         *ob= (*base)->object;
441                                         fase= F_SCENE;
442                                 }
443                                 else {
444                                     /* exception: empty scene */
445                                         if(G.scene->set && G.scene->set->base.first) {
446                                                 *base= G.scene->set->base.first;
447                                                 *ob= (*base)->object;
448                                                 fase= F_SET;
449                                         }
450                                 }
451                         }
452                         else {
453                                 if(*base && fase!=F_DUPLI) {
454                                         *base= (*base)->next;
455                                         if(*base) *ob= (*base)->object;
456                                         else {
457                                                 if(fase==F_SCENE) {
458                                                         /* scene is finished, now do the set */
459                                                         if(G.scene->set && G.scene->set->base.first) {
460                                                                 *base= G.scene->set->base.first;
461                                                                 *ob= (*base)->object;
462                                                                 fase= F_SET;
463                                                         }
464                                                 }
465                                         }
466                                 }
467                         }
468                         
469                         if(*base == 0) fase= F_START;
470                         else {
471                                 if(fase!=F_DUPLI) {
472                                         if( (*base)->object->transflag & OB_DUPLI) {
473                                                 
474                                                 make_duplilist(G.scene, (*base)->object);
475                                                 dupob= duplilist.first;
476                                                 
477                                         }
478                                 }
479                                 /* handle dupli's */
480                                 if(dupob) {
481                                         
482                                         *ob= dupob;
483                                         fase= F_DUPLI;
484                                         
485                                         dupob= dupob->id.next;
486                                 }
487                                 else if(fase==F_DUPLI) {
488                                         fase= F_SCENE;
489                                         free_duplilist();
490                                         run_again= 1;
491                                 }
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 }