Merge 16964:17122
[blender.git] / source / creator / creator.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL 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.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29 #include <stdlib.h>
30 #include <string.h>
31
32 /* This little block needed for linking to Blender... */
33
34 #include "MEM_guardedalloc.h"
35
36 #ifdef WIN32
37 #include "BLI_winstuff.h"
38 #endif
39
40 #include "GEN_messaging.h"
41
42 #include "DNA_ID.h"
43 #include "DNA_scene_types.h"
44
45 #include "BLI_blenlib.h"
46 #include "blendef.h" /* for MAXFRAME */
47
48
49 #include "BKE_utildefines.h"
50 #include "BKE_blender.h"
51 #include "BKE_font.h"
52 #include "BKE_global.h"
53 #include "BKE_main.h"
54 #include "BKE_material.h"
55 #include "BKE_packedFile.h"
56 #include "BKE_scene.h"
57 #include "BKE_node.h"
58
59 #include "BIF_gl.h"
60 #include "BIF_graphics.h"
61 #include "BIF_mainqueue.h"
62 #include "BIF_graphics.h"
63 #include "BIF_editsound.h"
64 #include "BIF_usiblender.h"
65 #include "BIF_drawscene.h"      /* set_scene() */
66 #include "BIF_screen.h"         /* waitcursor and more */
67 #include "BIF_usiblender.h"
68 #include "BIF_toolbox.h"
69
70 #include "BLO_writefile.h"
71 #include "BLO_readfile.h"
72
73 #include "IMB_imbuf.h"  // for quicktime_init
74
75 #include "BPY_extern.h"
76
77 #include "RE_pipeline.h"
78
79 #include "GPU_draw.h"
80 #include "GPU_extensions.h"
81
82 #include "playanim_ext.h"
83 #include "mydevice.h"
84 #include "nla.h"
85 #include "datatoc.h"
86
87 /* for passing information between creator and gameengine */
88 #include "SYS_System.h"
89
90 #include <signal.h>
91
92 #ifdef __FreeBSD__
93 # include <sys/types.h>
94 # include <floatingpoint.h>
95 # include <sys/rtprio.h>
96 #endif
97
98 #ifdef WITH_BINRELOC
99 #include "binreloc.h"
100 #endif
101
102 // from buildinfo.c
103 #ifdef BUILD_DATE
104 extern char * build_date;
105 extern char * build_time;
106 extern char * build_platform;
107 extern char * build_type;
108 #endif
109
110 /*      Local Function prototypes */
111 static void print_help(void);
112 static void print_version(void);
113
114
115 /* defined in ghostwinlay and winlay, we can't include carbon here, conflict with DNA */
116 #ifdef __APPLE__
117 extern int checkAppleVideoCard(void);
118 extern void getMacAvailableBounds(short *top, short *left, short *bottom, short *right);
119 extern void     winlay_get_screensize(int *width_r, int *height_r);
120 extern void     winlay_process_events(int wait_for_event);
121 #endif
122
123
124 /* for the callbacks: */
125
126 extern int pluginapi_force_ref(void);  /* from blenpluginapi:pluginapi.c */
127
128 char bprogname[FILE_MAXDIR+FILE_MAXFILE]; /* from blenpluginapi:pluginapi.c */
129 char btempdir[FILE_MAXDIR+FILE_MAXFILE];
130
131 /* Initialise callbacks for the modules that need them */
132 static void setCallbacks(void); 
133
134 #if defined(__sgi) || defined(__alpha__)
135 static void fpe_handler(int sig)
136 {
137         // printf("SIGFPE trapped\n");
138 }
139 #endif
140
141 /* handling ctrl-c event in console */
142 static void blender_esc(int sig)
143 {
144         static int count = 0;
145         
146         G.afbreek = 1;  /* forces render loop to read queue, not sure if its needed */
147         
148         if (sig == 2) {
149                 if (count) {
150                         printf("\nBlender killed\n");
151                         exit(2);
152                 }
153                 printf("\nSent an internal break event. Press ^C again to kill Blender\n");
154                 count++;
155         }
156 }
157
158 static void print_version(void)
159 {
160 #ifdef BUILD_DATE
161         printf ("Blender %d.%02d (sub %d) Build\n", G.version/100, G.version%100, BLENDER_SUBVERSION);
162         printf ("\tbuild date: %s\n", build_date);
163         printf ("\tbuild time: %s\n", build_time);
164         printf ("\tbuild revision: %s\n", build_rev);
165         printf ("\tbuild platform: %s\n", build_platform);
166         printf ("\tbuild type: %s\n", build_type);
167 #else
168         printf ("Blender %d.%02d (sub %d) Build\n", G.version/100, G.version%100, BLENDER_SUBVERSION);
169 #endif
170 }
171
172 static void print_help(void)
173 {
174         printf ("Blender %d.%02d (sub %d) Build\n", G.version/100, G.version%100, BLENDER_SUBVERSION);
175         printf ("Usage: blender [args ...] [file] [args ...]\n");
176         printf ("\nRender options:\n");
177         printf ("  -b <file>\tRender <file> in background (doesn't load the user defaults .B.blend file)\n");
178         printf ("    -a render frames from start to end (inclusive), only works when used after -b\n");
179         printf ("    -S <name>\tSet scene <name>\n");
180         printf ("    -f <frame>\tRender frame <frame> and save it\n");                          
181         printf ("    -s <frame>\tSet start to frame <frame> (use before the -a argument)\n");
182         printf ("    -e <frame>\tSet end to frame <frame> (use before the -a argument)\n");
183         printf ("    -o <path>\tSet the render path and file name.\n");
184         printf ("      Use // at the start of the path to\n");
185         printf ("        render relative to the blend file.\n");
186         printf ("      The # characters are replaced by the frame number, and used to define zero padding.\n");
187         printf ("        ani_##_test.png becomes ani_01_test.png\n");
188         printf ("        test-######.png becomes test-000001.png\n");
189         printf ("        When the filename has no #, The suffix #### is added to the filename\n");
190         printf ("      The frame number will be added at the end of the filename.\n");
191         printf ("      eg: blender -b foobar.blend -o //render_ -F PNG -x 1 -a\n");
192         printf ("\nFormat options:\n");
193         printf ("    -F <format>\tSet the render format, Valid options are...\n");
194         printf ("    \tTGA IRIS HAMX JPEG MOVIE IRIZ RAWTGA\n");
195         printf ("    \tAVIRAW AVIJPEG PNG BMP FRAMESERVER\n");
196         printf ("    (formats that can be compiled into blender, not available on all systems)\n");
197         printf ("    \tHDR TIFF EXR MULTILAYER MPEG AVICODEC QUICKTIME CINEON DPX DDS\n");
198         printf ("    -x <bool>\tSet option to add the file extension to the end of the file.\n");
199         printf ("    -t <threads>\tUse amount of <threads> for rendering (background mode only).\n");
200         printf ("      [1-8], 0 for systems processor count.\n");
201         printf ("\nAnimation playback options:\n");
202         printf ("  -a <file(s)>\tPlayback <file(s)>, only operates this way when -b is not used.\n");
203         printf ("    -p <sx> <sy>\tOpen with lower left corner at <sx>, <sy>\n");
204         printf ("    -m\t\tRead from disk (Don't buffer)\n");
205         printf ("    -f <fps> <fps-base>\t\tSpecify FPS to start with\n");
206         printf ("    -j <frame>\tSet frame step to <frame>\n");
207                                 
208         printf ("\nWindow options:\n");
209         printf ("  -w\t\tForce opening with borders (default)\n");
210         printf ("  -W\t\tForce opening without borders\n");
211         printf ("  -p <sx> <sy> <w> <h>\tOpen with lower left corner at <sx>, <sy>\n");
212         printf ("                      \tand width and height <w>, <h>\n");
213         printf ("\nGame Engine specific options:\n");
214         printf ("  -g fixedtime\t\tRun on 50 hertz without dropping frames\n");
215         printf ("  -g vertexarrays\tUse Vertex Arrays for rendering (usually faster)\n");
216         printf ("  -g noaudio\t\tNo audio in Game Engine\n");
217         printf ("  -g nomipmap\t\tNo Texture Mipmapping\n");
218         printf ("  -g linearmipmap\tLinear Texture Mipmapping instead of Nearest (default)\n");
219
220         printf ("\nMisc options:\n");
221         printf ("  -d\t\tTurn debugging on\n");
222         printf ("  -noaudio\tDisable audio on systems that support audio\n");
223         printf ("  -nojoystick\tDisable joystick support\n");
224         printf ("  -noglsl\tDisable GLSL shading\n");
225         printf ("  -h\t\tPrint this help text\n");
226         printf ("  -y\t\tDisable automatic python script execution (scriptlinks, pydrivers, pyconstraints, pynodes)\n");
227         printf ("  -P <filename>\tRun the given Python script (filename or Blender Text)\n");
228 #ifdef WIN32
229         printf ("  -R\t\tRegister .blend extension\n");
230 #endif
231         printf ("  -v\t\tPrint Blender version and exit\n");
232         printf ("  --\t\tEnds option processing.  Following arguments are \n");
233         printf ("    \t\t   passed unchanged.  Access via Python's sys.argv\n");
234         printf ("\nEnvironment Variables:\n");
235         printf ("  $HOME\t\t\tStore files such as .blender/ .B.blend .Bfs .Blog here.\n");
236 #ifdef WIN32
237         printf ("  $TEMP\t\tStore temporary files here.\n");
238 #else
239         printf ("  $TMP or $TMPDIR\tStore temporary files here.\n");
240         printf ("  $BF_TIFF_LIB\t\tUse an alternative libtiff.so for loading tiff image files.\n");
241 #endif
242 #ifndef DISABLE_SDL
243         printf ("  $SDL_AUDIODRIVER\tLibSDL audio driver - alsa, esd, alsa, dma.\n");
244 #endif
245         printf ("  $IMAGEEDITOR\t\tImage editor executable, launch with the IKey from the file selector.\n");
246         printf ("  $WINEDITOR\t\tText editor executable, launch with the EKey from the file selector.\n");
247         printf ("  $PYTHONHOME\t\tPath to the python directory, eg. /usr/lib/python.\n");
248         printf ("\nNote: Arguments must be separated by white space. eg:\n");
249         printf ("    \"blender -ba test.blend\"\n");
250         printf ("  ...will ignore the 'a'\n");
251         printf ("    \"blender -b test.blend -f8\"\n");
252         printf ("  ...will ignore 8 because there is no space between the -f and the frame value\n");
253         printf ("Note: Arguments are executed in the order they are given. eg:\n");
254         printf ("    \"blender -b test.blend -f 1 -o /tmp\"\n");
255         printf ("  ...may not render to /tmp because '-f 1' renders before the output path is set\n");
256         printf ("    \"blender -b -o /tmp test.blend -f 1\"\n");
257         printf ("  ...may not render to /tmp because loading the blend file overwrites the output path that was set\n");
258         printf ("    \"blender -b test.blend -o /tmp -f 1\" works as expected.\n\n");
259 }
260
261
262 double PIL_check_seconds_timer(void);
263 extern void winlay_get_screensize(int *width_r, int *height_r);
264
265 static void main_init_screen( void )
266 {
267         setscreen(G.curscreen);
268         
269         if(G.main->scene.first==0) {
270                 set_scene( add_scene("1") );
271         }
272 }
273
274 int main(int argc, char **argv)
275 {
276         int a, i, stax, stay, sizx, sizy, scr_init = 0;
277         SYS_SystemHandle syshandle;
278
279 #if defined(WIN32) || defined (__linux__)
280         int audio = 1;
281 #else
282         int audio = 0;
283 #endif
284
285         
286 #ifdef WITH_BINRELOC
287         br_init( NULL );
288 #endif
289
290         setCallbacks();
291 #ifdef __APPLE__
292                 /* patch to ignore argument finder gives us (pid?) */
293         if (argc==2 && strncmp(argv[1], "-psn_", 5)==0) {
294                 extern int GHOST_HACK_getFirstFile(char buf[]);
295                 static char firstfilebuf[512];
296                 int scr_x,scr_y;
297
298                 argc= 1;
299
300         /* first let us check if we are hardware accelerated and with VRAM > 16 Mo */
301         
302         if (checkAppleVideoCard()) {
303                         short top, left, bottom, right;
304                         
305                         winlay_get_screensize(&scr_x, &scr_y); 
306                         getMacAvailableBounds(&top, &left, &bottom, &right);
307                         setprefsize(left +10,scr_y - bottom +10,right-left -20,bottom - 64, 0);
308
309         } else {
310                         winlay_get_screensize(&scr_x, &scr_y);
311
312                         /* 40 + 684 + (headers) 22 + 22 = 768, the powerbook screen height */
313                         setprefsize(120, 40, 850, 684, 0);
314         }
315     
316                 winlay_process_events(0);
317                 if (GHOST_HACK_getFirstFile(firstfilebuf)) {
318                         argc= 2;
319                         argv[1]= firstfilebuf;
320                 }
321         }
322
323 #endif
324
325 #ifdef __FreeBSD__
326         fpsetmask(0);
327 #endif
328 #ifdef __linux__
329     #ifdef __alpha__
330         signal (SIGFPE, fpe_handler);
331     #endif
332 #endif
333 #if defined(__sgi)
334         signal (SIGFPE, fpe_handler);
335 #endif
336
337         // copy path to executable in bprogname. playanim and creting runtimes
338         // need this.
339
340         BLI_where_am_i(bprogname, argv[0]);
341         
342                 /* Hack - force inclusion of the plugin api functions,
343                  * see blenpluginapi:pluginapi.c
344                  */
345         pluginapi_force_ref();
346         
347         init_nodesystem();
348         
349         initglobals();  /* blender.c */
350
351         syshandle = SYS_GetSystem();
352         GEN_init_messaging_system();
353
354         /* first test for background */
355
356         G.f |= G_DOSCRIPTLINKS; /* script links enabled by default */
357
358         for(a=1; a<argc; a++) {
359
360                 /* Handle unix and windows style help requests */
361                 if ((!strcmp(argv[a], "--help")) || (!strcmp(argv[a], "/?"))){
362                         print_help();
363                         exit(0);
364                 }
365
366                 /* end argument processing after -- */
367                 if (!strcmp( argv[a], "--")){
368                         a = argc;
369                         break;
370                 }
371
372                 /* Handle long version request */
373                 if (!strcmp(argv[a], "--version")){
374                         print_version();
375                         exit(0);
376                 }
377
378                 /* Handle -* switches */
379                 else if(argv[a][0] == '-') {
380                         switch(argv[a][1]) {
381                         case 'a': /* -b was not given, play an animation */
382                                 
383                                 /* exception here, see below, it probably needs happens after qt init? */
384                                 libtiff_init();
385
386                                 playanim(argc-1, argv+1);
387                                 exit(0);
388                                 break;
389                         case 'b':
390                         case 'B':
391                                 G.background = 1;
392                                 a= argc;
393                                 break;
394
395                         case 'y':
396                                 G.f &= ~G_DOSCRIPTLINKS;
397                                 break;
398
399                         case 'Y':
400                                 printf ("-y was used to disable script links because,\n");
401                                 printf ("\t-p being taken, Ton was of the opinion that Y\n");
402                                 printf ("\tlooked like a split (disabled) snake, and also\n");
403                                 printf ("\twas similar to a python's tongue (unproven).\n\n");
404
405                                 printf ("\tZr agreed because it gave him a reason to add a\n");
406                                 printf ("\tcompletely useless text into Blender.\n\n");
407                                 
408                                 printf ("\tADDENDUM! Ton, in defense, found this picture of\n");
409                                 printf ("\tan Australian python, exhibiting her (his/its) forked\n");
410                                 printf ("\tY tongue. It could be part of an H Zr retorted!\n\n");
411                                 printf ("\thttp://www.users.bigpond.com/snake.man/\n");
412                                 
413                                 exit(252);
414                                 
415                         case 'h':                       
416                                 print_help();
417                                 exit(0);
418                         case 'v':
419                                 print_version();
420                                 exit(0);
421                         default:
422                                 break;
423                         }
424                 }
425         }
426
427 #ifdef __sgi
428         setuid(getuid()); /* end superuser */
429 #endif
430
431         /* for all platforms, even windos has it! */
432         if(G.background) signal(SIGINT, blender_esc);   /* ctrl c out bg render */
433         
434         /* background render uses this font too */
435         BKE_font_register_builtin(datatoc_Bfont, datatoc_Bfont_size);
436         
437         init_def_material();
438
439         if(G.background==0) {
440                 winlay_get_screensize(&sizx, &sizy);
441                 stax=0;
442                 stay=0;
443
444                 for(a=1; a<argc; a++) {
445                         if(argv[a][0] == '-') {
446                                 switch(argv[a][1]) {
447                                 case 'p':       /* prefsize */
448                                         if (argc-a < 5) {
449                                                 printf ("-p requires four arguments\n");
450                                                 exit(1);
451                                         }
452                                         a++;
453                                         stax= atoi(argv[a]);
454                                         a++;
455                                         stay= atoi(argv[a]);
456                                         a++;
457                                         sizx= atoi(argv[a]);
458                                         a++;
459                                         sizy= atoi(argv[a]);
460                                         G.windowstate = G_WINDOWSTATE_BORDER;
461
462                                         break;
463                                 case 'd':
464                                         G.f |= G_DEBUG;         /* std output printf's */ 
465                                         printf ("Blender %d.%02d (sub %d) Build\n", G.version/100, G.version%100, BLENDER_SUBVERSION);
466                                         MEM_set_memory_debug();
467 #ifdef NAN_BUILDINFO
468                                         printf("Build: %s %s %s %s\n", build_date, build_time, build_platform, build_type);
469
470 #endif // NAN_BUILDINFO
471                                         for (i = 0; i < argc; i++) {
472                                                 printf("argv[%d] = %s\n", i, argv[i]);
473                                         }
474                                         break;
475             
476                                 case 'w':
477                                         G.windowstate = G_WINDOWSTATE_BORDER;
478                                         break;
479                                 case 'W':
480                                         /* XXX, fixme mein, borderless on OSX */
481                                         G.windowstate = G_WINDOWSTATE_FULLSCREEN;
482                                         break;
483                                 case 'R':
484                                         /* Registering filetypes only makes sense on windows...      */
485 #ifdef WIN32
486                                         RegisterBlendExtension(argv[0]);
487 #endif
488                                         break;
489                                 case 'n':
490                                 case 'N':
491                                         if (BLI_strcasecmp(argv[a], "-noaudio") == 0|| BLI_strcasecmp(argv[a], "-nosound") == 0) {
492                                                 /**
493                                                         notify the gameengine that no audio is wanted, even if the user didn't give
494                                                         the flag -g noaudio.
495                                                 */
496
497                                                 SYS_WriteCommandLineInt(syshandle,"noaudio",1);
498                                                 audio = 0;
499                                                 if (G.f & G_DEBUG) printf("setting audio to: %d\n", audio);
500                                         }
501                                         if (BLI_strcasecmp(argv[a], "-nojoystick") == 0) {
502                                                 /**
503                                                         don't initialize joysticks if user doesn't want to use joysticks
504                                                         failed joystick initialization delays over 5 seconds, before game engine start
505                                                 */
506                                                 SYS_WriteCommandLineInt(syshandle,"nojoystick",1);
507                                                 if (G.f & G_DEBUG) printf("disabling nojoystick\n");
508                                         }
509                                         if (BLI_strcasecmp(argv[a], "-noglsl") == 0)
510                                                 GPU_extensions_disable();
511                                         break;
512                                 }
513                         }
514                 }
515
516                 if ( (G.windowstate == G_WINDOWSTATE_BORDER) || (G.windowstate == G_WINDOWSTATE_FULLSCREEN)) 
517                         setprefsize(stax, stay, sizx, sizy, 0);
518                 
519                 BPY_start_python(argc, argv);
520                 
521                 /**
522                  * NOTE: sound_init_audio() *must be* after start_python,
523                  * at least on FreeBSD.
524                  * added note (ton): i removed it altogether
525                  */
526
527                 BIF_init(); /* loads .B.blend */
528                 
529                 BLI_where_is_temp( btempdir, 1 ); /* call after loading the .B.blend so we can read U.tempdir */
530
531 #ifndef DISABLE_SDL
532 #ifdef __linux__
533                 /* On linux the default SDL driver dma often would not play
534                  * use alsa if none is set */
535                 if ( getenv("SDL_AUDIODRIVER") == NULL) {
536                         setenv("SDL_AUDIODRIVER", "alsa", 1);
537                 }
538 #endif
539 #endif
540         }
541         else {
542                 BPY_start_python(argc, argv);
543                 
544                 BLI_where_is_temp( btempdir, 0 ); /* call after loading the .B.blend so we can read U.tempdir */
545                 
546                 // (ton) Commented out. I have no idea whats thisfor... will mail around!
547                 // SYS_WriteCommandLineInt(syshandle,"noaudio",1);
548         // audio = 0;
549         // sound_init_audio();
550         // if (G.f & G_DEBUG) printf("setting audio to: %d\n", audio);
551         }
552
553         /**
554          * NOTE: the U.pythondir string is NULL until BIF_init() is executed,
555          * so we provide the BPY_ function below to append the user defined
556          * pythondir to Python's sys.path at this point.  Simply putting
557          * BIF_init() before BPY_start_python() crashes Blender at startup.
558          * Update: now this function also inits the bpymenus, which also depend
559          * on U.pythondir.
560          */
561         BPY_post_start_python();
562
563 #ifdef WITH_QUICKTIME
564
565         quicktime_init();
566
567 #endif /* WITH_QUICKTIME */
568
569         /* dynamically load libtiff, if available */
570         libtiff_init();
571         if (!G.have_libtiff && (G.f & G_DEBUG)) {
572                 printf("Unable to load: libtiff.\n");
573                 printf("Try setting the BF_TIFF_LIB environment variable if you want this support.\n");
574                 printf("Example: setenv BF_TIFF_LIB /usr/lib/libtiff.so\n");
575         }
576
577         /* OK we are ready for it */
578
579         for(a=1; a<argc; a++) {
580                 if (G.afbreek==1) break;
581
582                 if(argv[a][0] == '-') {
583                         switch(argv[a][1]) {
584                         case '-':  /* -- ends argument processing */
585                                 a = argc;
586                                 break;
587                                 
588                         case 'p':       /* prefsize */
589                                 a+= 4;
590                                 break;
591
592                         case 'g':
593                                 {
594                                 /**
595                                 gameengine parameters are automaticly put into system
596                                 -g [paramname = value]
597                                 -g [boolparamname]
598                                 example:
599                                 -g novertexarrays
600                                 -g maxvertexarraysize = 512
601                                 */
602
603                                         if(++a < argc)
604                                         {
605                                                 char* paramname = argv[a];
606                                                 /* check for single value versus assignment */
607                                                 if (a+1 < argc && (*(argv[a+1]) == '='))
608                                                 {
609                                                         a++;
610                                                         if (a+1 < argc)
611                                                         {
612                                                                 a++;
613                                                                 /* assignment */
614                                                                 SYS_WriteCommandLineString(syshandle,paramname,argv[a]);
615                                                         }  else
616                                                         {
617                                                                 printf("error: argument assignment (%s) without value.\n",paramname);
618                                                         }
619                                                         /* name arg eaten */
620
621                                                 } else
622                                                 {
623                                                         SYS_WriteCommandLineInt(syshandle,argv[a],1);
624
625                                                         /* doMipMap */
626                                                         if (!strcmp(argv[a],"nomipmap"))
627                                                         {
628                                                                 GPU_set_mipmap(0); //doMipMap = 0;
629                                                         }
630                                                         /* linearMipMap */
631                                                         if (!strcmp(argv[a],"linearmipmap"))
632                                                         {
633                                                                 GPU_set_linear_mipmap(1); //linearMipMap = 1;
634                                                         }
635
636
637                                                 } /* if (*(argv[a+1]) == '=') */
638                                         } /*    if(++a < argc)  */
639                                         break;
640                                 }
641                         case 'f':
642                                 a++;
643                                 if (G.scene) {
644                                         if (a < argc) {
645                                                 int frame= MIN2(MAXFRAME, MAX2(1, atoi(argv[a])));
646                                                 Render *re= RE_NewRender(G.scene->id.name);
647
648                                                 if (G.f & G_DOSCRIPTLINKS)
649                                                         BPY_do_all_scripts(SCRIPT_RENDER, 0);
650
651                                                 RE_BlenderAnim(re, G.scene, frame, frame, G.scene->frame_step);
652
653                                                 BPY_do_all_scripts(SCRIPT_POSTRENDER, 0);
654                                         }
655                                 } else {
656                                         printf("\nError: no blend loaded. cannot use '-f'.\n");
657                                 }
658                                 break;
659                         case 'a':
660                                 if (G.scene) {
661                                         Render *re= RE_NewRender(G.scene->id.name);
662
663                                         if (G.f & G_DOSCRIPTLINKS)
664                                                 BPY_do_all_scripts(SCRIPT_RENDER, 1);
665
666                                         RE_BlenderAnim(re, G.scene, G.scene->r.sfra, G.scene->r.efra, G.scene->frame_step);
667
668                                         if (G.f & G_DOSCRIPTLINKS)
669                                                 BPY_do_all_scripts(SCRIPT_POSTRENDER, 1);
670                                 } else {
671                                         printf("\nError: no blend loaded. cannot use '-a'.\n");
672                                 }
673                                 break;
674                         case 'S':
675                                 if(++a < argc) {
676                                         set_scene_name(argv[a]);
677                                 }
678                                 break;
679                         case 's':
680                                 a++;
681                                 if(G.scene) {
682                                         int frame= MIN2(MAXFRAME, MAX2(1, atoi(argv[a])));
683                                         if (a < argc) (G.scene->r.sfra) = frame;
684                                 } else {
685                                         printf("\nError: no blend loaded. cannot use '-s'.\n");
686                                 }
687                                 break;
688                         case 'e':
689                                 a++;
690                                 if(G.scene) {
691                                         int frame= MIN2(MAXFRAME, MAX2(1, atoi(argv[a])));
692                                         if (a < argc) (G.scene->r.efra) = frame;
693                                 } else {
694                                         printf("\nError: no blend loaded. cannot use '-e'.\n");
695                                 }
696                                 break;
697                         case 'j':
698                                 a++;
699                                 if(G.scene) {
700                                         int fstep= MIN2(MAXFRAME, MAX2(1, atoi(argv[a])));
701                                         if (a < argc) (G.scene->frame_step) = fstep;
702                                 } else {
703                                         printf("\nError: no blend loaded. cannot use '-j'.\n");
704                                 }
705                                 break;
706                         case 'P':
707                                 a++;
708                                 if (a < argc) {
709                                         /* If we're not running in background mode, then give python a valid screen */
710                                         if ((G.background==0) && (scr_init==0)) {
711                                                 main_init_screen();
712                                                 scr_init = 1;
713                                         }
714                                         BPY_run_python_script (argv[a]);
715                                 }
716                                 else printf("\nError: you must specify a Python script after '-P '.\n");
717                                 break;
718                         case 'o':
719                                 a++;
720                                 if (a < argc){
721                                         if(G.scene) {
722                                                 BLI_strncpy(G.scene->r.pic, argv[a], FILE_MAXDIR);
723                                         } else {
724                                                 printf("\nError: no blend loaded. cannot use '-o'.\n");
725                                         }
726                                 } else {
727                                         printf("\nError: you must specify a path after '-o '.\n");
728                                 }
729                                 break;
730                         case 'F':
731                                 a++;
732                                 if (a < argc){
733                                         if(!G.scene) {
734                                                 printf("\nError: no blend loaded. order the arguments so '-F ' is after the blend is loaded.\n");
735                                         } else {
736                                                 if      (!strcmp(argv[a],"TGA")) G.scene->r.imtype = R_TARGA;
737                                                 else if (!strcmp(argv[a],"IRIS")) G.scene->r.imtype = R_IRIS;
738                                                 else if (!strcmp(argv[a],"HAMX")) G.scene->r.imtype = R_HAMX;
739 #ifdef WITH_DDS
740                                                 else if (!strcmp(argv[a],"DDS")) G.scene->r.imtype = R_DDS;
741 #endif
742                                                 else if (!strcmp(argv[a],"JPEG")) G.scene->r.imtype = R_JPEG90;
743                                                 else if (!strcmp(argv[a],"MOVIE")) G.scene->r.imtype = R_MOVIE;
744                                                 else if (!strcmp(argv[a],"IRIZ")) G.scene->r.imtype = R_IRIZ;
745                                                 else if (!strcmp(argv[a],"RAWTGA")) G.scene->r.imtype = R_RAWTGA;
746                                                 else if (!strcmp(argv[a],"AVIRAW")) G.scene->r.imtype = R_AVIRAW;
747                                                 else if (!strcmp(argv[a],"AVIJPEG")) G.scene->r.imtype = R_AVIJPEG;
748                                                 else if (!strcmp(argv[a],"PNG")) G.scene->r.imtype = R_PNG;
749                                                 else if (!strcmp(argv[a],"AVICODEC")) G.scene->r.imtype = R_AVICODEC;
750                                                 else if (!strcmp(argv[a],"QUICKTIME")) G.scene->r.imtype = R_QUICKTIME;
751                                                 else if (!strcmp(argv[a],"BMP")) G.scene->r.imtype = R_BMP;
752                                                 else if (!strcmp(argv[a],"HDR")) G.scene->r.imtype = R_RADHDR;
753                                                 else if (!strcmp(argv[a],"TIFF")) G.scene->r.imtype = R_TIFF;
754 #ifdef WITH_OPENEXR
755                                                 else if (!strcmp(argv[a],"EXR")) G.scene->r.imtype = R_OPENEXR;
756                                                 else if (!strcmp(argv[a],"MULTILAYER")) G.scene->r.imtype = R_MULTILAYER;
757 #endif
758                                                 else if (!strcmp(argv[a],"MPEG")) G.scene->r.imtype = R_FFMPEG;
759                                                 else if (!strcmp(argv[a],"FRAMESERVER")) G.scene->r.imtype = R_FRAMESERVER;
760                                                 else if (!strcmp(argv[a],"CINEON")) G.scene->r.imtype = R_CINEON;
761                                                 else if (!strcmp(argv[a],"DPX")) G.scene->r.imtype = R_DPX;
762                                                 else printf("\nError: Format from '-F' not known or not compiled in this release.\n");
763                                         }
764                                 } else {
765                                         printf("\nError: no blend loaded. cannot use '-x'.\n");
766                                 }
767                                 break;
768                                 
769                         case 't':
770                                 a++;
771                                 if(G.background) {
772                                         RE_set_max_threads(atoi(argv[a]));
773                                 } else {
774                                         printf("Warning: threads can only be set in background mode\n");
775                                 }
776                                 break;
777                         case 'x': /* extension */
778                                 a++;
779                                 if (a < argc) {
780                                         if(G.scene) {
781                                                 if (argv[a][0] == '0') {
782                                                         G.scene->r.scemode &= ~R_EXTENSION;
783                                                 } else if (argv[a][0] == '1') {
784                                                         G.scene->r.scemode |= R_EXTENSION;
785                                                 } else {
786                                                         printf("\nError: Use '-x 1' or '-x 0' To set the extension option.\n");
787                                                 }
788                                         } else {
789                                                 printf("\nError: no blend loaded. order the arguments so '-o ' is after '-x '.\n");
790                                         }
791                                 } else {
792                                         printf("\nError: you must specify a path after '- '.\n");
793                                 }
794                                 break;
795                         }
796                 }
797                 else {
798                         
799                         /* Make the path absolute because its needed for relative linked blends to be found */
800                         char filename[FILE_MAXDIR + FILE_MAXFILE];
801                         
802                         BLI_strncpy(filename, argv[a], sizeof(filename));
803                         BLI_convertstringcwd(filename);
804                         
805                         if (G.background) {
806                                 int retval = BKE_read_file(filename, NULL);
807                                 sound_initialize_sounds();
808                                 
809                                 /*we successfully loaded a blend file, get sure that
810                                 pointcache works */
811                                 if (retval!=0) G.relbase_valid = 1;
812
813                                 /* happens for the UI on file reading too */
814                                 BKE_reset_undo();
815                                 BKE_write_undo("original");     /* save current state */
816                         } else {
817                                 /* we are not running in background mode here, but start blender in UI mode with 
818                                    a file - this should do everything a 'load file' does */
819                                 BIF_read_file(filename);
820                         }
821                 }
822         }
823
824         if(G.background) {
825                 /* actually incorrect, but works for now (ton) */
826                 exit_usiblender();
827         }
828         
829         if (scr_init==0) {
830                 main_init_screen();
831         }
832         
833         screenmain(); /* main display loop */
834
835         return 0;
836 } /* end of int main(argc,argv) */
837
838 static void error_cb(char *err)
839 {
840         error("%s", err);
841 }
842
843 static void mem_error_cb(char *errorStr)
844 {
845         fprintf(stderr, errorStr);
846         fflush(stderr);
847 }
848
849 static void setCallbacks(void)
850 {
851         /* Error output from the alloc routines: */
852         MEM_set_error_callback(mem_error_cb);
853
854
855         /* BLI_blenlib: */
856
857         BLI_setErrorCallBack(error_cb); /* */
858         BLI_setInterruptCallBack(blender_test_break);
859
860 }