Preparation to force an audio device via command line. Will be implemented by theeth.
[blender-staging.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
33 /* for setuid / getuid */
34 #ifdef __sgi
35 #include <sys/types.h>
36 #include <unistd.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 #include "DNA_scene_types.h"
51
52 #include "BLI_blenlib.h"
53
54 #include "BKE_utildefines.h"
55 #include "BKE_blender.h"
56 #include "BKE_context.h"
57 #include "BKE_font.h"
58 #include "BKE_global.h"
59 #include "BKE_main.h"
60 #include "BKE_material.h"
61 #include "BKE_packedFile.h"
62 #include "BKE_scene.h"
63 #include "BKE_node.h"
64 #include "BKE_report.h"
65 #include "BKE_sound.h"
66
67 #include "IMB_imbuf.h"  // for quicktime_init
68
69 #ifndef DISABLE_PYTHON
70 #include "BPY_extern.h"
71 #endif
72
73 #include "RE_pipeline.h"
74
75 //XXX #include "playanim_ext.h"
76 #include "ED_datafiles.h"
77 #include "UI_interface.h"
78
79 #include "WM_api.h"
80
81 #include "RNA_define.h"
82
83 #include "GPU_draw.h"
84 #include "GPU_extensions.h"
85
86 /* for passing information between creator and gameengine */
87 #include "SYS_System.h"
88
89 #include <signal.h>
90
91 #ifdef __FreeBSD__
92 # include <sys/types.h>
93 # include <floatingpoint.h>
94 # include <sys/rtprio.h>
95 #endif
96
97 #ifdef WITH_BINRELOC
98 #include "binreloc.h"
99 #endif
100
101 // from buildinfo.c
102 #ifdef BUILD_DATE
103 extern char * build_date;
104 extern char * build_time;
105 extern char * build_rev;
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 /* for the callbacks: */
115
116 extern int pluginapi_force_ref(void);  /* from blenpluginapi:pluginapi.c */
117
118 char bprogname[FILE_MAXDIR+FILE_MAXFILE]; /* from blenpluginapi:pluginapi.c */
119 char btempdir[FILE_MAXDIR+FILE_MAXFILE];
120
121 /* unix path support.
122  * defined by the compiler. eg "/usr/share/blender/2.5" "/opt/blender/2.5" */
123 #ifndef BLENDERPATH
124 #define BLENDERPATH ""
125 #endif
126  
127 char blender_path[FILE_MAXDIR+FILE_MAXFILE] = BLENDERPATH;
128
129 /* Initialise callbacks for the modules that need them */
130 static void setCallbacks(void); 
131
132 #if defined(__sgi) || defined(__alpha__)
133 static void fpe_handler(int sig)
134 {
135         // printf("SIGFPE trapped\n");
136 }
137 #endif
138
139 /* handling ctrl-c event in console */
140 static void blender_esc(int sig)
141 {
142         static int count = 0;
143         
144         G.afbreek = 1;  /* forces render loop to read queue, not sure if its needed */
145         
146         if (sig == 2) {
147                 if (count) {
148                         printf("\nBlender killed\n");
149                         exit(2);
150                 }
151                 printf("\nSent an internal break event. Press ^C again to kill Blender\n");
152                 count++;
153         }
154 }
155
156 static void print_version(void)
157 {
158 #ifdef BUILD_DATE
159         printf ("Blender %d.%02d (sub %d) Build\n", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
160         printf ("\tbuild date: %s\n", build_date);
161         printf ("\tbuild time: %s\n", build_time);
162         printf ("\tbuild revision: %s\n", build_rev);
163         printf ("\tbuild platform: %s\n", build_platform);
164         printf ("\tbuild type: %s\n", build_type);
165 #else
166         printf ("Blender %d.%02d (sub %d) Build\n", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
167 #endif
168 }
169
170 static void print_help(void)
171 {
172         printf ("Blender %d.%02d (sub %d) Build\n", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
173         printf ("Usage: blender [args ...] [file] [args ...]\n");
174         printf ("\nRender options:\n");
175         printf ("  -b <file>\tRender <file> in background (doesn't load the user defaults .B.blend file)\n");
176         printf ("    -a render frames from start to end (inclusive), only works when used after -b\n");
177         printf ("    -S <name>\tSet scene <name>\n");
178         printf ("    -f <frame>\tRender frame <frame> and save it\n");                          
179         printf ("    -s <frame>\tSet start to frame <frame> (use before the -a argument)\n");
180         printf ("    -e <frame>\tSet end to frame <frame> (use before the -a argument)\n");
181         printf ("    -o <path>\tSet the render path and file name.\n");
182         printf ("      Use // at the start of the path to\n");
183         printf ("        render relative to the blend file.\n");
184         printf ("      The # characters are replaced by the frame number, and used to define zero padding.\n");
185         printf ("        ani_##_test.png becomes ani_01_test.png\n");
186         printf ("        test-######.png becomes test-000001.png\n");
187         printf ("        When the filename has no #, The suffix #### is added to the filename\n");
188         printf ("      The frame number will be added at the end of the filename.\n");
189         printf ("      eg: blender -b foobar.blend -o //render_ -F PNG -x 1 -a\n");
190         printf ("    -E <engine>\tSpecify the render engine.\n");
191         printf ("      use -E help to list available engines.\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 <options> <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 nomipmap\t\tNo Texture Mipmapping\n");
217         printf ("  -g linearmipmap\tLinear Texture Mipmapping instead of Nearest (default)\n");
218
219         printf ("\nMisc options:\n");
220         printf ("  -d\t\tTurn debugging on\n");
221         printf ("  -nojoystick\tDisable joystick support\n");
222         printf ("  -noglsl\tDisable GLSL shading\n");
223         printf ("  -noaudio\tForce sound system to None\n");
224         printf ("  -h\t\tPrint this help text\n");
225         printf ("  -y\t\tDisable automatic python script execution (pydrivers, pyconstraints, pynodes)\n");
226         printf ("  -P <filename>\tRun the given Python script (filename or Blender Text)\n");
227 #ifdef WIN32
228         printf ("  -R\t\tRegister .blend extension\n");
229 #endif
230         printf ("  -v\t\tPrint Blender version and exit\n");
231         printf ("  --\t\tEnds option processing.  Following arguments are \n");
232         printf ("    \t\t   passed unchanged.  Access via Python's sys.argv\n");
233         printf ("\nEnvironment Variables:\n");
234         printf ("  $HOME\t\t\tStore files such as .blender/ .B.blend .Bfs .Blog here.\n");
235         printf ("  $BLENDERPATH\tSystem directory to use for data files and scripts.\n");
236         printf ("              \tFor this build of blender the default BLENDERPATH is...\n");
237         printf ("              \t\"%s\"\n", blender_path);
238         printf ("              \tseting the $BLENDERPATH will override this\n");
239 #ifdef WIN32
240         printf ("  $TEMP\t\tStore temporary files here.\n");
241 #else
242         printf ("  $TMP or $TMPDIR\tStore temporary files here.\n");
243         printf ("  $BF_TIFF_LIB\t\tUse an alternative libtiff.so for loading tiff image files.\n");
244 #endif
245 #ifndef DISABLE_SDL
246         printf ("  $SDL_AUDIODRIVER\tLibSDL audio driver - alsa, esd, alsa, dma.\n");
247 #endif
248         printf ("  $IMAGEEDITOR\t\tImage editor executable, launch with the IKey from the file selector.\n");
249         printf ("  $WINEDITOR\t\tText editor executable, launch with the EKey from the file selector.\n");
250         printf ("  $PYTHONHOME\t\tPath to the python directory, eg. /usr/lib/python.\n");
251         printf ("\nNote: Arguments must be separated by white space. eg:\n");
252         printf ("    \"blender -ba test.blend\"\n");
253         printf ("  ...will ignore the 'a'\n");
254         printf ("    \"blender -b test.blend -f8\"\n");
255         printf ("  ...will ignore 8 because there is no space between the -f and the frame value\n");
256         printf ("Note: Arguments are executed in the order they are given. eg:\n");
257         printf ("    \"blender -b test.blend -f 1 -o /tmp\"\n");
258         printf ("  ...may not render to /tmp because '-f 1' renders before the output path is set\n");
259         printf ("    \"blender -b -o /tmp test.blend -f 1\"\n");
260         printf ("  ...may not render to /tmp because loading the blend file overwrites the output path that was set\n");
261         printf ("    \"blender -b test.blend -o /tmp -f 1\" works as expected.\n\n");
262 }
263
264
265 double PIL_check_seconds_timer(void);
266
267 /* XXX This was here to fix a crash when running python scripts
268  * with -P that used the screen.
269  *
270  * static void main_init_screen( void )
271 {
272         setscreen(G.curscreen);
273         
274         if(G.main->scene.first==0) {
275                 set_scene( add_scene("1") );
276         }
277 }*/
278
279 int main(int argc, char **argv)
280 {
281         SYS_SystemHandle syshandle;
282         bContext *C= CTX_create();
283         int a, i, stax, stay, sizx, sizy /*XXX, scr_init = 0*/, file_loaded= 0;
284
285 #ifdef WITH_BINRELOC
286         br_init( NULL );
287 #endif
288
289         setCallbacks();
290 #ifdef __APPLE__
291                 /* patch to ignore argument finder gives us (pid?) */
292         if (argc==2 && strncmp(argv[1], "-psn_", 5)==0) {
293                 extern int GHOST_HACK_getFirstFile(char buf[]);
294                 static char firstfilebuf[512];
295
296                 argc= 1;
297
298                 if (GHOST_HACK_getFirstFile(firstfilebuf)) {
299                         argc= 2;
300                         argv[1]= firstfilebuf;
301                 }
302         }
303
304 #endif
305
306 #ifdef __FreeBSD__
307         fpsetmask(0);
308 #endif
309 #ifdef __linux__
310     #ifdef __alpha__
311         signal (SIGFPE, fpe_handler);
312     #endif
313 #endif
314 #if defined(__sgi)
315         signal (SIGFPE, fpe_handler);
316 #endif
317
318         // copy path to executable in bprogname. playanim and creting runtimes
319         // need this.
320
321         BLI_where_am_i(bprogname, argv[0]);
322         
323         {       /* override the hard coded blender path */
324                 char *blender_path_env = getenv("BLENDERPATH");
325                 if(blender_path_env)
326                         BLI_strncpy(blender_path, blender_path_env, sizeof(blender_path));
327         }
328         
329         RNA_init();
330         RE_engines_init();
331
332                 /* Hack - force inclusion of the plugin api functions,
333                  * see blenpluginapi:pluginapi.c
334                  */
335         pluginapi_force_ref();
336
337         init_nodesystem();
338         
339         initglobals();  /* blender.c */
340
341         syshandle = SYS_GetSystem();
342         GEN_init_messaging_system();
343
344         /* first test for background */
345
346         G.f |= G_DOSCRIPTLINKS; /* script links enabled by default */
347
348         for(a=1; a<argc; a++) {
349
350                 /* Handle unix and windows style help requests */
351                 if ((!strcmp(argv[a], "--help")) || (!strcmp(argv[a], "/?"))){
352                         print_help();
353                         exit(0);
354                 }
355
356                 /* end argument processing after -- */
357                 if (!strcmp( argv[a], "--")){
358                         a = argc;
359                         break;
360                 }
361
362                 /* Handle long version request */
363                 if (!strcmp(argv[a], "--version")){
364                         print_version();
365                         exit(0);
366                 }
367
368                 /* Handle -* switches */
369                 else if(argv[a][0] == '-') {
370                         switch(argv[a][1]) {
371                         case 'a': /* -b was not given, play an animation */
372                                 
373                                 /* exception here, see below, it probably needs happens after qt init? */
374                                 libtiff_init();
375
376 // XXX                          playanim(argc-1, argv+1);
377                                 exit(0);
378                                 break;
379                         case 'b':
380                         case 'B':
381                                 G.background = 1;
382                                 a= argc;
383                                 break;
384
385                         case 'y':
386                                 G.f &= ~G_DOSCRIPTLINKS;
387                                 break;
388
389                         case 'Y':
390                                 printf ("-y was used to disable script links because,\n");
391                                 printf ("\t-p being taken, Ton was of the opinion that Y\n");
392                                 printf ("\tlooked like a split (disabled) snake, and also\n");
393                                 printf ("\twas similar to a python's tongue (unproven).\n\n");
394
395                                 printf ("\tZr agreed because it gave him a reason to add a\n");
396                                 printf ("\tcompletely useless text into Blender.\n\n");
397                                 
398                                 printf ("\tADDENDUM! Ton, in defense, found this picture of\n");
399                                 printf ("\tan Australian python, exhibiting her (his/its) forked\n");
400                                 printf ("\tY tongue. It could be part of an H Zr retorted!\n\n");
401                                 printf ("\thttp://www.users.bigpond.com/snake.man/\n");
402                                 
403                                 exit(252);
404                                 
405                         case 'h':                       
406                                 print_help();
407                                 exit(0);
408                         case 'v':
409                                 print_version();
410                                 exit(0);
411                         default:
412                                 break;
413                         }
414                 }
415         }
416
417 #ifdef __sgi
418         setuid(getuid()); /* end superuser */
419 #endif
420
421         /* for all platforms, even windos has it! */
422         if(G.background) signal(SIGINT, blender_esc);   /* ctrl c out bg render */
423         
424         /* background render uses this font too */
425         BKE_font_register_builtin(datatoc_Bfont, datatoc_Bfont_size);
426         
427         init_def_material();
428
429         if(G.background==0) {
430                 for(a=1; a<argc; a++) {
431                         if(argv[a][0] == '-') {
432                                 switch(argv[a][1]) {
433                                 case 'p':       /* prefsize */
434                                         if (argc-a < 5) {
435                                                 printf ("-p requires four arguments\n");
436                                                 exit(1);
437                                         }
438                                         a++;
439                                         stax= atoi(argv[a]);
440                                         a++;
441                                         stay= atoi(argv[a]);
442                                         a++;
443                                         sizx= atoi(argv[a]);
444                                         a++;
445                                         sizy= atoi(argv[a]);
446
447                                         WM_setprefsize(stax, stay, sizx, sizy);
448                                         break;
449                                 case 'd':
450                                         G.f |= G_DEBUG;         /* std output printf's */ 
451                                         printf ("Blender %d.%02d (sub %d) Build\n", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
452                                         MEM_set_memory_debug();
453 #ifdef NAN_BUILDINFO
454                                         printf("Build: %s %s %s %s\n", build_date, build_time, build_platform, build_type);
455
456 #endif // NAN_BUILDINFO
457                                         for (i = 0; i < argc; i++) {
458                                                 printf("argv[%d] = %s\n", i, argv[i]);
459                                         }
460                                         break;
461                                 case 'E':
462                                         G.f |= G_SWAP_EXCHANGE;
463                                         break;
464                                 case 'w':
465                                         /* with borders XXX OLD CRUFT!*/
466                                         break;
467                                 case 'W':
468                                         /* borderless, win + linux XXX OLD CRUFT */
469                                         /* XXX, fixme mein, borderless on OSX */
470                                         break;
471                                 case 'R':
472                                         /* Registering filetypes only makes sense on windows...      */
473 #ifdef WIN32
474                                         RegisterBlendExtension(argv[0]);
475 #endif
476                                         break;
477                                 case 'n':
478                                 case 'N':
479                                         if (BLI_strcasecmp(argv[a], "-nojoystick") == 0) {
480                                                 /**
481                                                         don't initialize joysticks if user doesn't want to use joysticks
482                                                         failed joystick initialization delays over 5 seconds, before game engine start
483                                                 */
484                                                 SYS_WriteCommandLineInt(syshandle,"nojoystick",1);
485                                                 if (G.f & G_DEBUG) printf("disabling nojoystick\n");
486                                         }
487                                         else if (BLI_strcasecmp(argv[a], "-noglsl") == 0)
488                                                 GPU_extensions_disable();
489                                         else if (BLI_strcasecmp(argv[a], "-noaudio") == 0)
490                                                 sound_force_device(0);
491                                         break;
492                                 }
493                         }
494                 }
495
496                 WM_init(C, argc, argv);
497                 
498                 // XXX BRECHT SOLVE
499                 BLI_where_is_temp( btempdir, 1 ); /* call after loading the .B.blend so we can read U.tempdir */
500
501 #ifndef DISABLE_SDL
502         BLI_setenv("SDL_VIDEODRIVER", "dummy");
503 #ifdef __linux__
504         /* On linux the default SDL driver dma often would not play
505          * use alsa if none is set */
506         setenv("SDL_AUDIODRIVER", "alsa", 0);
507 #endif
508 #endif
509         }
510         else {
511                 /* background mode options */
512                 for(a=1; a<argc; a++) {
513                         if(argv[a][0] == '-') {
514                                 switch(argv[a][1]) {
515                                 case 'd':
516                                         G.f |= G_DEBUG;         /* std output printf's */
517                                         printf ("Blender %d.%02d (sub %d) Build\n", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
518                                         MEM_set_memory_debug();
519 #ifdef NAN_BUILDINFO
520                                         printf("Build: %s %s %s %s\n", build_date, build_time, build_platform, build_type);
521
522 #endif // NAN_BUILDINFO
523                                         for (i = 0; i < argc; i++) {
524                                                 printf("argv[%d] = %s\n", i, argv[i]);
525                                         }
526                                         break;
527                                 case 'n':
528                                 case 'N':
529                                         if (BLI_strcasecmp(argv[a], "-noaudio") == 0)
530                                                 sound_force_device(0);
531                                         break;
532                                 }
533                         }
534                 }
535
536                 WM_init(C, argc, argv);
537
538                 BLI_where_is_temp( btempdir, 0 ); /* call after loading the .B.blend so we can read U.tempdir */
539         }
540 #ifndef DISABLE_PYTHON
541         /**
542          * NOTE: the U.pythondir string is NULL until WM_init() is executed,
543          * so we provide the BPY_ function below to append the user defined
544          * pythondir to Python's sys.path at this point.  Simply putting
545          * WM_init() before BPY_start_python() crashes Blender at startup.
546          * Update: now this function also inits the bpymenus, which also depend
547          * on U.pythondir.
548          */
549         
550         // TODO - U.pythondir
551
552 #endif
553         
554         CTX_py_init_set(C, 1);
555         WM_keymap_init(C);
556
557 #ifdef WITH_QUICKTIME
558
559         quicktime_init();
560
561 #endif /* WITH_QUICKTIME */
562
563         /* dynamically load libtiff, if available */
564         libtiff_init();
565         if (!G.have_libtiff && (G.f & G_DEBUG)) {
566                 printf("Unable to load: libtiff.\n");
567                 printf("Try setting the BF_TIFF_LIB environment variable if you want this support.\n");
568                 printf("Example: setenv BF_TIFF_LIB /usr/lib/libtiff.so\n");
569         }
570
571         /* OK we are ready for it */
572
573         for(a=1; a<argc; a++) {
574                 if (G.afbreek==1) break;
575
576                 if(argv[a][0] == '-') {
577                         switch(argv[a][1]) {
578                         case '-':  /* -- ends argument processing */
579                                 a = argc;
580                                 break;
581                                 
582                         case 'p':       /* prefsize */
583                                 a+= 4;
584                                 break;
585
586                         case 'g':
587                                 {
588                                 /**
589                                 gameengine parameters are automaticly put into system
590                                 -g [paramname = value]
591                                 -g [boolparamname]
592                                 example:
593                                 -g novertexarrays
594                                 -g maxvertexarraysize = 512
595                                 */
596
597                                         if(++a < argc)
598                                         {
599                                                 char* paramname = argv[a];
600                                                 /* check for single value versus assignment */
601                                                 if (a+1 < argc && (*(argv[a+1]) == '='))
602                                                 {
603                                                         a++;
604                                                         if (a+1 < argc)
605                                                         {
606                                                                 a++;
607                                                                 /* assignment */
608                                                                 SYS_WriteCommandLineString(syshandle,paramname,argv[a]);
609                                                         }  else
610                                                         {
611                                                                 printf("error: argument assignment (%s) without value.\n",paramname);
612                                                         }
613                                                         /* name arg eaten */
614
615                                                 } else
616                                                 {
617                                                         SYS_WriteCommandLineInt(syshandle,argv[a],1);
618
619                                                         /* doMipMap */
620                                                         if (!strcmp(argv[a],"nomipmap"))
621                                                         {
622                                                                 GPU_set_mipmap(0); //doMipMap = 0;
623                                                         }
624                                                         /* linearMipMap */
625                                                         if (!strcmp(argv[a],"linearmipmap"))
626                                                         {
627                                                                 GPU_set_linear_mipmap(1); //linearMipMap = 1;
628                                                         }
629
630
631                                                 } /* if (*(argv[a+1]) == '=') */
632                                         } /*    if(++a < argc)  */
633                                         break;
634                                 }
635                         case 'f':
636                                 a++;
637                                 if (CTX_data_scene(C)) {
638                                         Scene *scene= CTX_data_scene(C);
639                                         
640                                         if (a < argc) {
641                                                 int frame = atoi(argv[a]);
642                                                 Render *re = RE_NewRender(scene->id.name);
643                                                 ReportList reports;
644
645                                                 BKE_reports_init(&reports, RPT_PRINT);
646
647                                                 frame = MIN2(MAXFRAME, MAX2(MINAFRAME, frame));
648                                                 
649                                                 RE_BlenderAnim(re, scene, frame, frame, scene->r.frame_step, &reports);
650                                         }
651                                 } else {
652                                         printf("\nError: no blend loaded. cannot use '-f'.\n");
653                                 }
654                                 break;
655                         case 'a':
656                                 if (CTX_data_scene(C)) {
657                                         Scene *scene= CTX_data_scene(C);
658                                         Render *re= RE_NewRender(scene->id.name);
659                                         ReportList reports;
660                                         BKE_reports_init(&reports, RPT_PRINT);
661                                         RE_BlenderAnim(re, scene, scene->r.sfra, scene->r.efra, scene->r.frame_step, &reports);
662                                 } else {
663                                         printf("\nError: no blend loaded. cannot use '-a'.\n");
664                                 }
665                                 break;
666                         case 'S':
667                                 if(++a < argc) {
668                                         set_scene_name(argv[a]);
669                                 }
670                                 break;
671                         case 's':
672                                 a++;
673                                 if (CTX_data_scene(C)) {
674                                         Scene *scene= CTX_data_scene(C);
675                                         if (a < argc) {
676                                                 int frame = atoi(argv[a]);
677                                                 (scene->r.sfra) = MIN2(MAXFRAME, MAX2(1, frame));
678                                         }
679                                 } else {
680                                         printf("\nError: no blend loaded. cannot use '-s'.\n");
681                                 }
682                                 break;
683                         case 'e':
684                                 a++;
685                                 if (CTX_data_scene(C)) {
686                                         Scene *scene= CTX_data_scene(C);
687                                         if (a < argc) {
688                                                 int frame = atoi(argv[a]);
689                                                 (scene->r.efra) = MIN2(MAXFRAME, MAX2(1, frame));
690                                         }
691                                 } else {
692                                         printf("\nError: no blend loaded. cannot use '-e'.\n");
693                                 }
694                                 break;
695                         case 'j':
696                                 a++;
697                                 if (CTX_data_scene(C)) {
698                                         Scene *scene= CTX_data_scene(C);
699                                         if (a < argc) {
700                                                 int frame = atoi(argv[a]);
701                                                 (scene->r.frame_step) = MIN2(MAXFRAME, MAX2(1, frame));
702                                         }
703                                 } else {
704                                         printf("\nError: no blend loaded. cannot use '-j'.\n");
705                                 }
706                                 break;
707                         case 'P':
708
709 #ifndef DISABLE_PYTHON
710                                 a++;
711
712                                 /* workaround for scripts not getting a bpy.context.scene, causes internal errors elsewhere */
713                                 {
714                                         /* XXX, temp setting the WM is ugly, splash also does this :S */
715                                         wmWindowManager *wm= CTX_wm_manager(C);
716                                         wmWindow *prevwin= CTX_wm_window(C);
717
718                                         if(wm->windows.first) {
719                                                 CTX_wm_window_set(C, wm->windows.first);
720
721                                                 BPY_run_python_script(C, argv[a], NULL, NULL); // use reports?
722
723                                                 CTX_wm_window_set(C, prevwin);
724                                         }
725                                         else {
726                                                 fprintf(stderr, "Python script \"%s\" running with missing context data.\n", argv[a]);
727                                                 BPY_run_python_script(C, argv[a], NULL, NULL); // use reports?
728                                         }
729                                 }
730 #if 0
731                                 a++;
732                                 if (a < argc) {
733                                         /* If we're not running in background mode, then give python a valid screen */
734                                         if ((G.background==0) && (scr_init==0)) {
735                                                 main_init_screen();
736                                                 scr_init = 1;
737                                         }
738                                         BPY_run_python_script(C, argv[a], NULL, NULL); // use reports?
739                                 }
740                                 else printf("\nError: you must specify a Python script after '-P '.\n");
741 #endif
742 #else
743                                 printf("This blender was built without python support\n");
744 #endif /* DISABLE_PYTHON */
745                                 break;
746                         case 'o':
747                                 a++;
748                                 if (a < argc){
749                                         if (CTX_data_scene(C)) {
750                                                 Scene *scene= CTX_data_scene(C);
751                                                 BLI_strncpy(scene->r.pic, argv[a], FILE_MAXDIR);
752                                         } else {
753                                                 printf("\nError: no blend loaded. cannot use '-o'.\n");
754                                         }
755                                 } else {
756                                         printf("\nError: you must specify a path after '-o '.\n");
757                                 }
758                                 break;
759                         case 'E':
760                                 a++;
761                                 if (a < argc)
762                                 {
763                                         if (!strcmp(argv[a],"help"))
764                                         {
765                                                 RenderEngineType *type = NULL;
766
767                                                 for( type = R_engines.first; type; type = type->next )
768                                                 {
769                                                         printf("\t%s\n", type->idname);
770                                                 }
771                                                 exit(0);
772                                         }
773                                         else 
774                                         {
775                                                 if (CTX_data_scene(C)==NULL)
776                                                 {
777                                                         printf("\nError: no blend loaded. order the arguments so '-E ' is after the blend is loaded.\n");
778                                                 }
779                                                 else 
780                                                 {
781                                                         Scene *scene= CTX_data_scene(C);
782                                                         RenderData *rd = &scene->r;
783                                                         RenderEngineType *type = NULL;
784                                                         
785                                                         for( type = R_engines.first; type; type = type->next )
786                                                         {
787                                                                 if (!strcmp(argv[a],type->idname))
788                                                                 {
789                                                                         BLI_strncpy(rd->engine, type->idname, sizeof(rd->engine));
790                                                                 }
791                                                         }
792                                                 }
793                                         }
794                                 }
795                                 else
796                                 {
797                                         printf("\nEngine not specified.\n");
798                                 }
799                                 break;
800                         case 'F':
801                                 a++;
802                                 if (a < argc){
803                                         if (CTX_data_scene(C)==NULL) {
804                                                 printf("\nError: no blend loaded. order the arguments so '-F ' is after the blend is loaded.\n");
805                                         } else {
806                                                 Scene *scene= CTX_data_scene(C);
807                                                 if      (!strcmp(argv[a],"TGA")) scene->r.imtype = R_TARGA;
808                                                 else if (!strcmp(argv[a],"IRIS")) scene->r.imtype = R_IRIS;
809                                                 else if (!strcmp(argv[a],"HAMX")) scene->r.imtype = R_HAMX;
810 #ifdef WITH_DDS
811                                                 else if (!strcmp(argv[a],"DDS")) scene->r.imtype = R_DDS;
812 #endif
813                                                 else if (!strcmp(argv[a],"JPEG")) scene->r.imtype = R_JPEG90;
814                                                 else if (!strcmp(argv[a],"MOVIE")) scene->r.imtype = R_MOVIE;
815                                                 else if (!strcmp(argv[a],"IRIZ")) scene->r.imtype = R_IRIZ;
816                                                 else if (!strcmp(argv[a],"RAWTGA")) scene->r.imtype = R_RAWTGA;
817                                                 else if (!strcmp(argv[a],"AVIRAW")) scene->r.imtype = R_AVIRAW;
818                                                 else if (!strcmp(argv[a],"AVIJPEG")) scene->r.imtype = R_AVIJPEG;
819                                                 else if (!strcmp(argv[a],"PNG")) scene->r.imtype = R_PNG;
820                                                 else if (!strcmp(argv[a],"AVICODEC")) scene->r.imtype = R_AVICODEC;
821                                                 else if (!strcmp(argv[a],"QUICKTIME")) scene->r.imtype = R_QUICKTIME;
822                                                 else if (!strcmp(argv[a],"BMP")) scene->r.imtype = R_BMP;
823                                                 else if (!strcmp(argv[a],"HDR")) scene->r.imtype = R_RADHDR;
824                                                 else if (!strcmp(argv[a],"TIFF")) scene->r.imtype = R_TIFF;
825 #ifdef WITH_OPENEXR
826                                                 else if (!strcmp(argv[a],"EXR")) scene->r.imtype = R_OPENEXR;
827                                                 else if (!strcmp(argv[a],"MULTILAYER")) scene->r.imtype = R_MULTILAYER;
828 #endif
829                                                 else if (!strcmp(argv[a],"MPEG")) scene->r.imtype = R_FFMPEG;
830                                                 else if (!strcmp(argv[a],"FRAMESERVER")) scene->r.imtype = R_FRAMESERVER;
831                                                 else if (!strcmp(argv[a],"CINEON")) scene->r.imtype = R_CINEON;
832                                                 else if (!strcmp(argv[a],"DPX")) scene->r.imtype = R_DPX;
833 #if WITH_OPENJPEG
834                                                 else if (!strcmp(argv[a],"JP2")) scene->r.imtype = R_JP2;
835 #endif
836                                                 else printf("\nError: Format from '-F' not known or not compiled in this release.\n");
837                                         }
838                                 } else {
839                                         printf("\nError: no blend loaded. cannot use '-x'.\n");
840                                 }
841                                 break;
842                                 
843                         case 't':
844                                 a++;
845                                 if (a < argc) {
846                                         if(G.background) {
847                                                 RE_set_max_threads(atoi(argv[a]));
848                                         } else {
849                                                 printf("Warning: threads can only be set in background mode\n");
850                                         }
851                                 } else {
852                                         printf("\nError: you must specify a number of threads between 0 and 8 '-t '.\n");
853                                 }
854                                 break;
855                         case 'x': /* extension */
856                                 a++;
857                                 if (a < argc) {
858                                         if (CTX_data_scene(C)) {
859                                                 Scene *scene= CTX_data_scene(C);
860                                                 if (argv[a][0] == '0') {
861                                                         scene->r.scemode &= ~R_EXTENSION;
862                                                 } else if (argv[a][0] == '1') {
863                                                         scene->r.scemode |= R_EXTENSION;
864                                                 } else {
865                                                         printf("\nError: Use '-x 1' or '-x 0' To set the extension option.\n");
866                                                 }
867                                         } else {
868                                                 printf("\nError: no blend loaded. order the arguments so '-o ' is after '-x '.\n");
869                                         }
870                                 } else {
871                                         printf("\nError: you must specify a path after '- '.\n");
872                                 }
873                                 break;
874                         }
875                 }
876                 else {
877                         
878                         /* Make the path absolute because its needed for relative linked blends to be found */
879                         char filename[FILE_MAXDIR + FILE_MAXFILE];
880                         
881                         BLI_strncpy(filename, argv[a], sizeof(filename));
882                         BLI_convertstringcwd(filename);
883                         
884                         if (G.background) {
885                                 int retval = BKE_read_file(C, argv[a], NULL, NULL);
886                                 
887                                 /*we successfully loaded a blend file, get sure that
888                                 pointcache works */
889                                 if (retval!=0) {
890                                         CTX_wm_manager_set(C, NULL); /* remove wm to force check */
891                                         WM_check(C);
892                                         G.relbase_valid = 1;
893                                 }
894
895                                 /* happens for the UI on file reading too (huh? (ton))*/
896 // XXX                  BKE_reset_undo();
897 //                              BKE_write_undo("original");     /* save current state */
898                         } else {
899                                 /* we are not running in background mode here, but start blender in UI mode with 
900                                    a file - this should do everything a 'load file' does */
901                                 WM_read_file(C, filename, NULL);
902                         }
903
904                         file_loaded = 1;
905                 }
906         }
907         
908         if(G.background) {
909                 /* actually incorrect, but works for now (ton) */
910                 WM_exit(C);
911         }
912
913         if(!G.background && !file_loaded)
914                 WM_init_splash(C);
915
916         WM_main(C);
917
918
919         /*XXX if (scr_init==0) {
920                 main_init_screen();
921         }
922         
923         screenmain();*/ /* main display loop */
924
925         return 0;
926 } /* end of int main(argc,argv) */
927
928 static void error_cb(char *err)
929 {
930         
931         printf("%s\n", err);    /* XXX do this in WM too */
932 }
933
934 static void mem_error_cb(char *errorStr)
935 {
936         fputs(errorStr, stderr);
937         fflush(stderr);
938 }
939
940 static void setCallbacks(void)
941 {
942         /* Error output from the alloc routines: */
943         MEM_set_error_callback(mem_error_cb);
944
945
946         /* BLI_blenlib: */
947
948         BLI_setErrorCallBack(error_cb); /* */
949 // XXX  BLI_setInterruptCallBack(blender_test_break);
950
951 }