svn merge -r 13415:13452 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[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
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 [options ...] [file]\n");
178         printf ("Note: Arguments are executed in the order they are given. eg.\n");
179         printf ("    blender -b test.blend -f 1 -o /tmp\n");
180         printf ("  ...may not render to /tmp because '-f 1' renders before the output path is set\n");
181         printf ("    blender -b -o /tmp test.blend -f 1\n");
182         printf ("  ...may not render to /tmp because loading the blend file overwrites the output path that was set\n");
183         printf ("    \"blender -b test.blend -o /tmp -f 1\" works as expected.\n");
184         printf ("\nRender options:\n");
185         printf ("  -b <file>\tRender <file> in background\n");
186         printf ("    -a render frames from start to end (inclusive), only works when used after -b\n");
187         printf ("    -S <name>\tSet scene <name>\n");
188         printf ("    -f <frame>\tRender frame <frame> and save it\n");                          
189         printf ("    -s <frame>\tSet start to frame <frame> (use with -a)\n");
190         printf ("    -e <frame>\tSet end to frame (use with -a)<frame>\n");
191         printf ("    -o <path>\tSet the render path and file name.\n");
192         printf ("      Use // at the start of the path to\n");
193         printf ("        render relative to the blend file.\n");
194         printf ("      The frame number will be added at the end of the filename.\n");
195         printf ("      eg: blender -b foobar.blend -o //render_ -F PNG -x 1 -a\n");
196         printf ("\nFormat options:\n");
197         printf ("    -F <format>\tSet the render format, Valid options are...\n");
198         printf ("    \tTGA IRIS HAMX JPEG MOVIE IRIZ RAWTGA\n");
199         printf ("    \tAVIRAW AVIJPEG PNG BMP FRAMESERVER\n");
200         printf ("    (formats that can be compiled into blender, not available on all systems)\n");
201         printf ("    \tHDR TIFF EXR MPEG AVICODEC QUICKTIME CINEON DPX DDS\n");
202         printf ("    -x <bool>\tSet option to add the file extension to the end of the file.\n");
203         printf ("    -t <threads>\tUse amount of <threads> for rendering\n");
204         /*Add these later - Campbell*/
205         /*
206         printf ("    -colorchannel <type>\tColors to save, valid types are: BW RGB RGBA \n");
207         printf ("    -compression <type>\t Use with EXR format, valid types are..\n");
208         printf ("    \tZIP Pxr24 PIZ RLE\n");
209         printf ("    -zbuf <bool>\tUse with EXR format, set the zbuf save option\n");
210         printf ("    -halffloat <bool>\tUse with EXR format, set the half float option\n");
211         printf ("    -preview <bool>\tUse with EXR format, save a jpeg for viewing as well as the EXR\n");*/
212         printf ("\nAnimation playback options:\n");
213         printf ("  -a <file(s)>\tPlayback <file(s)>, only operates this way when -b is not used.\n");
214         printf ("    -p <sx> <sy>\tOpen with lower left corner at <sx>, <sy>\n");
215         printf ("    -m\t\tRead from disk (Don't buffer)\n");
216         printf ("    -f <fps> <fps-base>\t\tSpecify FPS to start with\n");
217                                 
218         printf ("\nWindow options:\n");
219         printf ("  -w\t\tForce opening with borders (default)\n");
220         printf ("  -W\t\tForce opening without borders\n");
221         printf ("  -p <sx> <sy> <w> <h>\tOpen with lower left corner at <sx>, <sy>\n");
222         printf ("                      \tand width and height <w>, <h>\n");
223         printf ("\nGame Engine specific options:\n");
224         printf ("  -g fixedtime\t\tRun on 50 hertz without dropping frames\n");
225         printf ("  -g vertexarrays\tUse Vertex Arrays for rendering (usually faster)\n");
226         printf ("  -g noaudio\t\tNo audio in Game Engine\n");
227         printf ("  -g nomipmap\t\tNo Texture Mipmapping\n");
228         printf ("  -g linearmipmap\tLinear Texture Mipmapping instead of Nearest (default)\n");
229
230         printf ("\nMisc options:\n");
231         printf ("  -d\t\tTurn debugging on\n");
232         printf ("  -noaudio\tDisable audio on systems that support audio\n");
233         printf ("  -h\t\tPrint this help text\n");
234         printf ("  -y\t\tDisable script links, use -Y to find out why its -y\n");
235         printf ("  -P <filename>\tRun the given Python script (filename or Blender Text)\n");
236 #ifdef WIN32
237         printf ("  -R\t\tRegister .blend extension\n");
238 #endif
239         printf ("  -v\t\tPrint Blender version and exit\n");
240         printf ("  --\t\tEnds option processing.  Following arguments are \n");
241         printf ("    \t\t   passed unchanged.  Access via Python's sys.argv\n");
242 }
243
244
245 double PIL_check_seconds_timer(void);
246 extern void winlay_get_screensize(int *width_r, int *height_r);
247
248 int main(int argc, char **argv)
249 {
250         int a, i, stax, stay, sizx, sizy;
251         SYS_SystemHandle syshandle;
252         Scene *sce;
253
254 #if defined(WIN32) || defined (__linux__)
255         int audio = 1;
256 #else
257         int audio = 0;
258 #endif
259
260         
261 #ifdef WITH_BINRELOC
262         br_init( NULL );
263 #endif
264
265         setCallbacks();
266 #ifdef __APPLE__
267                 /* patch to ignore argument finder gives us (pid?) */
268         if (argc==2 && strncmp(argv[1], "-psn_", 5)==0) {
269                 extern int GHOST_HACK_getFirstFile(char buf[]);
270                 static char firstfilebuf[512];
271                 int scr_x,scr_y;
272
273                 argc= 1;
274
275         /* first let us check if we are hardware accelerated and with VRAM > 16 Mo */
276         
277         if (checkAppleVideoCard()) {
278                         short top, left, bottom, right;
279                         
280                         winlay_get_screensize(&scr_x, &scr_y); 
281                         getMacAvailableBounds(&top, &left, &bottom, &right);
282                         setprefsize(left +10,scr_y - bottom +10,right-left -20,bottom - 64, 0);
283
284         } else {
285                                 winlay_get_screensize(&scr_x, &scr_y);
286
287                 /* 40 + 684 + (headers) 22 + 22 = 768, the powerbook screen height */
288                 setprefsize(120, 40, 850, 684, 0);
289         }
290     
291                 winlay_process_events(0);
292                 if (GHOST_HACK_getFirstFile(firstfilebuf)) {
293                         argc= 2;
294                         argv[1]= firstfilebuf;
295                 }
296         }
297
298 #endif
299
300 #ifdef __FreeBSD__
301         fpsetmask(0);
302 #endif
303 #ifdef __linux__
304     #ifdef __alpha__
305         signal (SIGFPE, fpe_handler);
306     #else
307         if ( getenv("SDL_AUDIODRIVER") == NULL) {
308                 setenv("SDL_AUDIODRIVER", "dma", 1);
309         }
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                 /* Hack - force inclusion of the plugin api functions,
322                  * see blenpluginapi:pluginapi.c
323                  */
324         pluginapi_force_ref();
325         
326         init_nodesystem();
327         
328         initglobals();  /* blender.c */
329
330         syshandle = SYS_GetSystem();
331         GEN_init_messaging_system();
332
333         /* first test for background */
334
335         G.f |= G_DOSCRIPTLINKS; /* script links enabled by default */
336
337         for(a=1; a<argc; a++) {
338
339                 /* Handle unix and windows style help requests */
340                 if ((!strcmp(argv[a], "--help")) || (!strcmp(argv[a], "/?"))){
341                         print_help();
342                         exit(0);
343                 }
344
345                 /* end argument processing after -- */
346                 if (!strcmp( argv[a], "--")){
347                         a = argc;
348                         break;
349                 }
350
351                 /* Handle long version request */
352                 if (!strcmp(argv[a], "--version")){
353                         print_version();
354                         exit(0);
355                 }
356
357                 /* Handle -* switches */
358                 else if(argv[a][0] == '-') {
359                         switch(argv[a][1]) {
360                         case 'a': /* -b was not given, play an animation */
361                                 playanim(argc-1, argv+1);
362                                 exit(0);
363                                 break;
364                         case 'b':
365                         case 'B':
366                                 G.background = 1;
367                                 a= argc;
368                                 break;
369
370                         case 'y':
371                                 G.f &= ~G_DOSCRIPTLINKS;
372                                 break;
373
374                         case 'Y':
375                                 printf ("-y was used to disable script links because,\n");
376                                 printf ("\t-p being taken, Ton was of the opinion that Y\n");
377                                 printf ("\tlooked like a split (disabled) snake, and also\n");
378                                 printf ("\twas similar to a python's tongue (unproven).\n\n");
379
380                                 printf ("\tZr agreed because it gave him a reason to add a\n");
381                                 printf ("\tcompletely useless text into Blender.\n\n");
382                                 
383                                 printf ("\tADDENDUM! Ton, in defense, found this picture of\n");
384                                 printf ("\tan Australian python, exhibiting her (his/its) forked\n");
385                                 printf ("\tY tongue. It could be part of an H Zr retorted!\n\n");
386                                 printf ("\thttp://www.users.bigpond.com/snake.man/\n");
387                                 
388                                 exit(252);
389                                 
390                         case 'h':                       
391                                 print_help();
392                                 exit(0);
393                         case 'v':
394                                 print_version();
395                                 exit(0);
396                         default:
397                                 break;
398                         }
399                 }
400         }
401
402 #ifdef __sgi
403         setuid(getuid()); /* end superuser */
404 #endif
405
406         /* for all platforms, even windos has it! */
407         if(G.background) signal(SIGINT, blender_esc);   /* ctrl c out bg render */
408
409         /* background render uses this font too */
410         BKE_font_register_builtin(datatoc_Bfont, datatoc_Bfont_size);
411         
412         init_def_material();
413
414         if(G.background==0) {
415                 for(a=1; a<argc; a++) {
416                         if(argv[a][0] == '-') {
417                                 switch(argv[a][1]) {
418                                 case 'p':       /* prefsize */
419                                         if (argc-a < 5) {
420                                                 printf ("-p requires four arguments\n");
421                                                 exit(1);
422                                         }
423                                         a++;
424                                         stax= atoi(argv[a]);
425                                         a++;
426                                         stay= atoi(argv[a]);
427                                         a++;
428                                         sizx= atoi(argv[a]);
429                                         a++;
430                                         sizy= atoi(argv[a]);
431
432                                         setprefsize(stax, stay, sizx, sizy, 0);
433                                         break;
434                                 case 'd':
435                                         G.f |= G_DEBUG;         /* std output printf's */ 
436                                         printf ("Blender %d.%02d (sub %d) Build\n", G.version/100, G.version%100, BLENDER_SUBVERSION);
437                                         MEM_set_memory_debug();
438 #ifdef NAN_BUILDINFO
439                                         printf("Build: %s %s %s %s\n", build_date, build_time, build_platform, build_type);
440
441 #endif // NAN_BUILDINFO
442                                         for (i = 0; i < argc; i++) {
443                                                 printf("argv[%d] = %s\n", i, argv[i]);
444                                         }
445                                         break;
446             
447                                 case 'w':
448                                         /* XXX, fixme zr, with borders */
449                                         /* there probably is a better way to do
450                                          * this, right now do as if blender was
451                                          * called with "-p 0 0 xres yres" -- sgefant
452                                          */ 
453                                         winlay_get_screensize(&sizx, &sizy);
454                                         setprefsize(0, 0, sizx, sizy, 1);
455                                         G.windowstate = G_WINDOWSTATE_BORDER;
456                                         break;
457                                 case 'W':
458                                         /* XXX, fixme zr, borderless on win32 */
459                                         /* now on all platforms as of 20070411 - DJC */
460                                         G.windowstate = G_WINDOWSTATE_FULLSCREEN;
461                                         break;
462                                 case 'R':
463                                         /* Registering filetypes only makes sense on windows...      */
464 #ifdef WIN32
465                                         RegisterBlendExtension(argv[0]);
466 #endif
467                                         break;
468                                 case 'n':
469                                 case 'N':
470                                         if (BLI_strcasecmp(argv[a], "-noaudio") == 0|| BLI_strcasecmp(argv[a], "-nosound") == 0) {
471                                                 /**
472                                                         notify the gameengine that no audio is wanted, even if the user didn't give
473                                                         the flag -g noaudio.
474                                                 */
475
476                                                 SYS_WriteCommandLineInt(syshandle,"noaudio",1);
477                                                 audio = 0;
478                                                 if (G.f & G_DEBUG) printf("setting audio to: %d\n", audio);
479                                         }
480                                         break;
481                                 }
482                         }
483                 }
484
485                 BPY_start_python(argc, argv);
486                 
487                 /**
488                  * NOTE: sound_init_audio() *must be* after start_python,
489                  * at least on FreeBSD.
490                  * added note (ton): i removed it altogether
491                  */
492
493                 BIF_init();
494
495         }
496         else {
497                 BPY_start_python(argc, argv);
498                 
499                 // (ton) Commented out. I have no idea whats thisfor... will mail around!
500                 // SYS_WriteCommandLineInt(syshandle,"noaudio",1);
501         // audio = 0;
502         // sound_init_audio();
503         // if (G.f & G_DEBUG) printf("setting audio to: %d\n", audio);
504         }
505
506         /**
507          * NOTE: the U.pythondir string is NULL until BIF_init() is executed,
508          * so we provide the BPY_ function below to append the user defined
509          * pythondir to Python's sys.path at this point.  Simply putting
510          * BIF_init() before BPY_start_python() crashes Blender at startup.
511          * Update: now this function also inits the bpymenus, which also depend
512          * on U.pythondir.
513          */
514         BPY_post_start_python();
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                                                                 set_mipmap(0); //doMipMap = 0;
582                                                         }
583                                                         /* linearMipMap */
584                                                         if (!strcmp(argv[a],"linearmipmap"))
585                                                         {
586                                                                 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 (G.scene) {
597                                         if (a < argc) {
598                                                 int frame= MIN2(MAXFRAME, MAX2(1, atoi(argv[a])));
599                                                 Render *re= RE_NewRender(G.scene->id.name);
600                                                 RE_BlenderAnim(re, G.scene, frame, frame);
601                                         }
602                                 } else {
603                                         printf("\nError: no blend loaded. cannot use '-f'.\n");
604                                 }
605                                 break;
606                         case 'a':
607                                 if (G.scene) {
608                                         Render *re= RE_NewRender(G.scene->id.name);
609                                         RE_BlenderAnim(re, G.scene, G.scene->r.sfra, G.scene->r.efra);
610                                 } else {
611                                         printf("\nError: no blend loaded. cannot use '-a'.\n");
612                                 }
613                                 break;
614                         case 'S':
615                                 if(++a < argc) {
616                                         set_scene_name(argv[a]);
617                                 }
618                                 break;
619                         case 's':
620                                 a++;
621                                 if(G.scene) {
622                                         int frame= MIN2(MAXFRAME, MAX2(1, atoi(argv[a])));
623                                         if (a < argc) (G.scene->r.sfra) = frame;
624                                 } else {
625                                         printf("\nError: no blend loaded. cannot use '-s'.\n");
626                                 }
627                                 break;
628                         case 'e':
629                                 a++;
630                                 if(G.scene) {
631                                         int frame= MIN2(MAXFRAME, MAX2(1, atoi(argv[a])));
632                                         if (a < argc) (G.scene->r.efra) = frame;
633                                 } else {
634                                         printf("\nError: no blend loaded. cannot use '-e'.\n");
635                                 }
636                                 break;
637                         case 'P':
638                                 a++;
639                                 if (a < argc) BPY_run_python_script (argv[a]);
640                                 else printf("\nError: you must specify a Python script after '-P '.\n");
641                                 break;
642                         case 'o':
643                                 a++;
644                                 if (a < argc){
645                                         if(G.scene) {
646                                                 BLI_strncpy(G.scene->r.pic, argv[a], FILE_MAXDIR);
647                                         } else {
648                                                 printf("\nError: no blend loaded. cannot use '-o'.\n");
649                                         }
650                                 } else {
651                                         printf("\nError: you must specify a path after '-o '.\n");
652                                 }
653                                 break;
654                         case 'F':
655                                 a++;
656                                 if (a < argc){
657                                         if(!G.scene) {
658                                                 printf("\nError: no blend loaded. order the arguments so '-F ' is after the blend is loaded.\n");
659                                         } else {
660                                                 if      (!strcmp(argv[a],"TGA")) G.scene->r.imtype = R_TARGA;
661                                                 else if (!strcmp(argv[a],"IRIS")) G.scene->r.imtype = R_IRIS;
662                                                 else if (!strcmp(argv[a],"HAMX")) G.scene->r.imtype = R_HAMX;
663 #ifdef WITH_DDS
664                                                 else if (!strcmp(argv[a],"DDS")) G.scene->r.imtype = R_DDS;
665 #endif
666                                                 else if (!strcmp(argv[a],"JPEG")) G.scene->r.imtype = R_JPEG90;
667                                                 else if (!strcmp(argv[a],"MOVIE")) G.scene->r.imtype = R_MOVIE;
668                                                 else if (!strcmp(argv[a],"IRIZ")) G.scene->r.imtype = R_IRIZ;
669                                                 else if (!strcmp(argv[a],"RAWTGA")) G.scene->r.imtype = R_RAWTGA;
670                                                 else if (!strcmp(argv[a],"AVIRAW")) G.scene->r.imtype = R_AVIRAW;
671                                                 else if (!strcmp(argv[a],"AVIJPEG")) G.scene->r.imtype = R_AVIJPEG;
672                                                 else if (!strcmp(argv[a],"PNG")) G.scene->r.imtype = R_PNG;
673                                                 else if (!strcmp(argv[a],"AVICODEC")) G.scene->r.imtype = R_AVICODEC;
674                                                 else if (!strcmp(argv[a],"QUICKTIME")) G.scene->r.imtype = R_QUICKTIME;
675                                                 else if (!strcmp(argv[a],"BMP")) G.scene->r.imtype = R_BMP;
676                                                 else if (!strcmp(argv[a],"HDR")) G.scene->r.imtype = R_RADHDR;
677                                                 else if (!strcmp(argv[a],"TIFF")) G.scene->r.imtype = R_IRIS;
678 #ifdef WITH_OPENEXR
679                                                 else if (!strcmp(argv[a],"EXR")) G.scene->r.imtype = R_OPENEXR;
680 #endif
681                                                 else if (!strcmp(argv[a],"MPEG")) G.scene->r.imtype = R_FFMPEG;
682                                                 else if (!strcmp(argv[a],"FRAMESERVER")) G.scene->r.imtype = R_FRAMESERVER;
683                                                 else if (!strcmp(argv[a],"CINEON")) G.scene->r.imtype = R_CINEON;
684                                                 else if (!strcmp(argv[a],"DPX")) G.scene->r.imtype = R_DPX;
685                                                 else printf("\nError: Format from '-F' not known or not compiled in this release.\n");
686                                         }
687                                 } else {
688                                         printf("\nError: no blend loaded. cannot use '-x'.\n");
689                                 }
690                                 break;
691                                 
692                         case 't':
693                                 a++;
694                                 if(G.background) {
695                                         RE_set_max_threads(atoi(argv[a]));
696                                 }
697                                 break;
698                         case 'x': /* extension */
699                                 a++;
700                                 if (a < argc) {
701                                         if(G.scene) {
702                                                 if (argv[a][0] == '0') {
703                                                         G.scene->r.scemode &= ~R_EXTENSION;
704                                                 } else if (argv[a][0] == '1') {
705                                                         G.scene->r.scemode |= R_EXTENSION;
706                                                 } else {
707                                                         printf("\nError: Use '-x 1' or '-x 0' To set the extension option.\n");
708                                                 }
709                                         } else {
710                                                 printf("\nError: no blend loaded. order the arguments so '-o ' is after '-x '.\n");
711                                         }
712                                 } else {
713                                         printf("\nError: you must specify a path after '- '.\n");
714                                 }
715                                 break;
716                         }
717                 }
718                 else {
719                         
720                         /* Make the path absolute because its needed for relative linked blends to be found */
721                         int abs = 0;
722                         int filelen;
723                         char cwd[FILE_MAXDIR + FILE_MAXFILE];
724                         char filename[FILE_MAXDIR + FILE_MAXFILE];
725                         cwd[0] = filename[0] = '\0';
726                         
727                         BLI_strncpy(filename, argv[a], sizeof(filename));
728                         filelen = strlen(filename);
729                         
730                         /* relative path checks, could do more tests here... */
731 #ifdef WIN32
732                         /* Account for X:/ and X:\ - should be enough */
733                         if (filelen >= 3 && filename[1] == ':' && (filename[2] == '\\' || filename[2] == '/'))
734                                 abs = 1;
735 #else
736                         if (filelen >= 2 && filename[0] == '/')
737                                 abs = 1 ;
738 #endif
739                         if (!abs) {
740                                 BLI_getwdN(cwd); /* incase the full path to the blend isnt used */
741                                 
742                                 if (cwd[0] == '\0') {
743                                         printf(
744                                         "Could not get the current working directory - $PWD for an unknown reason.\n\t"
745                                         "Relative linked files will not load if the entire blend path is not used.\n\t"
746                                         "The 'Play' button may also fail.\n"
747                                         );
748                                 } else {
749                                         /* uses the blend path relative to cwd important for loading relative linked files.
750                                         *
751                                         * cwd should contain c:\ etc on win32 so the relbase can be NULL
752                                         * relbase being NULL also prevents // being misunderstood as relative to the current
753                                         * blend file which isnt a feature we want to use in this case since were dealing
754                                         * with a path from the command line, rather then from inside Blender */
755                                         
756                                         BLI_make_file_string(NULL, filename, cwd, argv[a]); 
757                                 }
758                         }
759                         
760                         if (G.background) {
761                                 BKE_read_file(filename, NULL);
762                                 sound_initialize_sounds();
763
764                                 /* happens for the UI on file reading too */
765                                 BKE_reset_undo();
766                                 BKE_write_undo("original");     /* save current state */
767                         } else {
768                                 /* we are not running in background mode here, but start blender in UI mode with 
769                                    a file - this should do everything a 'load file' does */
770                                 BIF_read_file(filename);
771                         }
772                 }
773         }
774
775         if(G.background) {
776                 /* actually incorrect, but works for now (ton) */
777                 exit_usiblender();
778         }
779
780         setscreen(G.curscreen);
781
782         if(G.main->scene.first==0) {
783                 sce= add_scene("1");
784                 set_scene(sce);
785         }
786
787         screenmain();
788
789         return 0;
790 } /* end of int main(argc,argv) */
791
792 static void error_cb(char *err)
793 {
794         error("%s", err);
795 }
796
797 static void mem_error_cb(char *errorStr)
798 {
799         fprintf(stderr, errorStr);
800         fflush(stderr);
801 }
802
803 void setCallbacks(void)
804 {
805         /* Error output from the alloc routines: */
806         MEM_set_error_callback(mem_error_cb);
807
808
809         /* BLI_blenlib: */
810
811         BLI_setErrorCallBack(error_cb); /* */
812         BLI_setInterruptCallBack(blender_test_break);
813
814 }