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