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