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