ok, apparently didn't commit this either. apparently includes a merge with 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 #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*/, file_loaded= 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                 WM_init(C);
519
520 #ifndef DISABLE_PYTHON          
521                 BPY_set_context(C); /* necessary evil */
522                 BPY_start_python(argc, argv);
523                 BPY_load_user_modules(C);
524 #endif          
525
526                 // XXX BRECHT SOLVE
527                 BLI_where_is_temp( btempdir, 1 ); /* call after loading the .B.blend so we can read U.tempdir */
528
529 #ifndef DISABLE_SDL
530         BLI_setenv("SDL_VIDEODRIVER", "dummy");
531 #ifdef __linux__
532         /* On linux the default SDL driver dma often would not play
533          * use alsa if none is set */
534         setenv("SDL_AUDIODRIVER", "alsa", 0);
535 #endif
536 #endif
537         }
538         else {
539                 for(a=1; a<argc; a++) {
540                         if(argv[a][0] == '-') {
541                                 switch(argv[a][1]) {
542                                 case 'd':
543                                         G.f |= G_DEBUG;         /* std output printf's */
544                                         printf ("Blender %d.%02d (sub %d) Build\n", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
545                                         MEM_set_memory_debug();
546 #ifdef NAN_BUILDINFO
547                                         printf("Build: %s %s %s %s\n", build_date, build_time, build_platform, build_type);
548
549 #endif // NAN_BUILDINFO
550                                         for (i = 0; i < argc; i++) {
551                                                 printf("argv[%d] = %s\n", i, argv[i]);
552                                         }
553                                         break;
554                                 }
555                         }
556                 }
557
558                 WM_init(C);
559
560 #ifndef DISABLE_PYTHON
561                 BPY_set_context(C); /* necessary evil */
562                 BPY_start_python(argc, argv);
563                 BPY_load_user_modules(C);
564 #endif          
565                 BLI_where_is_temp( btempdir, 0 ); /* call after loading the .B.blend so we can read U.tempdir */
566         }
567 #ifndef DISABLE_PYTHON
568         /**
569          * NOTE: the U.pythondir string is NULL until WM_init() is executed,
570          * so we provide the BPY_ function below to append the user defined
571          * pythondir to Python's sys.path at this point.  Simply putting
572          * WM_init() before BPY_start_python() crashes Blender at startup.
573          * Update: now this function also inits the bpymenus, which also depend
574          * on U.pythondir.
575          */
576
577         // TODO - U.pythondir
578
579 #endif
580         
581         CTX_py_init_set(C, 1);
582         WM_keymap_init(C);
583
584 #ifdef WITH_QUICKTIME
585
586         quicktime_init();
587
588 #endif /* WITH_QUICKTIME */
589
590         /* dynamically load libtiff, if available */
591         libtiff_init();
592         if (!G.have_libtiff && (G.f & G_DEBUG)) {
593                 printf("Unable to load: libtiff.\n");
594                 printf("Try setting the BF_TIFF_LIB environment variable if you want this support.\n");
595                 printf("Example: setenv BF_TIFF_LIB /usr/lib/libtiff.so\n");
596         }
597
598         /* OK we are ready for it */
599
600         for(a=1; a<argc; a++) {
601                 if (G.afbreek==1) break;
602
603                 if(argv[a][0] == '-') {
604                         switch(argv[a][1]) {
605                         case '-':  /* -- ends argument processing */
606                                 a = argc;
607                                 break;
608                                 
609                         case 'p':       /* prefsize */
610                                 a+= 4;
611                                 break;
612
613                         case 'g':
614                                 {
615                                 /**
616                                 gameengine parameters are automaticly put into system
617                                 -g [paramname = value]
618                                 -g [boolparamname]
619                                 example:
620                                 -g novertexarrays
621                                 -g maxvertexarraysize = 512
622                                 */
623
624                                         if(++a < argc)
625                                         {
626                                                 char* paramname = argv[a];
627                                                 /* check for single value versus assignment */
628                                                 if (a+1 < argc && (*(argv[a+1]) == '='))
629                                                 {
630                                                         a++;
631                                                         if (a+1 < argc)
632                                                         {
633                                                                 a++;
634                                                                 /* assignment */
635                                                                 SYS_WriteCommandLineString(syshandle,paramname,argv[a]);
636                                                         }  else
637                                                         {
638                                                                 printf("error: argument assignment (%s) without value.\n",paramname);
639                                                         }
640                                                         /* name arg eaten */
641
642                                                 } else
643                                                 {
644                                                         SYS_WriteCommandLineInt(syshandle,argv[a],1);
645
646                                                         /* doMipMap */
647                                                         if (!strcmp(argv[a],"nomipmap"))
648                                                         {
649                                                                 GPU_set_mipmap(0); //doMipMap = 0;
650                                                         }
651                                                         /* linearMipMap */
652                                                         if (!strcmp(argv[a],"linearmipmap"))
653                                                         {
654                                                                 GPU_set_linear_mipmap(1); //linearMipMap = 1;
655                                                         }
656
657
658                                                 } /* if (*(argv[a+1]) == '=') */
659                                         } /*    if(++a < argc)  */
660                                         break;
661                                 }
662                         case 'f':
663                                 a++;
664                                 if (CTX_data_scene(C)) {
665                                         Scene *scene= CTX_data_scene(C);
666                                         
667                                         if (a < argc) {
668                                                 int frame = atoi(argv[a]);
669                                                 Render *re = RE_NewRender(scene->id.name);
670
671                                                 frame = MIN2(MAXFRAME, MAX2(MINAFRAME, frame));
672                                                 
673                                                 RE_BlenderAnim(re, scene, frame, frame, scene->r.frame_step);
674                                         }
675                                 } else {
676                                         printf("\nError: no blend loaded. cannot use '-f'.\n");
677                                 }
678                                 break;
679                         case 'a':
680                                 if (CTX_data_scene(C)) {
681                                         Scene *scene= CTX_data_scene(C);
682                                         Render *re= RE_NewRender(scene->id.name);
683                                         RE_BlenderAnim(re, scene, scene->r.sfra, scene->r.efra, scene->r.frame_step);
684                                 } else {
685                                         printf("\nError: no blend loaded. cannot use '-a'.\n");
686                                 }
687                                 break;
688                         case 'S':
689                                 if(++a < argc) {
690                                         set_scene_name(argv[a]);
691                                 }
692                                 break;
693                         case 's':
694                                 a++;
695                                 if (CTX_data_scene(C)) {
696                                         Scene *scene= CTX_data_scene(C);
697                                         if (a < argc) {
698                                                 int frame = atoi(argv[a]);
699                                                 (scene->r.sfra) = MIN2(MAXFRAME, MAX2(1, frame));
700                                         }
701                                 } else {
702                                         printf("\nError: no blend loaded. cannot use '-s'.\n");
703                                 }
704                                 break;
705                         case 'e':
706                                 a++;
707                                 if (CTX_data_scene(C)) {
708                                         Scene *scene= CTX_data_scene(C);
709                                         if (a < argc) {
710                                                 int frame = atoi(argv[a]);
711                                                 (scene->r.efra) = MIN2(MAXFRAME, MAX2(1, frame));
712                                         }
713                                 } else {
714                                         printf("\nError: no blend loaded. cannot use '-e'.\n");
715                                 }
716                                 break;
717                         case 'j':
718                                 a++;
719                                 if (CTX_data_scene(C)) {
720                                         Scene *scene= CTX_data_scene(C);
721                                         if (a < argc) {
722                                                 int frame = atoi(argv[a]);
723                                                 (scene->r.frame_step) = MIN2(MAXFRAME, MAX2(1, frame));
724                                         }
725                                 } else {
726                                         printf("\nError: no blend loaded. cannot use '-j'.\n");
727                                 }
728                                 break;
729                         case 'P':
730
731 #ifndef DISABLE_PYTHON
732                                 //XXX 
733                                 // FOR TESTING ONLY
734                                 a++;
735                                 BPY_run_python_script(C, argv[a], NULL, NULL); // use reports?
736 #if 0
737                                 a++;
738                                 if (a < argc) {
739                                         /* If we're not running in background mode, then give python a valid screen */
740                                         if ((G.background==0) && (scr_init==0)) {
741                                                 main_init_screen();
742                                                 scr_init = 1;
743                                         }
744                                         BPY_run_python_script(C, argv[a], NULL, NULL); // use reports?
745                                 }
746                                 else printf("\nError: you must specify a Python script after '-P '.\n");
747 #endif
748 #else
749                                 printf("This blender was built without python support\n");
750 #endif /* DISABLE_PYTHON */
751                                 break;
752                         case 'o':
753                                 a++;
754                                 if (a < argc){
755                                         if (CTX_data_scene(C)) {
756                                                 Scene *scene= CTX_data_scene(C);
757                                                 BLI_strncpy(scene->r.pic, argv[a], FILE_MAXDIR);
758                                         } else {
759                                                 printf("\nError: no blend loaded. cannot use '-o'.\n");
760                                         }
761                                 } else {
762                                         printf("\nError: you must specify a path after '-o '.\n");
763                                 }
764                                 break;
765                         case 'E':
766                                 a++;
767                                 if (a < argc)
768                                 {
769                                         if (!strcmp(argv[a],"help"))
770                                         {
771                                                 RenderEngineType *type = NULL;
772
773                                                 for( type = R_engines.first; type; type = type->next )
774                                                 {
775                                                         printf("\t%s\n", type->idname);
776                                                 }
777                                                 exit(0);
778                                         }
779                                         else 
780                                         {
781                                                 if (CTX_data_scene(C)==NULL)
782                                                 {
783                                                         printf("\nError: no blend loaded. order the arguments so '-E ' is after the blend is loaded.\n");
784                                                 }
785                                                 else 
786                                                 {
787                                                         Scene *scene= CTX_data_scene(C);
788                                                         RenderData *rd = &scene->r;
789                                                         RenderEngineType *type = NULL;
790                                                         
791                                                         for( type = R_engines.first; type; type = type->next )
792                                                         {
793                                                                 if (!strcmp(argv[a],type->idname))
794                                                                 {
795                                                                         BLI_strncpy(rd->engine, type->idname, sizeof(rd->engine));
796                                                                 }
797                                                         }
798                                                 }
799                                         }
800                                 }
801                                 else
802                                 {
803                                         printf("\nEngine not specified.\n");
804                                 }
805                                 break;
806                         case 'F':
807                                 a++;
808                                 if (a < argc){
809                                         if (CTX_data_scene(C)==NULL) {
810                                                 printf("\nError: no blend loaded. order the arguments so '-F ' is after the blend is loaded.\n");
811                                         } else {
812                                                 Scene *scene= CTX_data_scene(C);
813                                                 if      (!strcmp(argv[a],"TGA")) scene->r.imtype = R_TARGA;
814                                                 else if (!strcmp(argv[a],"IRIS")) scene->r.imtype = R_IRIS;
815                                                 else if (!strcmp(argv[a],"HAMX")) scene->r.imtype = R_HAMX;
816 #ifdef WITH_DDS
817                                                 else if (!strcmp(argv[a],"DDS")) scene->r.imtype = R_DDS;
818 #endif
819                                                 else if (!strcmp(argv[a],"JPEG")) scene->r.imtype = R_JPEG90;
820                                                 else if (!strcmp(argv[a],"MOVIE")) scene->r.imtype = R_MOVIE;
821                                                 else if (!strcmp(argv[a],"IRIZ")) scene->r.imtype = R_IRIZ;
822                                                 else if (!strcmp(argv[a],"RAWTGA")) scene->r.imtype = R_RAWTGA;
823                                                 else if (!strcmp(argv[a],"AVIRAW")) scene->r.imtype = R_AVIRAW;
824                                                 else if (!strcmp(argv[a],"AVIJPEG")) scene->r.imtype = R_AVIJPEG;
825                                                 else if (!strcmp(argv[a],"PNG")) scene->r.imtype = R_PNG;
826                                                 else if (!strcmp(argv[a],"AVICODEC")) scene->r.imtype = R_AVICODEC;
827                                                 else if (!strcmp(argv[a],"QUICKTIME")) scene->r.imtype = R_QUICKTIME;
828                                                 else if (!strcmp(argv[a],"BMP")) scene->r.imtype = R_BMP;
829                                                 else if (!strcmp(argv[a],"HDR")) scene->r.imtype = R_RADHDR;
830                                                 else if (!strcmp(argv[a],"TIFF")) scene->r.imtype = R_TIFF;
831 #ifdef WITH_OPENEXR
832                                                 else if (!strcmp(argv[a],"EXR")) scene->r.imtype = R_OPENEXR;
833                                                 else if (!strcmp(argv[a],"MULTILAYER")) scene->r.imtype = R_MULTILAYER;
834 #endif
835                                                 else if (!strcmp(argv[a],"MPEG")) scene->r.imtype = R_FFMPEG;
836                                                 else if (!strcmp(argv[a],"FRAMESERVER")) scene->r.imtype = R_FRAMESERVER;
837                                                 else if (!strcmp(argv[a],"CINEON")) scene->r.imtype = R_CINEON;
838                                                 else if (!strcmp(argv[a],"DPX")) scene->r.imtype = R_DPX;
839 #if WITH_OPENJPEG
840                                                 else if (!strcmp(argv[a],"JP2")) scene->r.imtype = R_JP2;
841 #endif
842                                                 else printf("\nError: Format from '-F' not known or not compiled in this release.\n");
843                                         }
844                                 } else {
845                                         printf("\nError: no blend loaded. cannot use '-x'.\n");
846                                 }
847                                 break;
848                                 
849                         case 't':
850                                 a++;
851                                 if (a < argc) {
852                                         if(G.background) {
853                                                 RE_set_max_threads(atoi(argv[a]));
854                                         } else {
855                                                 printf("Warning: threads can only be set in background mode\n");
856                                         }
857                                 } else {
858                                         printf("\nError: you must specify a number of threads between 0 and 8 '-t '.\n");
859                                 }
860                                 break;
861                         case 'x': /* extension */
862                                 a++;
863                                 if (a < argc) {
864                                         if (CTX_data_scene(C)) {
865                                                 Scene *scene= CTX_data_scene(C);
866                                                 if (argv[a][0] == '0') {
867                                                         scene->r.scemode &= ~R_EXTENSION;
868                                                 } else if (argv[a][0] == '1') {
869                                                         scene->r.scemode |= R_EXTENSION;
870                                                 } else {
871                                                         printf("\nError: Use '-x 1' or '-x 0' To set the extension option.\n");
872                                                 }
873                                         } else {
874                                                 printf("\nError: no blend loaded. order the arguments so '-o ' is after '-x '.\n");
875                                         }
876                                 } else {
877                                         printf("\nError: you must specify a path after '- '.\n");
878                                 }
879                                 break;
880                         }
881                 }
882                 else {
883                         
884                         /* Make the path absolute because its needed for relative linked blends to be found */
885                         char filename[FILE_MAXDIR + FILE_MAXFILE];
886                         
887                         BLI_strncpy(filename, argv[a], sizeof(filename));
888                         BLI_convertstringcwd(filename);
889                         
890                         if (G.background) {
891                                 int retval = BKE_read_file(C, argv[a], NULL, NULL);
892                                 
893                                 /*we successfully loaded a blend file, get sure that
894                                 pointcache works */
895                                 if (retval!=0) {
896                                         CTX_wm_manager_set(C, NULL); /* remove wm to force check */
897                                         WM_check(C);
898                                         G.relbase_valid = 1;
899                                 }
900
901                                 /* happens for the UI on file reading too (huh? (ton))*/
902 // XXX                  BKE_reset_undo();
903 //                              BKE_write_undo("original");     /* save current state */
904                         } else {
905                                 /* we are not running in background mode here, but start blender in UI mode with 
906                                    a file - this should do everything a 'load file' does */
907                                 WM_read_file(C, filename, NULL);
908                         }
909
910                         file_loaded = 1;
911                 }
912         }
913         
914         if(G.background) {
915                 /* actually incorrect, but works for now (ton) */
916                 WM_exit(C);
917         }
918
919         if(!G.background && !file_loaded)
920                 WM_init_splash(C);
921
922         WM_main(C);
923         
924
925         /*XXX if (scr_init==0) {
926                 main_init_screen();
927         }
928         
929         screenmain();*/ /* main display loop */
930
931         return 0;
932 } /* end of int main(argc,argv) */
933
934 static void error_cb(char *err)
935 {
936         
937         printf("%s\n", err);    /* XXX do this in WM too */
938 }
939
940 static void mem_error_cb(char *errorStr)
941 {
942         fputs(errorStr, stderr);
943         fflush(stderr);
944 }
945
946 static void setCallbacks(void)
947 {
948         /* Error output from the alloc routines: */
949         MEM_set_error_callback(mem_error_cb);
950
951
952         /* BLI_blenlib: */
953
954         BLI_setErrorCallBack(error_cb); /* */
955 // XXX  BLI_setInterruptCallBack(blender_test_break);
956
957 }