Merge -c 25001,25310,25311,25638,25746 from COLLADA branch into trunk.
[blender-staging.git] / source / creator / creator.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29 #include <stdlib.h>
30 #include <string.h>
31
32
33 /* for setuid / getuid */
34 #ifdef __sgi
35 #include <sys/types.h>
36 #include <unistd.h>
37 #endif
38
39 /* This little block needed for linking to Blender... */
40
41 #include "MEM_guardedalloc.h"
42
43 #ifdef WIN32
44 #include "BLI_winstuff.h"
45 #endif
46
47 #include "GEN_messaging.h"
48
49 #include "DNA_ID.h"
50 #include "DNA_scene_types.h"
51
52 #include "BLI_blenlib.h"
53
54 #include "BKE_utildefines.h"
55 #include "BKE_blender.h"
56 #include "BKE_context.h"
57 #include "BKE_font.h"
58 #include "BKE_global.h"
59 #include "BKE_main.h"
60 #include "BKE_material.h"
61 #include "BKE_packedFile.h"
62 #include "BKE_scene.h"
63 #include "BKE_node.h"
64 #include "BKE_report.h"
65 #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                 /* background mode options */
518                 for(a=1; a<argc; a++) {
519                         if(argv[a][0] == '-') {
520                                 switch(argv[a][1]) {
521                                 case 'd':
522                                         G.f |= G_DEBUG;         /* std output printf's */
523                                         printf ("Blender %d.%02d (sub %d) Build\n", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
524                                         MEM_set_memory_debug();
525 #ifdef NAN_BUILDINFO
526                                         printf("Build: %s %s %s %s\n", build_date, build_time, build_platform, build_type);
527
528 #endif // NAN_BUILDINFO
529                                         for (i = 0; i < argc; i++) {
530                                                 printf("argv[%d] = %s\n", i, argv[i]);
531                                         }
532                                         break;
533                                 case 'n':
534                                 case 'N':
535                                         if (BLI_strcasecmp(argv[a], "-noaudio") == 0)
536                                                 sound_disable();
537                                         break;
538                                 }
539                         }
540                 }
541
542                 WM_init(C);
543
544 #ifndef DISABLE_PYTHON
545                 BPY_set_context(C); /* necessary evil */
546                 BPY_start_python(argc, argv);
547                 BPY_load_user_modules(C);
548 #endif          
549                 BLI_where_is_temp( btempdir, 0 ); /* call after loading the .B.blend so we can read U.tempdir */
550         }
551 #ifndef DISABLE_PYTHON
552         /**
553          * NOTE: the U.pythondir string is NULL until WM_init() is executed,
554          * so we provide the BPY_ function below to append the user defined
555          * pythondir to Python's sys.path at this point.  Simply putting
556          * WM_init() before BPY_start_python() crashes Blender at startup.
557          * Update: now this function also inits the bpymenus, which also depend
558          * on U.pythondir.
559          */
560         
561         // TODO - U.pythondir
562
563 #endif
564         
565         CTX_py_init_set(C, 1);
566         WM_keymap_init(C);
567
568 #ifdef WITH_QUICKTIME
569
570         quicktime_init();
571
572 #endif /* WITH_QUICKTIME */
573
574         /* dynamically load libtiff, if available */
575         libtiff_init();
576         if (!G.have_libtiff && (G.f & G_DEBUG)) {
577                 printf("Unable to load: libtiff.\n");
578                 printf("Try setting the BF_TIFF_LIB environment variable if you want this support.\n");
579                 printf("Example: setenv BF_TIFF_LIB /usr/lib/libtiff.so\n");
580         }
581
582         /* OK we are ready for it */
583
584         for(a=1; a<argc; a++) {
585                 if (G.afbreek==1) break;
586
587                 if(argv[a][0] == '-') {
588                         switch(argv[a][1]) {
589                         case '-':  /* -- ends argument processing */
590                                 a = argc;
591                                 break;
592                                 
593                         case 'p':       /* prefsize */
594                                 a+= 4;
595                                 break;
596
597                         case 'g':
598                                 {
599                                 /**
600                                 gameengine parameters are automaticly put into system
601                                 -g [paramname = value]
602                                 -g [boolparamname]
603                                 example:
604                                 -g novertexarrays
605                                 -g maxvertexarraysize = 512
606                                 */
607
608                                         if(++a < argc)
609                                         {
610                                                 char* paramname = argv[a];
611                                                 /* check for single value versus assignment */
612                                                 if (a+1 < argc && (*(argv[a+1]) == '='))
613                                                 {
614                                                         a++;
615                                                         if (a+1 < argc)
616                                                         {
617                                                                 a++;
618                                                                 /* assignment */
619                                                                 SYS_WriteCommandLineString(syshandle,paramname,argv[a]);
620                                                         }  else
621                                                         {
622                                                                 printf("error: argument assignment (%s) without value.\n",paramname);
623                                                         }
624                                                         /* name arg eaten */
625
626                                                 } else
627                                                 {
628                                                         SYS_WriteCommandLineInt(syshandle,argv[a],1);
629
630                                                         /* doMipMap */
631                                                         if (!strcmp(argv[a],"nomipmap"))
632                                                         {
633                                                                 GPU_set_mipmap(0); //doMipMap = 0;
634                                                         }
635                                                         /* linearMipMap */
636                                                         if (!strcmp(argv[a],"linearmipmap"))
637                                                         {
638                                                                 GPU_set_linear_mipmap(1); //linearMipMap = 1;
639                                                         }
640
641
642                                                 } /* if (*(argv[a+1]) == '=') */
643                                         } /*    if(++a < argc)  */
644                                         break;
645                                 }
646                         case 'f':
647                                 a++;
648                                 if (CTX_data_scene(C)) {
649                                         Scene *scene= CTX_data_scene(C);
650                                         
651                                         if (a < argc) {
652                                                 int frame = atoi(argv[a]);
653                                                 Render *re = RE_NewRender(scene->id.name);
654                                                 ReportList reports;
655
656                                                 BKE_reports_init(&reports, RPT_PRINT);
657
658                                                 frame = MIN2(MAXFRAME, MAX2(MINAFRAME, frame));
659                                                 
660                                                 RE_BlenderAnim(re, scene, frame, frame, scene->r.frame_step, &reports);
661                                         }
662                                 } else {
663                                         printf("\nError: no blend loaded. cannot use '-f'.\n");
664                                 }
665                                 break;
666                         case 'a':
667                                 if (CTX_data_scene(C)) {
668                                         Scene *scene= CTX_data_scene(C);
669                                         Render *re= RE_NewRender(scene->id.name);
670                                         ReportList reports;
671                                         BKE_reports_init(&reports, RPT_PRINT);
672                                         RE_BlenderAnim(re, scene, scene->r.sfra, scene->r.efra, scene->r.frame_step, &reports);
673                                 } else {
674                                         printf("\nError: no blend loaded. cannot use '-a'.\n");
675                                 }
676                                 break;
677                         case 'S':
678                                 if(++a < argc) {
679                                         set_scene_name(argv[a]);
680                                 }
681                                 break;
682                         case 's':
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.sfra) = MIN2(MAXFRAME, MAX2(1, frame));
689                                         }
690                                 } else {
691                                         printf("\nError: no blend loaded. cannot use '-s'.\n");
692                                 }
693                                 break;
694                         case 'e':
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.efra) = MIN2(MAXFRAME, MAX2(1, frame));
701                                         }
702                                 } else {
703                                         printf("\nError: no blend loaded. cannot use '-e'.\n");
704                                 }
705                                 break;
706                         case 'j':
707                                 a++;
708                                 if (CTX_data_scene(C)) {
709                                         Scene *scene= CTX_data_scene(C);
710                                         if (a < argc) {
711                                                 int frame = atoi(argv[a]);
712                                                 (scene->r.frame_step) = MIN2(MAXFRAME, MAX2(1, frame));
713                                         }
714                                 } else {
715                                         printf("\nError: no blend loaded. cannot use '-j'.\n");
716                                 }
717                                 break;
718                         case 'P':
719
720 #ifndef DISABLE_PYTHON
721                                 //XXX 
722                                 // FOR TESTING ONLY
723                                 a++;
724                                 BPY_run_python_script(C, argv[a], NULL, NULL); // use reports?
725 #if 0
726                                 a++;
727                                 if (a < argc) {
728                                         /* If we're not running in background mode, then give python a valid screen */
729                                         if ((G.background==0) && (scr_init==0)) {
730                                                 main_init_screen();
731                                                 scr_init = 1;
732                                         }
733                                         BPY_run_python_script(C, argv[a], NULL, NULL); // use reports?
734                                 }
735                                 else printf("\nError: you must specify a Python script after '-P '.\n");
736 #endif
737 #else
738                                 printf("This blender was built without python support\n");
739 #endif /* DISABLE_PYTHON */
740                                 break;
741                         case 'o':
742                                 a++;
743                                 if (a < argc){
744                                         if (CTX_data_scene(C)) {
745                                                 Scene *scene= CTX_data_scene(C);
746                                                 BLI_strncpy(scene->r.pic, argv[a], FILE_MAXDIR);
747                                         } else {
748                                                 printf("\nError: no blend loaded. cannot use '-o'.\n");
749                                         }
750                                 } else {
751                                         printf("\nError: you must specify a path after '-o '.\n");
752                                 }
753                                 break;
754                         case 'E':
755                                 a++;
756                                 if (a < argc)
757                                 {
758                                         if (!strcmp(argv[a],"help"))
759                                         {
760                                                 RenderEngineType *type = NULL;
761
762                                                 for( type = R_engines.first; type; type = type->next )
763                                                 {
764                                                         printf("\t%s\n", type->idname);
765                                                 }
766                                                 exit(0);
767                                         }
768                                         else 
769                                         {
770                                                 if (CTX_data_scene(C)==NULL)
771                                                 {
772                                                         printf("\nError: no blend loaded. order the arguments so '-E ' is after the blend is loaded.\n");
773                                                 }
774                                                 else 
775                                                 {
776                                                         Scene *scene= CTX_data_scene(C);
777                                                         RenderData *rd = &scene->r;
778                                                         RenderEngineType *type = NULL;
779                                                         
780                                                         for( type = R_engines.first; type; type = type->next )
781                                                         {
782                                                                 if (!strcmp(argv[a],type->idname))
783                                                                 {
784                                                                         BLI_strncpy(rd->engine, type->idname, sizeof(rd->engine));
785                                                                 }
786                                                         }
787                                                 }
788                                         }
789                                 }
790                                 else
791                                 {
792                                         printf("\nEngine not specified.\n");
793                                 }
794                                 break;
795                         case 'F':
796                                 a++;
797                                 if (a < argc){
798                                         if (CTX_data_scene(C)==NULL) {
799                                                 printf("\nError: no blend loaded. order the arguments so '-F ' is after the blend is loaded.\n");
800                                         } else {
801                                                 Scene *scene= CTX_data_scene(C);
802                                                 if      (!strcmp(argv[a],"TGA")) scene->r.imtype = R_TARGA;
803                                                 else if (!strcmp(argv[a],"IRIS")) scene->r.imtype = R_IRIS;
804                                                 else if (!strcmp(argv[a],"HAMX")) scene->r.imtype = R_HAMX;
805 #ifdef WITH_DDS
806                                                 else if (!strcmp(argv[a],"DDS")) scene->r.imtype = R_DDS;
807 #endif
808                                                 else if (!strcmp(argv[a],"JPEG")) scene->r.imtype = R_JPEG90;
809                                                 else if (!strcmp(argv[a],"MOVIE")) scene->r.imtype = R_MOVIE;
810                                                 else if (!strcmp(argv[a],"IRIZ")) scene->r.imtype = R_IRIZ;
811                                                 else if (!strcmp(argv[a],"RAWTGA")) scene->r.imtype = R_RAWTGA;
812                                                 else if (!strcmp(argv[a],"AVIRAW")) scene->r.imtype = R_AVIRAW;
813                                                 else if (!strcmp(argv[a],"AVIJPEG")) scene->r.imtype = R_AVIJPEG;
814                                                 else if (!strcmp(argv[a],"PNG")) scene->r.imtype = R_PNG;
815                                                 else if (!strcmp(argv[a],"AVICODEC")) scene->r.imtype = R_AVICODEC;
816                                                 else if (!strcmp(argv[a],"QUICKTIME")) scene->r.imtype = R_QUICKTIME;
817                                                 else if (!strcmp(argv[a],"BMP")) scene->r.imtype = R_BMP;
818                                                 else if (!strcmp(argv[a],"HDR")) scene->r.imtype = R_RADHDR;
819                                                 else if (!strcmp(argv[a],"TIFF")) scene->r.imtype = R_TIFF;
820 #ifdef WITH_OPENEXR
821                                                 else if (!strcmp(argv[a],"EXR")) scene->r.imtype = R_OPENEXR;
822                                                 else if (!strcmp(argv[a],"MULTILAYER")) scene->r.imtype = R_MULTILAYER;
823 #endif
824                                                 else if (!strcmp(argv[a],"MPEG")) scene->r.imtype = R_FFMPEG;
825                                                 else if (!strcmp(argv[a],"FRAMESERVER")) scene->r.imtype = R_FRAMESERVER;
826                                                 else if (!strcmp(argv[a],"CINEON")) scene->r.imtype = R_CINEON;
827                                                 else if (!strcmp(argv[a],"DPX")) scene->r.imtype = R_DPX;
828 #if WITH_OPENJPEG
829                                                 else if (!strcmp(argv[a],"JP2")) scene->r.imtype = R_JP2;
830 #endif
831                                                 else printf("\nError: Format from '-F' not known or not compiled in this release.\n");
832                                         }
833                                 } else {
834                                         printf("\nError: no blend loaded. cannot use '-x'.\n");
835                                 }
836                                 break;
837                                 
838                         case 't':
839                                 a++;
840                                 if (a < argc) {
841                                         if(G.background) {
842                                                 RE_set_max_threads(atoi(argv[a]));
843                                         } else {
844                                                 printf("Warning: threads can only be set in background mode\n");
845                                         }
846                                 } else {
847                                         printf("\nError: you must specify a number of threads between 0 and 8 '-t '.\n");
848                                 }
849                                 break;
850                         case 'x': /* extension */
851                                 a++;
852                                 if (a < argc) {
853                                         if (CTX_data_scene(C)) {
854                                                 Scene *scene= CTX_data_scene(C);
855                                                 if (argv[a][0] == '0') {
856                                                         scene->r.scemode &= ~R_EXTENSION;
857                                                 } else if (argv[a][0] == '1') {
858                                                         scene->r.scemode |= R_EXTENSION;
859                                                 } else {
860                                                         printf("\nError: Use '-x 1' or '-x 0' To set the extension option.\n");
861                                                 }
862                                         } else {
863                                                 printf("\nError: no blend loaded. order the arguments so '-o ' is after '-x '.\n");
864                                         }
865                                 } else {
866                                         printf("\nError: you must specify a path after '- '.\n");
867                                 }
868                                 break;
869                         }
870                 }
871                 else {
872                         
873                         /* Make the path absolute because its needed for relative linked blends to be found */
874                         char filename[FILE_MAXDIR + FILE_MAXFILE];
875                         
876                         BLI_strncpy(filename, argv[a], sizeof(filename));
877                         BLI_convertstringcwd(filename);
878                         
879                         if (G.background) {
880                                 int retval = BKE_read_file(C, argv[a], NULL, NULL);
881                                 
882                                 /*we successfully loaded a blend file, get sure that
883                                 pointcache works */
884                                 if (retval!=0) {
885                                         CTX_wm_manager_set(C, NULL); /* remove wm to force check */
886                                         WM_check(C);
887                                         G.relbase_valid = 1;
888                                 }
889
890                                 /* happens for the UI on file reading too (huh? (ton))*/
891 // XXX                  BKE_reset_undo();
892 //                              BKE_write_undo("original");     /* save current state */
893                         } else {
894                                 /* we are not running in background mode here, but start blender in UI mode with 
895                                    a file - this should do everything a 'load file' does */
896                                 WM_read_file(C, filename, NULL);
897                         }
898
899                         file_loaded = 1;
900                 }
901         }
902         
903         if(G.background) {
904                 /* actually incorrect, but works for now (ton) */
905                 WM_exit(C);
906         }
907
908         if(!G.background && !file_loaded)
909                 WM_init_splash(C);
910
911         WM_main(C);
912
913
914         /*XXX if (scr_init==0) {
915                 main_init_screen();
916         }
917         
918         screenmain();*/ /* main display loop */
919
920         return 0;
921 } /* end of int main(argc,argv) */
922
923 static void error_cb(char *err)
924 {
925         
926         printf("%s\n", err);    /* XXX do this in WM too */
927 }
928
929 static void mem_error_cb(char *errorStr)
930 {
931         fputs(errorStr, stderr);
932         fflush(stderr);
933 }
934
935 static void setCallbacks(void)
936 {
937         /* Error output from the alloc routines: */
938         MEM_set_error_callback(mem_error_cb);
939
940
941         /* BLI_blenlib: */
942
943         BLI_setErrorCallBack(error_cb); /* */
944 // XXX  BLI_setInterruptCallBack(blender_test_break);
945
946 }