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