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