Mac / COCOA : Imbuf
[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_rev;
105 extern char * build_platform;
106 extern char * build_type;
107 #endif
108
109 /*      Local Function prototypes */
110 static void print_help(void);
111 static void print_version(void);
112
113 /* for the callbacks: */
114
115 extern int pluginapi_force_ref(void);  /* from blenpluginapi:pluginapi.c */
116
117 char bprogname[FILE_MAXDIR+FILE_MAXFILE]; /* from blenpluginapi:pluginapi.c */
118 char btempdir[FILE_MAXDIR+FILE_MAXFILE];
119
120 /* unix path support.
121  * defined by the compiler. eg "/usr/share/blender/2.5" "/opt/blender/2.5" */
122 #ifndef BLENDERPATH
123 #define BLENDERPATH ""
124 #endif
125  
126 char blender_path[FILE_MAXDIR+FILE_MAXFILE] = BLENDERPATH;
127
128 /* Initialise callbacks for the modules that need them */
129 static void setCallbacks(void); 
130
131 #if defined(__sgi) || defined(__alpha__)
132 static void fpe_handler(int sig)
133 {
134         // printf("SIGFPE trapped\n");
135 }
136 #endif
137
138 /* handling ctrl-c event in console */
139 static void blender_esc(int sig)
140 {
141         static int count = 0;
142         
143         G.afbreek = 1;  /* forces render loop to read queue, not sure if its needed */
144         
145         if (sig == 2) {
146                 if (count) {
147                         printf("\nBlender killed\n");
148                         exit(2);
149                 }
150                 printf("\nSent an internal break event. Press ^C again to kill Blender\n");
151                 count++;
152         }
153 }
154
155 static void print_version(void)
156 {
157 #ifdef BUILD_DATE
158         printf ("Blender %d.%02d (sub %d) Build\n", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
159         printf ("\tbuild date: %s\n", build_date);
160         printf ("\tbuild time: %s\n", build_time);
161         printf ("\tbuild revision: %s\n", build_rev);
162         printf ("\tbuild platform: %s\n", build_platform);
163         printf ("\tbuild type: %s\n", build_type);
164 #else
165         printf ("Blender %d.%02d (sub %d) Build\n", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
166 #endif
167 }
168
169 static void print_help(void)
170 {
171         printf ("Blender %d.%02d (sub %d) Build\n", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
172         printf ("Usage: blender [args ...] [file] [args ...]\n");
173         printf ("\nRender options:\n");
174         printf ("  -b <file>\tRender <file> in background (doesn't load the user defaults .B.blend file)\n");
175         printf ("    -a render frames from start to end (inclusive), only works when used after -b\n");
176         printf ("    -S <name>\tSet scene <name>\n");
177         printf ("    -f <frame>\tRender frame <frame> and save it\n");                          
178         printf ("    -s <frame>\tSet start to frame <frame> (use before the -a argument)\n");
179         printf ("    -e <frame>\tSet end to frame <frame> (use before the -a argument)\n");
180         printf ("    -o <path>\tSet the render path and file name.\n");
181         printf ("      Use // at the start of the path to\n");
182         printf ("        render relative to the blend file.\n");
183         printf ("      The # characters are replaced by the frame number, and used to define zero padding.\n");
184         printf ("        ani_##_test.png becomes ani_01_test.png\n");
185         printf ("        test-######.png becomes test-000001.png\n");
186         printf ("        When the filename has no #, The suffix #### is added to the filename\n");
187         printf ("      The frame number will be added at the end of the filename.\n");
188         printf ("      eg: blender -b foobar.blend -o //render_ -F PNG -x 1 -a\n");
189         printf ("    -E <engine>\tSpecify the render engine.\n");
190         printf ("      use -E help to list available engines.\n");
191         printf ("\nFormat options:\n");
192         printf ("    -F <format>\tSet the render format, Valid options are...\n");
193         printf ("    \tTGA IRIS HAMX JPEG MOVIE IRIZ RAWTGA\n");
194         printf ("    \tAVIRAW AVIJPEG PNG BMP FRAMESERVER\n");
195         printf ("    (formats that can be compiled into blender, not available on all systems)\n");
196         printf ("    \tHDR TIFF EXR MULTILAYER MPEG AVICODEC QUICKTIME CINEON DPX DDS\n");
197         printf ("    -x <bool>\tSet option to add the file extension to the end of the file.\n");
198         printf ("    -t <threads>\tUse amount of <threads> for rendering (background mode only).\n");
199         printf ("      [1-8], 0 for systems processor count.\n");
200         printf ("\nAnimation playback options:\n");
201         printf ("  -a <options> <file(s)>\tPlayback <file(s)>, only operates this way when -b is not used.\n");
202         printf ("    -p <sx> <sy>\tOpen with lower left corner at <sx>, <sy>\n");
203         printf ("    -m\t\tRead from disk (Don't buffer)\n");
204         printf ("    -f <fps> <fps-base>\t\tSpecify FPS to start with\n");
205         printf ("    -j <frame>\tSet frame step to <frame>\n");
206                                 
207         printf ("\nWindow options:\n");
208         printf ("  -w\t\tForce opening with borders (default)\n");
209         printf ("  -W\t\tForce opening without borders\n");
210         printf ("  -p <sx> <sy> <w> <h>\tOpen with lower left corner at <sx>, <sy>\n");
211         printf ("                      \tand width and height <w>, <h>\n");
212         printf ("\nGame Engine specific options:\n");
213         printf ("  -g fixedtime\t\tRun on 50 hertz without dropping frames\n");
214         printf ("  -g vertexarrays\tUse Vertex Arrays for rendering (usually faster)\n");
215         printf ("  -g nomipmap\t\tNo Texture Mipmapping\n");
216         printf ("  -g linearmipmap\tLinear Texture Mipmapping instead of Nearest (default)\n");
217
218         printf ("\nMisc options:\n");
219         printf ("  -d\t\tTurn debugging on\n");
220         printf ("  -nojoystick\tDisable joystick support\n");
221         printf ("  -noglsl\tDisable GLSL shading\n");
222         printf ("  -h\t\tPrint this help text\n");
223         printf ("  -y\t\tDisable automatic python script execution (pydrivers, pyconstraints, pynodes)\n");
224         printf ("  -P <filename>\tRun the given Python script (filename or Blender Text)\n");
225 #ifdef WIN32
226         printf ("  -R\t\tRegister .blend extension\n");
227 #endif
228         printf ("  -v\t\tPrint Blender version and exit\n");
229         printf ("  --\t\tEnds option processing.  Following arguments are \n");
230         printf ("    \t\t   passed unchanged.  Access via Python's sys.argv\n");
231         printf ("\nEnvironment Variables:\n");
232         printf ("  $HOME\t\t\tStore files such as .blender/ .B.blend .Bfs .Blog here.\n");
233         printf ("  $BLENDERPATH\tSystem directory to use for data files and scripts.\n");
234         printf ("              \tFor this build of blender the default BLENDERPATH is...\n");
235         printf ("              \t\"%s\"\n", blender_path);
236         printf ("              \tseting the $BLENDERPATH will override this\n");
237 #ifdef WIN32
238         printf ("  $TEMP\t\tStore temporary files here.\n");
239 #else
240         printf ("  $TMP or $TMPDIR\tStore temporary files here.\n");
241         printf ("  $BF_TIFF_LIB\t\tUse an alternative libtiff.so for loading tiff image files.\n");
242 #endif
243 #ifndef DISABLE_SDL
244         printf ("  $SDL_AUDIODRIVER\tLibSDL audio driver - alsa, esd, alsa, dma.\n");
245 #endif
246         printf ("  $IMAGEEDITOR\t\tImage editor executable, launch with the IKey from the file selector.\n");
247         printf ("  $WINEDITOR\t\tText editor executable, launch with the EKey from the file selector.\n");
248         printf ("  $PYTHONHOME\t\tPath to the python directory, eg. /usr/lib/python.\n");
249         printf ("\nNote: Arguments must be separated by white space. eg:\n");
250         printf ("    \"blender -ba test.blend\"\n");
251         printf ("  ...will ignore the 'a'\n");
252         printf ("    \"blender -b test.blend -f8\"\n");
253         printf ("  ...will ignore 8 because there is no space between the -f and the frame value\n");
254         printf ("Note: Arguments are executed in the order they are given. eg:\n");
255         printf ("    \"blender -b test.blend -f 1 -o /tmp\"\n");
256         printf ("  ...may not render to /tmp because '-f 1' renders before the output path is set\n");
257         printf ("    \"blender -b -o /tmp test.blend -f 1\"\n");
258         printf ("  ...may not render to /tmp because loading the blend file overwrites the output path that was set\n");
259         printf ("    \"blender -b test.blend -o /tmp -f 1\" works as expected.\n\n");
260 }
261
262
263 double PIL_check_seconds_timer(void);
264
265 /* XXX This was here to fix a crash when running python scripts
266  * with -P that used the screen.
267  *
268  * static void main_init_screen( void )
269 {
270         setscreen(G.curscreen);
271         
272         if(G.main->scene.first==0) {
273                 set_scene( add_scene("1") );
274         }
275 }*/
276
277 int main(int argc, char **argv)
278 {
279         SYS_SystemHandle syshandle;
280         bContext *C= CTX_create();
281         int a, i, stax, stay, sizx, sizy /*XXX, scr_init = 0*/;
282
283 #ifdef WITH_BINRELOC
284         br_init( NULL );
285 #endif
286
287         setCallbacks();
288 #ifdef __APPLE__
289                 /* patch to ignore argument finder gives us (pid?) */
290         if (argc==2 && strncmp(argv[1], "-psn_", 5)==0) {
291                 extern int GHOST_HACK_getFirstFile(char buf[]);
292                 static char firstfilebuf[512];
293
294                 argc= 1;
295
296                 if (GHOST_HACK_getFirstFile(firstfilebuf)) {
297                         argc= 2;
298                         argv[1]= firstfilebuf;
299                 }
300         }
301
302 #endif
303
304 #ifdef __FreeBSD__
305         fpsetmask(0);
306 #endif
307 #ifdef __linux__
308     #ifdef __alpha__
309         signal (SIGFPE, fpe_handler);
310     #endif
311 #endif
312 #if defined(__sgi)
313         signal (SIGFPE, fpe_handler);
314 #endif
315
316         // copy path to executable in bprogname. playanim and creting runtimes
317         // need this.
318
319         BLI_where_am_i(bprogname, argv[0]);
320         
321         {       /* override the hard coded blender path */
322                 char *blender_path_env = getenv("BLENDERPATH");
323                 if(blender_path_env)
324                         BLI_strncpy(blender_path, blender_path_env, sizeof(blender_path));
325         }
326         
327         RNA_init();
328         RE_engines_init();
329
330                 /* Hack - force inclusion of the plugin api functions,
331                  * see blenpluginapi:pluginapi.c
332                  */
333         pluginapi_force_ref();
334
335         init_nodesystem();
336         
337         initglobals();  /* blender.c */
338
339         syshandle = SYS_GetSystem();
340         GEN_init_messaging_system();
341
342         /* first test for background */
343
344         G.f |= G_DOSCRIPTLINKS; /* script links enabled by default */
345
346         for(a=1; a<argc; a++) {
347
348                 /* Handle unix and windows style help requests */
349                 if ((!strcmp(argv[a], "--help")) || (!strcmp(argv[a], "/?"))){
350                         print_help();
351                         exit(0);
352                 }
353
354                 /* end argument processing after -- */
355                 if (!strcmp( argv[a], "--")){
356                         a = argc;
357                         break;
358                 }
359
360                 /* Handle long version request */
361                 if (!strcmp(argv[a], "--version")){
362                         print_version();
363                         exit(0);
364                 }
365
366                 /* Handle -* switches */
367                 else if(argv[a][0] == '-') {
368                         switch(argv[a][1]) {
369                         case 'a': /* -b was not given, play an animation */
370                                 
371 #if !(defined(__APPLE__) && defined(GHOST_COCOA))
372                                 /* exception here, see below, it probably needs happens after qt init? */
373                                 libtiff_init();
374 #endif
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                                         if (BLI_strcasecmp(argv[a], "-noglsl") == 0)
488                                                 GPU_extensions_disable();
489                                         break;
490                                 }
491                         }
492                 }
493
494 #ifndef DISABLE_PYTHON          
495                 BPY_start_python(argc, argv);
496 #endif          
497
498                 WM_init(C);
499                 
500                 // XXX BRECHT SOLVE
501                 BLI_where_is_temp( btempdir, 1 ); /* call after loading the .B.blend so we can read U.tempdir */
502
503 #ifndef DISABLE_SDL
504         BLI_setenv("SDL_VIDEODRIVER", "dummy");
505 #ifdef __linux__
506         /* On linux the default SDL driver dma often would not play
507          * use alsa if none is set */
508         setenv("SDL_AUDIODRIVER", "alsa", 0);
509 #endif
510 #endif
511         }
512         else {
513 #ifndef DISABLE_PYTHON
514                 BPY_start_python(argc, argv);
515 #endif          
516                 BLI_where_is_temp( btempdir, 0 ); /* call after loading the .B.blend so we can read U.tempdir */
517         }
518 #ifndef DISABLE_PYTHON
519         /**
520          * NOTE: the U.pythondir string is NULL until WM_init() is executed,
521          * so we provide the BPY_ function below to append the user defined
522          * pythondir to Python's sys.path at this point.  Simply putting
523          * WM_init() before BPY_start_python() crashes Blender at startup.
524          * Update: now this function also inits the bpymenus, which also depend
525          * on U.pythondir.
526          */
527         BPY_post_start_python();
528
529         if(!G.background)
530                 BPY_run_ui_scripts(C, 0); /* dont need to reload the first time */
531 #endif
532         
533         CTX_py_init_set(C, 1);
534         if(!G.background)
535                 WM_keymap_init(C); /* after BPY_run_ui_scripts() */
536
537 #ifdef WITH_QUICKTIME
538
539         quicktime_init();
540
541 #endif /* WITH_QUICKTIME */
542
543 #if defined(__APPLE__) && defined(GHOST_COCOA)
544         /* libtiff is not used, Cocoa services are used instead for tiff I/O */
545         G.have_libtiff = 1;
546 #else
547         /* dynamically load libtiff, if available */
548         libtiff_init();
549         if (!G.have_libtiff && (G.f & G_DEBUG)) {
550                 printf("Unable to load: libtiff.\n");
551                 printf("Try setting the BF_TIFF_LIB environment variable if you want this support.\n");
552                 printf("Example: setenv BF_TIFF_LIB /usr/lib/libtiff.so\n");
553         }
554 #endif
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(MINAFRAME, 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 'E':
724                                 a++;
725                                 if (a < argc)
726                                 {
727                                         if (!strcmp(argv[a],"help"))
728                                         {
729                                                 RenderEngineType *type = NULL;
730
731                                                 for( type = R_engines.first; type; type = type->next )
732                                                 {
733                                                         printf("\t%s\n", type->idname);
734                                                 }
735                                                 exit(0);
736                                         }
737                                         else 
738                                         {
739                                                 if (CTX_data_scene(C)==NULL)
740                                                 {
741                                                         printf("\nError: no blend loaded. order the arguments so '-E ' is after the blend is loaded.\n");
742                                                 }
743                                                 else 
744                                                 {
745                                                         Scene *scene= CTX_data_scene(C);
746                                                         RenderData *rd = &scene->r;
747                                                         RenderEngineType *type = NULL;
748                                                         
749                                                         for( type = R_engines.first; type; type = type->next )
750                                                         {
751                                                                 if (!strcmp(argv[a],type->idname))
752                                                                 {
753                                                                         BLI_strncpy(rd->engine, type->idname, sizeof(rd->engine));
754                                                                 }
755                                                         }
756                                                 }
757                                         }
758                                 }
759                                 else
760                                 {
761                                         printf("\nEngine not specified.\n");
762                                 }
763                                 break;
764                         case 'F':
765                                 a++;
766                                 if (a < argc){
767                                         if (CTX_data_scene(C)==NULL) {
768                                                 printf("\nError: no blend loaded. order the arguments so '-F ' is after the blend is loaded.\n");
769                                         } else {
770                                                 Scene *scene= CTX_data_scene(C);
771                                                 if      (!strcmp(argv[a],"TGA")) scene->r.imtype = R_TARGA;
772                                                 else if (!strcmp(argv[a],"IRIS")) scene->r.imtype = R_IRIS;
773                                                 else if (!strcmp(argv[a],"HAMX")) scene->r.imtype = R_HAMX;
774 #ifdef WITH_DDS
775                                                 else if (!strcmp(argv[a],"DDS")) scene->r.imtype = R_DDS;
776 #endif
777                                                 else if (!strcmp(argv[a],"JPEG")) scene->r.imtype = R_JPEG90;
778                                                 else if (!strcmp(argv[a],"MOVIE")) scene->r.imtype = R_MOVIE;
779                                                 else if (!strcmp(argv[a],"IRIZ")) scene->r.imtype = R_IRIZ;
780                                                 else if (!strcmp(argv[a],"RAWTGA")) scene->r.imtype = R_RAWTGA;
781                                                 else if (!strcmp(argv[a],"AVIRAW")) scene->r.imtype = R_AVIRAW;
782                                                 else if (!strcmp(argv[a],"AVIJPEG")) scene->r.imtype = R_AVIJPEG;
783                                                 else if (!strcmp(argv[a],"PNG")) scene->r.imtype = R_PNG;
784                                                 else if (!strcmp(argv[a],"AVICODEC")) scene->r.imtype = R_AVICODEC;
785                                                 else if (!strcmp(argv[a],"QUICKTIME")) scene->r.imtype = R_QUICKTIME;
786                                                 else if (!strcmp(argv[a],"BMP")) scene->r.imtype = R_BMP;
787                                                 else if (!strcmp(argv[a],"HDR")) scene->r.imtype = R_RADHDR;
788                                                 else if (!strcmp(argv[a],"TIFF")) scene->r.imtype = R_TIFF;
789 #ifdef WITH_OPENEXR
790                                                 else if (!strcmp(argv[a],"EXR")) scene->r.imtype = R_OPENEXR;
791                                                 else if (!strcmp(argv[a],"MULTILAYER")) scene->r.imtype = R_MULTILAYER;
792 #endif
793                                                 else if (!strcmp(argv[a],"MPEG")) scene->r.imtype = R_FFMPEG;
794                                                 else if (!strcmp(argv[a],"FRAMESERVER")) scene->r.imtype = R_FRAMESERVER;
795                                                 else if (!strcmp(argv[a],"CINEON")) scene->r.imtype = R_CINEON;
796                                                 else if (!strcmp(argv[a],"DPX")) scene->r.imtype = R_DPX;
797 #if WITH_OPENJPEG
798                                                 else if (!strcmp(argv[a],"JP2")) scene->r.imtype = R_JP2;
799 #endif
800                                                 else printf("\nError: Format from '-F' not known or not compiled in this release.\n");
801                                         }
802                                 } else {
803                                         printf("\nError: no blend loaded. cannot use '-x'.\n");
804                                 }
805                                 break;
806                                 
807                         case 't':
808                                 a++;
809                                 if (a < argc) {
810                                         if(G.background) {
811                                                 RE_set_max_threads(atoi(argv[a]));
812                                         } else {
813                                                 printf("Warning: threads can only be set in background mode\n");
814                                         }
815                                 } else {
816                                         printf("\nError: you must specify a number of threads between 0 and 8 '-t '.\n");
817                                 }
818                                 break;
819                         case 'x': /* extension */
820                                 a++;
821                                 if (a < argc) {
822                                         if (CTX_data_scene(C)) {
823                                                 Scene *scene= CTX_data_scene(C);
824                                                 if (argv[a][0] == '0') {
825                                                         scene->r.scemode &= ~R_EXTENSION;
826                                                 } else if (argv[a][0] == '1') {
827                                                         scene->r.scemode |= R_EXTENSION;
828                                                 } else {
829                                                         printf("\nError: Use '-x 1' or '-x 0' To set the extension option.\n");
830                                                 }
831                                         } else {
832                                                 printf("\nError: no blend loaded. order the arguments so '-o ' is after '-x '.\n");
833                                         }
834                                 } else {
835                                         printf("\nError: you must specify a path after '- '.\n");
836                                 }
837                                 break;
838                         }
839                 }
840                 else {
841                         
842                         /* Make the path absolute because its needed for relative linked blends to be found */
843                         char filename[FILE_MAXDIR + FILE_MAXFILE];
844                         
845                         BLI_strncpy(filename, argv[a], sizeof(filename));
846                         BLI_convertstringcwd(filename);
847                         
848                         if (G.background) {
849                                 int retval = BKE_read_file(C, argv[a], NULL, NULL);
850                                 
851                                 /*we successfully loaded a blend file, get sure that
852                                 pointcache works */
853                                 if (retval!=0) G.relbase_valid = 1;
854
855                                 /* happens for the UI on file reading too (huh? (ton))*/
856 // XXX                  BKE_reset_undo();
857 //                              BKE_write_undo("original");     /* save current state */
858                         } else {
859                                 /* we are not running in background mode here, but start blender in UI mode with 
860                                    a file - this should do everything a 'load file' does */
861                                 WM_read_file(C, filename, NULL);
862                         }
863                 }
864         }
865         
866         if(G.background) {
867                 /* actually incorrect, but works for now (ton) */
868                 WM_exit(C);
869         }
870
871
872         WM_main(C);
873         
874         /*XXX if (scr_init==0) {
875                 main_init_screen();
876         }
877         
878         screenmain();*/ /* main display loop */
879
880         return 0;
881 } /* end of int main(argc,argv) */
882
883 static void error_cb(char *err)
884 {
885         
886         printf("%s\n", err);    /* XXX do this in WM too */
887 }
888
889 static void mem_error_cb(char *errorStr)
890 {
891         fputs(errorStr, stderr);
892         fflush(stderr);
893 }
894
895 static void setCallbacks(void)
896 {
897         /* Error output from the alloc routines: */
898         MEM_set_error_callback(mem_error_cb);
899
900
901         /* BLI_blenlib: */
902
903         BLI_setErrorCallBack(error_cb); /* */
904 // XXX  BLI_setInterruptCallBack(blender_test_break);
905
906 }