New scripts:
[blender.git] / source / creator / creator.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32 #include <stdlib.h>
33 #include <string.h>
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 /* This little block needed for linking to Blender... */
40
41 #include "MEM_guardedalloc.h"
42
43 #ifdef WIN32
44 #include "BLI_winstuff.h"
45 #endif
46
47 #include "GEN_messaging.h"
48
49 #include "DNA_ID.h"
50
51 #include "BLI_blenlib.h"
52
53 #include "BKE_utildefines.h"
54 #include "BKE_blender.h"
55 #include "BKE_font.h"
56 #include "BKE_global.h"
57 #include "BKE_main.h"
58 #include "BKE_packedFile.h"
59 #include "BKE_scene.h"
60
61 #include "BIF_gl.h"
62 #include "BIF_graphics.h"
63 #include "BIF_mainqueue.h"
64 #include "BIF_graphics.h"
65 #include "BIF_editsound.h"
66 #include "BIF_usiblender.h"
67 #include "BIF_drawscene.h"      /* set_scene() */
68 #include "BIF_screen.h"         /* waitcursor and more */
69 #include "BIF_usiblender.h"
70 #include "BIF_toolbox.h"
71
72 #include "BLO_writefile.h"
73 #include "BLO_readfile.h"
74
75 #include "BSE_headerbuttons.h" // for BIF_read_homefile
76
77 #include "BDR_drawmesh.h"
78
79 #include "IMB_imbuf.h"  // for quicktime_init
80
81 #include "RE_renderconverter.h"
82
83 #include "BPY_extern.h"
84
85 #include "playanim_ext.h"
86 #include "mydevice.h"
87 #include "render.h"
88 #include "nla.h"
89 #include "datatoc.h"
90
91 /* for passing information between creator and gameengine */
92 #include "SYS_System.h"
93
94 #include <signal.h>
95
96 #ifdef __FreeBSD__
97 # include <sys/types.h>
98 # include <floatingpoint.h>
99 # include <sys/rtprio.h>
100 #endif
101
102 // from buildinfo.c
103 extern char * build_date;
104 extern char * build_time;
105 extern char * build_platform;
106 extern char * build_type;
107
108 /*      Local Function prototypes */
109 static void print_help();
110 static void print_version();
111
112 /* for the callbacks: */
113
114 extern int pluginapi_force_ref(void);  /* from blenpluginapi:pluginapi.c */
115
116 char bprogname[FILE_MAXDIR+FILE_MAXFILE]; /* from blenpluginapi:pluginapi.c */
117
118 /* Initialise callbacks for the modules that need them */
119 void setCallbacks(void); 
120
121 static void fpe_handler(int sig)
122 {
123         // printf("SIGFPE trapped\n");
124 }
125
126 /* handling ctrl-c event in console */
127 static void blender_esc(int sig)
128 {
129         static int count = 0;
130         
131         G.afbreek = 1;  /* forces render loop to read queue, not sure if its needed */
132         
133         if (sig == 2) {
134                 if (count) {
135                         printf("\nBlender killed\n");
136                         exit(2);
137                 }
138                 printf("\nSent an internal break event. Press ^C again to kill Blender\n");
139                 count++;
140         }
141 }
142
143 static void print_version(void)
144 {
145         printf ("Blender %d.%02d\n", G.version/100, G.version%100);
146 }
147
148 static void print_help(void)
149 {
150         printf ("Blender V %d.%02d\n", G.version/100, G.version%100);
151         printf ("Usage: blender [options ...] [file]\n");
152                                 
153         printf ("\nRender options:\n");
154         printf ("  -b <file>\tRender <file> in background\n");
155         printf ("    -S <name>\tSet scene <name>\n");
156         printf ("    -f <frame>\tRender frame <frame> and save it\n");                          
157         printf ("    -s <frame>\tSet start to frame <frame> (use with -a)\n");
158         printf ("    -e <frame>\tSet end to frame (use with -a)<frame>\n");
159         printf ("    -a\t\tRender animation\n");
160                                 
161         printf ("\nAnimation options:\n");
162         printf ("  -a <file(s)>\tPlayback <file(s)>\n");
163         printf ("    -p <sx> <sy>\tOpen with lower left corner at <sx>, <sy>\n");
164         printf ("    -m\t\tRead from disk (Don't buffer)\n");
165                                 
166         printf ("\nWindow options:\n");
167         printf ("  -w\t\tForce opening with borders\n");
168 #ifdef WIN32
169         printf ("  -W\t\tForce opening without borders\n");
170 #endif
171         printf ("  -p <sx> <sy> <w> <h>\tOpen with lower left corner at <sx>, <sy>\n");
172         printf ("                      \tand width and height <w>, <h>\n");
173         printf ("\nGame Engine specific options:\n");
174         printf ("  -g fixedtime\t\tRun on 50 hertz without dropping frames\n");
175         printf ("  -g vertexarrays\tUse Vertex Arrays for rendering (usually faster)\n");
176         printf ("  -g noaudio\t\tNo audio in Game Engine\n");
177         printf ("  -g nomipmap\t\tNo Texture Mipmapping\n");
178         printf ("  -g linearmipmap\tLinear Texture Mipmapping instead of Nearest (default)\n");
179
180         printf ("\nMisc options:\n");
181         printf ("  -d\t\tTurn debugging on\n");
182         printf ("  -noaudio\tDisable audio on systems that support audio\n");
183         printf ("  -h\t\tPrint this help text\n");
184         printf ("  -y\t\tDisable OnLoad scene scripts, use -Y to find out why its -y\n");
185         printf ("  -P <filename>\tRun the given Python script (filename or Blender Text)\n");
186 #ifdef WIN32
187         printf ("  -R\t\tRegister .blend extension\n");
188 #endif
189         printf ("  -v\t\tPrint Blender version and exit\n");
190 }
191
192
193 double PIL_check_seconds_timer(void);
194 extern void winlay_get_screensize(int *width_r, int *height_r);
195 int main(int argc, char **argv)
196 {
197         int a, i, stax, stay, sizx, sizy;
198         SYS_SystemHandle syshandle;
199         Scene *sce;
200
201 #if defined(WIN32) || defined (__linux__)
202         int audio = 1;
203 #else
204         int audio = 0;
205 #endif
206         setCallbacks();
207
208 #ifdef __APPLE__
209                 /* patch to ignore argument finder gives us (pid?) */
210         if (argc==2 && strncmp(argv[1], "-psn_", 5)==0) {
211                 extern int GHOST_HACK_getFirstFile(char buf[]);
212                 static char firstfilebuf[512];
213                 int scr_x,scr_y;
214
215                 argc= 1;
216
217                 /* 40 + 684 + (headers) 22 + 22 = 768, the powerbook screen height */
218                 setprefsize(120, 40, 850, 684);
219
220                 winlay_get_screensize(&scr_x, &scr_y);
221                 winlay_process_events(0);
222                 if (GHOST_HACK_getFirstFile(firstfilebuf)) {
223                         argc= 2;
224                         argv[1]= firstfilebuf;
225                 }
226         }
227
228 #endif
229
230 #ifdef __FreeBSD__
231         fpsetmask(0);
232 #endif
233 #ifdef __linux__
234     #ifdef __alpha__
235         signal (SIGFPE, fpe_handler);
236     #endif
237 #endif
238 #if defined(__sgi)
239         signal (SIGFPE, fpe_handler);
240 #endif
241
242         // copy path to executable in bprogname. playanim and creting runtimes
243         // need this.
244
245         BLI_where_am_i(bprogname, argv[0]);
246         
247                 /* Hack - force inclusion of the plugin api functions,
248                  * see blenpluginapi:pluginapi.c
249                  */
250         pluginapi_force_ref();
251         
252         initglobals();  /* blender.c */
253
254         syshandle = SYS_GetSystem();
255         GEN_init_messaging_system();
256
257         /* first test for background */
258         G.f |= G_SCENESCRIPT; /* scenescript always set! */
259         for(a=1; a<argc; a++) {
260
261                 /* Handle unix and windows style help requests */
262                 if ((!strcmp(argv[a], "--help")) || (!strcmp(argv[a], "/?"))){
263                         print_help();
264                         exit(0);
265                 }
266
267                 /* Handle long version request */
268                 if (!strcmp(argv[a], "--version")){
269                         print_version();
270                         exit(0);
271                 }
272
273                 /* Handle -* switches */
274                 else if(argv[a][0] == '-') {
275                         switch(argv[a][1]) {
276                         case 'a':
277                                 playanim(argc-1, argv+1);
278                                 exit(0);
279                                 break;
280                         case 'b':
281                         case 'B':
282                                 G.background = 1;
283                                 a= argc;
284                                 break;
285
286          case 'm':
287              /* unified render pipeline */
288 /*               G.magic = 1; has become obsolete */
289                          printf("-m: enable unified renderer has become obsolete. Set \n");
290                          printf("\tthis option per individual file now.\n");
291              break;
292
293                         case 'y':
294                                 G.f &= ~G_SCENESCRIPT;
295                                 break;
296
297                         case 'Y':
298                                 printf ("-y was used to disable scene scripts because,\n");
299                                 printf ("\t-p being taken, Ton was of the opinion that Y\n");
300                                 printf ("\tlooked like a split (disabled) snake, and also\n");
301                                 printf ("\twas similar to a python's tongue (unproven).\n\n");
302
303                                 printf ("\tZr agreed because it gave him a reason to add a\n");
304                                 printf ("\tcompletely useless text into Blender.\n\n");
305                                 
306                                 printf ("\tADDENDUM! Ton, in defense, found this picture of\n");
307                                 printf ("\tan Australian python, exhibiting her (his/its) forked\n");
308                                 printf ("\tY tongue. It could be part of an H Zr retorted!\n\n");
309                                 printf ("\thttp://www.users.bigpond.com/snake.man/\n");
310                                 
311                                 exit(252);
312                                 
313                         case 'h':                       
314                                 print_help();
315                                 exit(0);
316                         
317                         case 'v':
318                                 print_version();
319                                 exit(0);
320
321                         default:
322                                 break;
323                         }
324                 }
325         }
326
327 #ifdef __sgi
328         setuid(getuid()); /* end superuser */
329 #endif
330
331         /* for all platforms, even windos has it! */
332         if(G.background) signal(SIGINT, blender_esc);   /* ctrl c out bg render */
333
334                 /* background render uses this font too */
335         BKE_font_register_builtin(datatoc_Bfont, datatoc_Bfont_size);
336                 /* must be called here because R.winpos from default file */
337         RE_init_render_data();
338
339         if(G.background==0) {
340                 for(a=1; a<argc; a++) {
341                         if(argv[a][0] == '-') {
342                                 switch(argv[a][1]) {
343                                 case 'p':       /* prefsize */
344                                         if (argc-a < 5) {
345                                                 printf ("-p requires four arguments\n");
346                                                 exit(1);
347                                         }
348                                         a++;
349                                         stax= atoi(argv[a]);
350                                         a++;
351                                         stay= atoi(argv[a]);
352                                         a++;
353                                         sizx= atoi(argv[a]);
354                                         a++;
355                                         sizy= atoi(argv[a]);
356
357                                         setprefsize(stax, stay, sizx, sizy);
358                                         break;
359                                 case 'd':
360                                         G.f |= G_DEBUG;         /* std output printf's */ 
361                                         printf ("Blender V %d.%02d\n", G.version/100, G.version%100);
362 #ifdef NAN_BUILDINFO
363                                         printf("Build: %s %s %s %s\n", build_date, build_time, build_platform, build_type);
364
365 #endif // NAN_BUILDINFO
366                                         for (i = 0; i < argc; i++) {
367                                                 printf("argv[%d] = %s\n", i, argv[i]);
368                                         }
369                                         break;
370             
371                                 case 'w':
372                                         /* XXX, fixme zr, with borders */
373                                         /* there probably is a better way to do
374                                          * this, right now do as if blender was
375                                          * called with "-p 0 0 xres yres" -- sgefant
376                                          */ 
377                                         winlay_get_screensize(&sizx, &sizy);
378                                         setprefsize(0, 0, sizx, sizy);
379 #ifdef _WIN32   // FULLSCREEN
380                                         G.windowstate = G_WINDOWSTATE_BORDER;
381 #endif
382                                         break;
383                                 case 'W':
384                                                 /* XXX, fixme zr, borderless on win32 */
385 #ifdef _WIN32   // FULLSCREEN
386                                         G.windowstate = G_WINDOWSTATE_FULLSCREEN;
387 #endif
388                                         break;
389                                 case 'R':
390                                 /* Registering filetypes only makes sense on windows...      */
391 #ifdef WIN32
392                                         RegisterBlendExtension(argv[0]);
393 #endif
394                                         break;
395                                 case 'n':
396                                 case 'N':
397                                         if (strcasecmp(argv[a], "-noaudio") == 0|| strcasecmp(argv[a], "-nosound") == 0) {
398                                                 /**
399                                                         notify the gameengine that no audio is wanted, even if the user didn't give
400                                                         the flag -g noaudio.
401                                         */
402
403                                                 SYS_WriteCommandLineInt(syshandle,"noaudio",1);
404                                                 audio = 0;
405                                                 if (G.f & G_DEBUG) printf("setting audio to: %d\n", audio);
406                                         }
407                                         else if (strcasecmp(argv[a], "-nofrozen") == 0) {
408                                                 /* disable initialization of frozen python modules */
409                                                 if (G.f & G_DEBUG) printf("disable frozen modules\n");
410                                                 G.f |= G_NOFROZEN;
411                                         }
412                                         break;
413                                 }
414                         }
415                 }
416
417                 BPY_start_python();
418                 
419                 /**
420                  * NOTE: sound_init_audio() *must be* after start_python,
421                  * at least on FreeBSD.
422                  * added note (ton): i removed it altogether
423                  */
424
425                 BIF_init();
426
427         }
428         else {
429                 BPY_start_python();
430                 
431                 // (ton) Commented out. I have no idea whats thisfor... will mail around!
432                 // SYS_WriteCommandLineInt(syshandle,"noaudio",1);
433         // audio = 0;
434         // sound_init_audio();
435         // if (G.f & G_DEBUG) printf("setting audio to: %d\n", audio);
436         }
437
438         /**
439          * NOTE: the U.pythondir string is NULL until BIF_init() is executed,
440          * so we provide the BPY_ function below to append the user defined
441          * pythondir to Python's sys.path at this point.  Simply putting
442          * BIF_init() before BPY_start_python() crashes Blender at startup.
443          * Update: now this function also inits the bpymenus, which also depend
444          * on U.pythondir.
445          */
446         BPY_post_start_python();
447
448         RE_init_filt_mask();
449
450 #ifdef WITH_QUICKTIME
451
452         quicktime_init();
453
454 #endif /* WITH_QUICKTIME */
455
456         /* OK we are ready for it */
457
458         for(a=1; a<argc; a++) {
459                 if (G.afbreek==1) break;
460
461                 if(argv[a][0] == '-') {
462                         switch(argv[a][1]) {
463                         case 'p':       /* prefsize */
464                                 a+= 4;
465                                 break;
466
467                         case 'g':
468                                 {
469                                 /**
470                                 gameengine parameters are automaticly put into system
471                                 -g [paramname = value]
472                                 -g [boolparamname]
473                                 example:
474                                 -g novertexarrays
475                                 -g maxvertexarraysize = 512
476                                 */
477
478                                         if(++a < argc)
479                                         {
480                                                 char* paramname = argv[a];
481                                                 /* check for single value versus assignment */
482                                                 if (a+1 < argc && (*(argv[a+1]) == '='))
483                                                 {
484                                                         a++;
485                                                         if (a+1 < argc)
486                                                         {
487                                                                 a++;
488                                                                 /* assignment */
489                                                                 SYS_WriteCommandLineString(syshandle,paramname,argv[a]);
490                                                         }  else
491                                                         {
492                                                                 printf("error: argument assignment (%s) without value.\n",paramname);
493                                                         }
494                                                         /* name arg eaten */
495
496                                                 } else
497                                                 {
498
499                                                         SYS_WriteCommandLineInt(syshandle,argv[a],1);
500
501                                                         /* doMipMap */
502                                                         if (!strcmp(argv[a],"nomipmap"))
503                                                         {
504                                                                 set_mipmap(0); //doMipMap = 0;
505                                                         }
506                                                         /* linearMipMap */
507                                                         if (!strcmp(argv[a],"linearmipmap"))
508                                                         {
509                                                                 set_linear_mipmap(1); //linearMipMap = 1;
510                                                         }
511
512
513                                                 } /* if (*(argv[a+1]) == '=') */
514                                         } /*    if(++a < argc)  */
515                                         break;
516                                 }
517                         case 'f':
518                                 a++;
519                                 if (G.scene && a < argc) {
520                                         G.real_sfra = (G.scene->r.sfra);
521                                         G.real_efra = (G.scene->r.efra);
522                                         (G.scene->r.sfra) = atoi(argv[a]);
523                                         (G.scene->r.efra) = (G.scene->r.sfra);
524                                         RE_animrender(NULL);
525                                 }
526                                 break;
527                         case 'a':
528                                 if (G.scene) {
529                                         G.real_sfra = (G.scene->r.sfra);
530                                         G.real_efra = (G.scene->r.efra);
531                                         RE_animrender(NULL);
532                                 }
533                                 break;
534                         case 'S':
535                                 if(++a < argc) {
536                                         set_scene_name(argv[a]);
537                                 }
538                                 break;
539                         case 's':
540                                 a++;
541                                 if(G.scene) {
542                                         if (a < argc) (G.scene->r.sfra) = atoi(argv[a]);
543                                 }
544                                 break;
545                         case 'e':
546                                 a++;
547                                 if(G.scene) {
548                                         if (a < argc) (G.scene->r.efra) = atoi(argv[a]);
549                                 }
550                                 break;
551                         case 'P':
552                                 a++;
553                                 if (a < argc) BPY_run_python_script (argv[a]);
554                                 else printf("\nError: you must specify a Python script after '-P '.\n");
555                                 break;
556                         }
557                 }
558                 else {
559                         BKE_read_file(argv[a], NULL);
560                         sound_initialize_sounds();
561                 }
562         }
563
564         if(G.background)
565         {
566                 exit_usiblender();
567         }
568
569         setscreen(G.curscreen);
570
571         if(G.main->scene.first==0) {
572                 sce= add_scene("1");
573                 set_scene(sce);
574         }
575
576         screenmain();
577
578         return 0;
579 } /* end of int main(argc,argv) */
580
581 static void error_cb(char *err)
582 {
583         error("%s", err);
584 }
585
586 void setCallbacks(void)
587 {
588         /* Error output from the alloc routines: */
589         MEM_set_error_stream(stderr);
590
591
592         /* BLI_blenlib: */
593
594         BLI_setErrorCallBack(error_cb); /* */
595         BLI_setInterruptCallBack(blender_test_break);
596
597         /* render module: execution flow, timers, cursors and display. */
598         RE_set_getrenderdata_callback(RE_rotateBlenderScene);
599         RE_set_freerenderdata_callback(RE_freeRotateBlenderScene);
600 }