Material buttons now view the active node material shading settings.
[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
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 main(int argc, char **argv)
278 {
279         SYS_SystemHandle syshandle;
280         bContext *C= CTX_create();
281         int a, i, stax, stay, sizx, sizy /*XXX, scr_init = 0*/;
282
283 #ifdef WITH_BINRELOC
284         br_init( NULL );
285 #endif
286
287         setCallbacks();
288 #ifdef __APPLE__
289                 /* patch to ignore argument finder gives us (pid?) */
290         if (argc==2 && strncmp(argv[1], "-psn_", 5)==0) {
291                 extern int GHOST_HACK_getFirstFile(char buf[]);
292                 static char firstfilebuf[512];
293
294                 argc= 1;
295
296                 if (GHOST_HACK_getFirstFile(firstfilebuf)) {
297                         argc= 2;
298                         argv[1]= firstfilebuf;
299                 }
300         }
301
302 #endif
303
304 #ifdef __FreeBSD__
305         fpsetmask(0);
306 #endif
307 #ifdef __linux__
308     #ifdef __alpha__
309         signal (SIGFPE, fpe_handler);
310     #endif
311 #endif
312 #if defined(__sgi)
313         signal (SIGFPE, fpe_handler);
314 #endif
315
316         // copy path to executable in bprogname. playanim and creting runtimes
317         // need this.
318
319         BLI_where_am_i(bprogname, argv[0]);
320         
321         {       /* override the hard coded blender path */
322                 char *blender_path_env = getenv("BLENDERPATH");
323                 if(blender_path_env)
324                         BLI_strncpy(blender_path, blender_path_env, sizeof(blender_path));
325         }
326         
327         RNA_init();
328         RE_engines_init();
329
330                 /* Hack - force inclusion of the plugin api functions,
331                  * see blenpluginapi:pluginapi.c
332                  */
333         pluginapi_force_ref();
334
335         init_nodesystem();
336         
337         initglobals();  /* blender.c */
338
339         syshandle = SYS_GetSystem();
340         GEN_init_messaging_system();
341
342         /* first test for background */
343
344         G.f |= G_DOSCRIPTLINKS; /* script links enabled by default */
345
346         for(a=1; a<argc; a++) {
347
348                 /* Handle unix and windows style help requests */
349                 if ((!strcmp(argv[a], "--help")) || (!strcmp(argv[a], "/?"))){
350                         print_help();
351                         exit(0);
352                 }
353
354                 /* end argument processing after -- */
355                 if (!strcmp( argv[a], "--")){
356                         a = argc;
357                         break;
358                 }
359
360                 /* Handle long version request */
361                 if (!strcmp(argv[a], "--version")){
362                         print_version();
363                         exit(0);
364                 }
365
366                 /* Handle -* switches */
367                 else if(argv[a][0] == '-') {
368                         switch(argv[a][1]) {
369                         case 'a': /* -b was not given, play an animation */
370                                 
371                                 /* exception here, see below, it probably needs happens after qt init? */
372                                 libtiff_init();
373
374 // XXX                          playanim(argc-1, argv+1);
375                                 exit(0);
376                                 break;
377                         case 'b':
378                         case 'B':
379                                 G.background = 1;
380                                 a= argc;
381                                 break;
382
383                         case 'y':
384                                 G.f &= ~G_DOSCRIPTLINKS;
385                                 break;
386
387                         case 'Y':
388                                 printf ("-y was used to disable script links because,\n");
389                                 printf ("\t-p being taken, Ton was of the opinion that Y\n");
390                                 printf ("\tlooked like a split (disabled) snake, and also\n");
391                                 printf ("\twas similar to a python's tongue (unproven).\n\n");
392
393                                 printf ("\tZr agreed because it gave him a reason to add a\n");
394                                 printf ("\tcompletely useless text into Blender.\n\n");
395                                 
396                                 printf ("\tADDENDUM! Ton, in defense, found this picture of\n");
397                                 printf ("\tan Australian python, exhibiting her (his/its) forked\n");
398                                 printf ("\tY tongue. It could be part of an H Zr retorted!\n\n");
399                                 printf ("\thttp://www.users.bigpond.com/snake.man/\n");
400                                 
401                                 exit(252);
402                                 
403                         case 'h':                       
404                                 print_help();
405                                 exit(0);
406                         case 'v':
407                                 print_version();
408                                 exit(0);
409                         default:
410                                 break;
411                         }
412                 }
413         }
414
415 #ifdef __sgi
416         setuid(getuid()); /* end superuser */
417 #endif
418
419         /* for all platforms, even windos has it! */
420         if(G.background) signal(SIGINT, blender_esc);   /* ctrl c out bg render */
421         
422         /* background render uses this font too */
423         BKE_font_register_builtin(datatoc_Bfont, datatoc_Bfont_size);
424         
425         init_def_material();
426
427         if(G.background==0) {
428                 for(a=1; a<argc; a++) {
429                         if(argv[a][0] == '-') {
430                                 switch(argv[a][1]) {
431                                 case 'p':       /* prefsize */
432                                         if (argc-a < 5) {
433                                                 printf ("-p requires four arguments\n");
434                                                 exit(1);
435                                         }
436                                         a++;
437                                         stax= atoi(argv[a]);
438                                         a++;
439                                         stay= atoi(argv[a]);
440                                         a++;
441                                         sizx= atoi(argv[a]);
442                                         a++;
443                                         sizy= atoi(argv[a]);
444
445                                         WM_setprefsize(stax, stay, sizx, sizy);
446                                         break;
447                                 case 'd':
448                                         G.f |= G_DEBUG;         /* std output printf's */ 
449                                         printf ("Blender %d.%02d (sub %d) Build\n", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
450                                         MEM_set_memory_debug();
451 #ifdef NAN_BUILDINFO
452                                         printf("Build: %s %s %s %s\n", build_date, build_time, build_platform, build_type);
453
454 #endif // NAN_BUILDINFO
455                                         for (i = 0; i < argc; i++) {
456                                                 printf("argv[%d] = %s\n", i, argv[i]);
457                                         }
458                                         break;
459                                 case 'E':
460                                         G.f |= G_SWAP_EXCHANGE;
461                                         break;
462                                 case 'w':
463                                         /* with borders XXX OLD CRUFT!*/
464                                         break;
465                                 case 'W':
466                                         /* borderless, win + linux XXX OLD CRUFT */
467                                         /* XXX, fixme mein, borderless on OSX */
468                                         break;
469                                 case 'R':
470                                         /* Registering filetypes only makes sense on windows...      */
471 #ifdef WIN32
472                                         RegisterBlendExtension(argv[0]);
473 #endif
474                                         break;
475                                 case 'n':
476                                 case 'N':
477                                         if (BLI_strcasecmp(argv[a], "-nojoystick") == 0) {
478                                                 /**
479                                                         don't initialize joysticks if user doesn't want to use joysticks
480                                                         failed joystick initialization delays over 5 seconds, before game engine start
481                                                 */
482                                                 SYS_WriteCommandLineInt(syshandle,"nojoystick",1);
483                                                 if (G.f & G_DEBUG) printf("disabling nojoystick\n");
484                                         }
485                                         if (BLI_strcasecmp(argv[a], "-noglsl") == 0)
486                                                 GPU_extensions_disable();
487                                         break;
488                                 }
489                         }
490                 }
491
492 #ifndef DISABLE_PYTHON          
493                 BPY_start_python(argc, argv);
494 #endif          
495
496                 WM_init(C);
497                 
498                 // XXX BRECHT SOLVE
499                 BLI_where_is_temp( btempdir, 1 ); /* call after loading the .B.blend so we can read U.tempdir */
500
501 #ifndef DISABLE_SDL
502         BLI_setenv("SDL_VIDEODRIVER", "dummy");
503 #ifdef __linux__
504         /* On linux the default SDL driver dma often would not play
505          * use alsa if none is set */
506         setenv("SDL_AUDIODRIVER", "alsa", 0);
507 #endif
508 #endif
509         }
510         else {
511 #ifndef DISABLE_PYTHON
512                 BPY_start_python(argc, argv);
513 #endif          
514                 BLI_where_is_temp( btempdir, 0 ); /* call after loading the .B.blend so we can read U.tempdir */
515         }
516 #ifndef DISABLE_PYTHON
517         /**
518          * NOTE: the U.pythondir string is NULL until WM_init() is executed,
519          * so we provide the BPY_ function below to append the user defined
520          * pythondir to Python's sys.path at this point.  Simply putting
521          * WM_init() before BPY_start_python() crashes Blender at startup.
522          * Update: now this function also inits the bpymenus, which also depend
523          * on U.pythondir.
524          */
525         BPY_post_start_python();
526
527         if(!G.background)
528                 BPY_run_ui_scripts(C, 0); /* dont need to reload the first time */
529 #endif
530         
531         CTX_py_init_set(C, 1);
532         if(!G.background)
533                 WM_keymap_init(C); /* after BPY_run_ui_scripts() */
534
535 #ifdef WITH_QUICKTIME
536
537         quicktime_init();
538
539 #endif /* WITH_QUICKTIME */
540
541         /* dynamically load libtiff, if available */
542         libtiff_init();
543         if (!G.have_libtiff && (G.f & G_DEBUG)) {
544                 printf("Unable to load: libtiff.\n");
545                 printf("Try setting the BF_TIFF_LIB environment variable if you want this support.\n");
546                 printf("Example: setenv BF_TIFF_LIB /usr/lib/libtiff.so\n");
547         }
548
549         /* OK we are ready for it */
550
551         for(a=1; a<argc; a++) {
552                 if (G.afbreek==1) break;
553
554                 if(argv[a][0] == '-') {
555                         switch(argv[a][1]) {
556                         case '-':  /* -- ends argument processing */
557                                 a = argc;
558                                 break;
559                                 
560                         case 'p':       /* prefsize */
561                                 a+= 4;
562                                 break;
563
564                         case 'g':
565                                 {
566                                 /**
567                                 gameengine parameters are automaticly put into system
568                                 -g [paramname = value]
569                                 -g [boolparamname]
570                                 example:
571                                 -g novertexarrays
572                                 -g maxvertexarraysize = 512
573                                 */
574
575                                         if(++a < argc)
576                                         {
577                                                 char* paramname = argv[a];
578                                                 /* check for single value versus assignment */
579                                                 if (a+1 < argc && (*(argv[a+1]) == '='))
580                                                 {
581                                                         a++;
582                                                         if (a+1 < argc)
583                                                         {
584                                                                 a++;
585                                                                 /* assignment */
586                                                                 SYS_WriteCommandLineString(syshandle,paramname,argv[a]);
587                                                         }  else
588                                                         {
589                                                                 printf("error: argument assignment (%s) without value.\n",paramname);
590                                                         }
591                                                         /* name arg eaten */
592
593                                                 } else
594                                                 {
595                                                         SYS_WriteCommandLineInt(syshandle,argv[a],1);
596
597                                                         /* doMipMap */
598                                                         if (!strcmp(argv[a],"nomipmap"))
599                                                         {
600                                                                 GPU_set_mipmap(0); //doMipMap = 0;
601                                                         }
602                                                         /* linearMipMap */
603                                                         if (!strcmp(argv[a],"linearmipmap"))
604                                                         {
605                                                                 GPU_set_linear_mipmap(1); //linearMipMap = 1;
606                                                         }
607
608
609                                                 } /* if (*(argv[a+1]) == '=') */
610                                         } /*    if(++a < argc)  */
611                                         break;
612                                 }
613                         case 'f':
614                                 a++;
615                                 if (CTX_data_scene(C)) {
616                                         Scene *scene= CTX_data_scene(C);
617                                         
618                                         if (a < argc) {
619                                                 int frame = atoi(argv[a]);
620                                                 Render *re = RE_NewRender(scene->id.name);
621
622                                                 frame = MIN2(MAXFRAME, MAX2(1, frame));
623                                                 
624                                                 RE_BlenderAnim(re, scene, frame, frame, scene->frame_step);
625                                         }
626                                 } else {
627                                         printf("\nError: no blend loaded. cannot use '-f'.\n");
628                                 }
629                                 break;
630                         case 'a':
631                                 if (CTX_data_scene(C)) {
632                                         Scene *scene= CTX_data_scene(C);
633                                         Render *re= RE_NewRender(scene->id.name);
634                                         RE_BlenderAnim(re, scene, scene->r.sfra, scene->r.efra, scene->frame_step);
635                                 } else {
636                                         printf("\nError: no blend loaded. cannot use '-a'.\n");
637                                 }
638                                 break;
639                         case 'S':
640                                 if(++a < argc) {
641                                         set_scene_name(argv[a]);
642                                 }
643                                 break;
644                         case 's':
645                                 a++;
646                                 if (CTX_data_scene(C)) {
647                                         Scene *scene= CTX_data_scene(C);
648                                         if (a < argc) {
649                                                 int frame = atoi(argv[a]);
650                                                 (scene->r.sfra) = MIN2(MAXFRAME, MAX2(1, frame));
651                                         }
652                                 } else {
653                                         printf("\nError: no blend loaded. cannot use '-s'.\n");
654                                 }
655                                 break;
656                         case 'e':
657                                 a++;
658                                 if (CTX_data_scene(C)) {
659                                         Scene *scene= CTX_data_scene(C);
660                                         if (a < argc) {
661                                                 int frame = atoi(argv[a]);
662                                                 (scene->r.efra) = MIN2(MAXFRAME, MAX2(1, frame));
663                                         }
664                                 } else {
665                                         printf("\nError: no blend loaded. cannot use '-e'.\n");
666                                 }
667                                 break;
668                         case 'j':
669                                 a++;
670                                 if (CTX_data_scene(C)) {
671                                         Scene *scene= CTX_data_scene(C);
672                                         if (a < argc) {
673                                                 int frame = atoi(argv[a]);
674                                                 (scene->frame_step) = MIN2(MAXFRAME, MAX2(1, frame));
675                                         }
676                                 } else {
677                                         printf("\nError: no blend loaded. cannot use '-j'.\n");
678                                 }
679                                 break;
680                         case 'P':
681
682 #ifndef DISABLE_PYTHON
683                                 //XXX 
684                                 // FOR TESTING ONLY
685                                 a++;
686                                 BPY_run_python_script(C, argv[a], NULL, NULL); // use reports?
687 #if 0
688                                 a++;
689                                 if (a < argc) {
690                                         /* If we're not running in background mode, then give python a valid screen */
691                                         if ((G.background==0) && (scr_init==0)) {
692                                                 main_init_screen();
693                                                 scr_init = 1;
694                                         }
695                                         BPY_run_python_script(C, argv[a], NULL, NULL); // use reports?
696                                 }
697                                 else printf("\nError: you must specify a Python script after '-P '.\n");
698 #endif
699 #else
700                                 printf("This blender was built without python support\n");
701 #endif /* DISABLE_PYTHON */
702                                 break;
703                         case 'o':
704                                 a++;
705                                 if (a < argc){
706                                         if (CTX_data_scene(C)) {
707                                                 Scene *scene= CTX_data_scene(C);
708                                                 BLI_strncpy(scene->r.pic, argv[a], FILE_MAXDIR);
709                                         } else {
710                                                 printf("\nError: no blend loaded. cannot use '-o'.\n");
711                                         }
712                                 } else {
713                                         printf("\nError: you must specify a path after '-o '.\n");
714                                 }
715                                 break;
716                         case 'E':
717                                 a++;
718                                 if (a < argc)
719                                 {
720                                         if (!strcmp(argv[a],"help"))
721                                         {
722                                                 RenderEngineType *type = NULL;
723
724                                                 for( type = R_engines.first; type; type = type->next )
725                                                 {
726                                                         printf("\t%s\n", type->idname);
727                                                 }
728                                                 exit(0);
729                                         }
730                                         else 
731                                         {
732                                                 if (CTX_data_scene(C)==NULL)
733                                                 {
734                                                         printf("\nError: no blend loaded. order the arguments so '-E ' is after the blend is loaded.\n");
735                                                 }
736                                                 else 
737                                                 {
738                                                         Scene *scene= CTX_data_scene(C);
739                                                         RenderData *rd = &scene->r;
740                                                         RenderEngineType *type = NULL;
741                                                         
742                                                         for( type = R_engines.first; type; type = type->next )
743                                                         {
744                                                                 if (!strcmp(argv[a],type->idname))
745                                                                 {
746                                                                         BLI_strncpy(rd->engine, type->idname, sizeof(rd->engine));
747                                                                 }
748                                                         }
749                                                 }
750                                         }
751                                 }
752                                 else
753                                 {
754                                         printf("\nEngine not specified.\n");
755                                 }
756                                 break;
757                         case 'F':
758                                 a++;
759                                 if (a < argc){
760                                         if (CTX_data_scene(C)==NULL) {
761                                                 printf("\nError: no blend loaded. order the arguments so '-F ' is after the blend is loaded.\n");
762                                         } else {
763                                                 Scene *scene= CTX_data_scene(C);
764                                                 if      (!strcmp(argv[a],"TGA")) scene->r.imtype = R_TARGA;
765                                                 else if (!strcmp(argv[a],"IRIS")) scene->r.imtype = R_IRIS;
766                                                 else if (!strcmp(argv[a],"HAMX")) scene->r.imtype = R_HAMX;
767 #ifdef WITH_DDS
768                                                 else if (!strcmp(argv[a],"DDS")) scene->r.imtype = R_DDS;
769 #endif
770                                                 else if (!strcmp(argv[a],"JPEG")) scene->r.imtype = R_JPEG90;
771                                                 else if (!strcmp(argv[a],"MOVIE")) scene->r.imtype = R_MOVIE;
772                                                 else if (!strcmp(argv[a],"IRIZ")) scene->r.imtype = R_IRIZ;
773                                                 else if (!strcmp(argv[a],"RAWTGA")) scene->r.imtype = R_RAWTGA;
774                                                 else if (!strcmp(argv[a],"AVIRAW")) scene->r.imtype = R_AVIRAW;
775                                                 else if (!strcmp(argv[a],"AVIJPEG")) scene->r.imtype = R_AVIJPEG;
776                                                 else if (!strcmp(argv[a],"PNG")) scene->r.imtype = R_PNG;
777                                                 else if (!strcmp(argv[a],"AVICODEC")) scene->r.imtype = R_AVICODEC;
778                                                 else if (!strcmp(argv[a],"QUICKTIME")) scene->r.imtype = R_QUICKTIME;
779                                                 else if (!strcmp(argv[a],"BMP")) scene->r.imtype = R_BMP;
780                                                 else if (!strcmp(argv[a],"HDR")) scene->r.imtype = R_RADHDR;
781                                                 else if (!strcmp(argv[a],"TIFF")) scene->r.imtype = R_TIFF;
782 #ifdef WITH_OPENEXR
783                                                 else if (!strcmp(argv[a],"EXR")) scene->r.imtype = R_OPENEXR;
784                                                 else if (!strcmp(argv[a],"MULTILAYER")) scene->r.imtype = R_MULTILAYER;
785 #endif
786                                                 else if (!strcmp(argv[a],"MPEG")) scene->r.imtype = R_FFMPEG;
787                                                 else if (!strcmp(argv[a],"FRAMESERVER")) scene->r.imtype = R_FRAMESERVER;
788                                                 else if (!strcmp(argv[a],"CINEON")) scene->r.imtype = R_CINEON;
789                                                 else if (!strcmp(argv[a],"DPX")) scene->r.imtype = R_DPX;
790 #if WITH_OPENJPEG
791                                                 else if (!strcmp(argv[a],"JP2")) scene->r.imtype = R_JP2;
792 #endif
793                                                 else printf("\nError: Format from '-F' not known or not compiled in this release.\n");
794                                         }
795                                 } else {
796                                         printf("\nError: no blend loaded. cannot use '-x'.\n");
797                                 }
798                                 break;
799                                 
800                         case 't':
801                                 a++;
802                                 if (a < argc) {
803                                         if(G.background) {
804                                                 RE_set_max_threads(atoi(argv[a]));
805                                         } else {
806                                                 printf("Warning: threads can only be set in background mode\n");
807                                         }
808                                 } else {
809                                         printf("\nError: you must specify a number of threads between 0 and 8 '-t '.\n");
810                                 }
811                                 break;
812                         case 'x': /* extension */
813                                 a++;
814                                 if (a < argc) {
815                                         if (CTX_data_scene(C)) {
816                                                 Scene *scene= CTX_data_scene(C);
817                                                 if (argv[a][0] == '0') {
818                                                         scene->r.scemode &= ~R_EXTENSION;
819                                                 } else if (argv[a][0] == '1') {
820                                                         scene->r.scemode |= R_EXTENSION;
821                                                 } else {
822                                                         printf("\nError: Use '-x 1' or '-x 0' To set the extension option.\n");
823                                                 }
824                                         } else {
825                                                 printf("\nError: no blend loaded. order the arguments so '-o ' is after '-x '.\n");
826                                         }
827                                 } else {
828                                         printf("\nError: you must specify a path after '- '.\n");
829                                 }
830                                 break;
831                         }
832                 }
833                 else {
834                         
835                         /* Make the path absolute because its needed for relative linked blends to be found */
836                         char filename[FILE_MAXDIR + FILE_MAXFILE];
837                         
838                         BLI_strncpy(filename, argv[a], sizeof(filename));
839                         BLI_convertstringcwd(filename);
840                         
841                         if (G.background) {
842                                 int retval = BKE_read_file(C, argv[a], NULL, NULL);
843                                 
844                                 /*we successfully loaded a blend file, get sure that
845                                 pointcache works */
846                                 if (retval!=0) G.relbase_valid = 1;
847
848                                 /* happens for the UI on file reading too (huh? (ton))*/
849 // XXX                  BKE_reset_undo();
850 //                              BKE_write_undo("original");     /* save current state */
851                         } else {
852                                 /* we are not running in background mode here, but start blender in UI mode with 
853                                    a file - this should do everything a 'load file' does */
854                                 WM_read_file(C, filename, NULL);
855                         }
856                 }
857         }
858         
859         if(G.background) {
860                 /* actually incorrect, but works for now (ton) */
861                 WM_exit(C);
862         }
863
864
865         WM_main(C);
866         
867         /*XXX if (scr_init==0) {
868                 main_init_screen();
869         }
870         
871         screenmain();*/ /* main display loop */
872
873         return 0;
874 } /* end of int main(argc,argv) */
875
876 static void error_cb(char *err)
877 {
878         
879         printf("%s\n", err);    /* XXX do this in WM too */
880 }
881
882 static void mem_error_cb(char *errorStr)
883 {
884         fputs(errorStr, stderr);
885         fflush(stderr);
886 }
887
888 static void setCallbacks(void)
889 {
890         /* Error output from the alloc routines: */
891         MEM_set_error_callback(mem_error_cb);
892
893
894         /* BLI_blenlib: */
895
896         BLI_setErrorCallBack(error_cb); /* */
897 // XXX  BLI_setInterruptCallBack(blender_test_break);
898
899 }