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