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