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