Linux only addition to know for sure the path of blender because sometimes the Play...
[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 __linux__
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 __linux__
262         /* linux uses binrealoc to know its binary path */
263         br_init( NULL );
264 #endif
265
266         setCallbacks();
267 #ifdef __APPLE__
268                 /* patch to ignore argument finder gives us (pid?) */
269         if (argc==2 && strncmp(argv[1], "-psn_", 5)==0) {
270                 extern int GHOST_HACK_getFirstFile(char buf[]);
271                 static char firstfilebuf[512];
272                 int scr_x,scr_y;
273
274                 argc= 1;
275
276         /* first let us check if we are hardware accelerated and with VRAM > 16 Mo */
277         
278         if (checkAppleVideoCard()) {
279                         short top, left, bottom, right;
280                         
281                         winlay_get_screensize(&scr_x, &scr_y); 
282                         getMacAvailableBounds(&top, &left, &bottom, &right);
283                         setprefsize(left +10,scr_y - bottom +10,right-left -20,bottom - 64, 0);
284
285         } else {
286                                 winlay_get_screensize(&scr_x, &scr_y);
287
288                 /* 40 + 684 + (headers) 22 + 22 = 768, the powerbook screen height */
289                 setprefsize(120, 40, 850, 684, 0);
290         }
291     
292                 winlay_process_events(0);
293                 if (GHOST_HACK_getFirstFile(firstfilebuf)) {
294                         argc= 2;
295                         argv[1]= firstfilebuf;
296                 }
297         }
298
299 #endif
300
301 #ifdef __FreeBSD__
302         fpsetmask(0);
303 #endif
304 #ifdef __linux__
305     #ifdef __alpha__
306         signal (SIGFPE, fpe_handler);
307     #else
308         if ( getenv("SDL_AUDIODRIVER") == NULL) {
309                 setenv("SDL_AUDIODRIVER", "dma", 1);
310         }
311     #endif
312 #endif
313 #if defined(__sgi)
314         signal (SIGFPE, fpe_handler);
315 #endif
316
317         // copy path to executable in bprogname. playanim and creting runtimes
318         // need this.
319
320         BLI_where_am_i(bprogname, argv[0]);
321
322                 /* Hack - force inclusion of the plugin api functions,
323                  * see blenpluginapi:pluginapi.c
324                  */
325         pluginapi_force_ref();
326         
327         init_nodesystem();
328         
329         initglobals();  /* blender.c */
330
331         syshandle = SYS_GetSystem();
332         GEN_init_messaging_system();
333
334         /* first test for background */
335
336         G.f |= G_DOSCRIPTLINKS; /* script links enabled by default */
337
338         for(a=1; a<argc; a++) {
339
340                 /* Handle unix and windows style help requests */
341                 if ((!strcmp(argv[a], "--help")) || (!strcmp(argv[a], "/?"))){
342                         print_help();
343                         exit(0);
344                 }
345
346                 /* end argument processing after -- */
347                 if (!strcmp( argv[a], "--")){
348                         a = argc;
349                         break;
350                 }
351
352                 /* Handle long version request */
353                 if (!strcmp(argv[a], "--version")){
354                         print_version();
355                         exit(0);
356                 }
357
358                 /* Handle -* switches */
359                 else if(argv[a][0] == '-') {
360                         switch(argv[a][1]) {
361                         case 'a': /* -b was not given, play an animation */
362                                 playanim(argc-1, argv+1);
363                                 exit(0);
364                                 break;
365                         case 'b':
366                         case 'B':
367                                 G.background = 1;
368                                 a= argc;
369                                 break;
370
371                         case 'y':
372                                 G.f &= ~G_DOSCRIPTLINKS;
373                                 break;
374
375                         case 'Y':
376                                 printf ("-y was used to disable script links because,\n");
377                                 printf ("\t-p being taken, Ton was of the opinion that Y\n");
378                                 printf ("\tlooked like a split (disabled) snake, and also\n");
379                                 printf ("\twas similar to a python's tongue (unproven).\n\n");
380
381                                 printf ("\tZr agreed because it gave him a reason to add a\n");
382                                 printf ("\tcompletely useless text into Blender.\n\n");
383                                 
384                                 printf ("\tADDENDUM! Ton, in defense, found this picture of\n");
385                                 printf ("\tan Australian python, exhibiting her (his/its) forked\n");
386                                 printf ("\tY tongue. It could be part of an H Zr retorted!\n\n");
387                                 printf ("\thttp://www.users.bigpond.com/snake.man/\n");
388                                 
389                                 exit(252);
390                                 
391                         case 'h':                       
392                                 print_help();
393                                 exit(0);
394                         case 'v':
395                                 print_version();
396                                 exit(0);
397                         default:
398                                 break;
399                         }
400                 }
401         }
402
403 #ifdef __sgi
404         setuid(getuid()); /* end superuser */
405 #endif
406
407         /* for all platforms, even windos has it! */
408         if(G.background) signal(SIGINT, blender_esc);   /* ctrl c out bg render */
409
410         /* background render uses this font too */
411         BKE_font_register_builtin(datatoc_Bfont, datatoc_Bfont_size);
412         
413         init_def_material();
414
415         if(G.background==0) {
416                 for(a=1; a<argc; a++) {
417                         if(argv[a][0] == '-') {
418                                 switch(argv[a][1]) {
419                                 case 'p':       /* prefsize */
420                                         if (argc-a < 5) {
421                                                 printf ("-p requires four arguments\n");
422                                                 exit(1);
423                                         }
424                                         a++;
425                                         stax= atoi(argv[a]);
426                                         a++;
427                                         stay= atoi(argv[a]);
428                                         a++;
429                                         sizx= atoi(argv[a]);
430                                         a++;
431                                         sizy= atoi(argv[a]);
432
433                                         setprefsize(stax, stay, sizx, sizy, 0);
434                                         break;
435                                 case 'd':
436                                         G.f |= G_DEBUG;         /* std output printf's */ 
437                                         printf ("Blender %d.%02d (sub %d) Build\n", G.version/100, G.version%100, BLENDER_SUBVERSION);
438                                         MEM_set_memory_debug();
439 #ifdef NAN_BUILDINFO
440                                         printf("Build: %s %s %s %s\n", build_date, build_time, build_platform, build_type);
441
442 #endif // NAN_BUILDINFO
443                                         for (i = 0; i < argc; i++) {
444                                                 printf("argv[%d] = %s\n", i, argv[i]);
445                                         }
446                                         break;
447             
448                                 case 'w':
449                                         /* XXX, fixme zr, with borders */
450                                         /* there probably is a better way to do
451                                          * this, right now do as if blender was
452                                          * called with "-p 0 0 xres yres" -- sgefant
453                                          */ 
454                                         winlay_get_screensize(&sizx, &sizy);
455                                         setprefsize(0, 0, sizx, sizy, 1);
456                                         G.windowstate = G_WINDOWSTATE_BORDER;
457                                         break;
458                                 case 'W':
459                                         /* XXX, fixme zr, borderless on win32 */
460                                         /* now on all platforms as of 20070411 - DJC */
461                                         G.windowstate = G_WINDOWSTATE_FULLSCREEN;
462                                         break;
463                                 case 'R':
464                                         /* Registering filetypes only makes sense on windows...      */
465 #ifdef WIN32
466                                         RegisterBlendExtension(argv[0]);
467 #endif
468                                         break;
469                                 case 'n':
470                                 case 'N':
471                                         if (BLI_strcasecmp(argv[a], "-noaudio") == 0|| BLI_strcasecmp(argv[a], "-nosound") == 0) {
472                                                 /**
473                                                         notify the gameengine that no audio is wanted, even if the user didn't give
474                                                         the flag -g noaudio.
475                                                 */
476
477                                                 SYS_WriteCommandLineInt(syshandle,"noaudio",1);
478                                                 audio = 0;
479                                                 if (G.f & G_DEBUG) printf("setting audio to: %d\n", audio);
480                                         }
481                                         break;
482                                 }
483                         }
484                 }
485
486                 BPY_start_python(argc, argv);
487                 
488                 /**
489                  * NOTE: sound_init_audio() *must be* after start_python,
490                  * at least on FreeBSD.
491                  * added note (ton): i removed it altogether
492                  */
493
494                 BIF_init();
495
496         }
497         else {
498                 BPY_start_python(argc, argv);
499                 
500                 // (ton) Commented out. I have no idea whats thisfor... will mail around!
501                 // SYS_WriteCommandLineInt(syshandle,"noaudio",1);
502         // audio = 0;
503         // sound_init_audio();
504         // if (G.f & G_DEBUG) printf("setting audio to: %d\n", audio);
505         }
506
507         /**
508          * NOTE: the U.pythondir string is NULL until BIF_init() is executed,
509          * so we provide the BPY_ function below to append the user defined
510          * pythondir to Python's sys.path at this point.  Simply putting
511          * BIF_init() before BPY_start_python() crashes Blender at startup.
512          * Update: now this function also inits the bpymenus, which also depend
513          * on U.pythondir.
514          */
515         BPY_post_start_python();
516
517 #ifdef WITH_QUICKTIME
518
519         quicktime_init();
520
521 #endif /* WITH_QUICKTIME */
522
523         /* dynamically load libtiff, if available */
524         libtiff_init();
525         if (!G.have_libtiff && (G.f & G_DEBUG)) {
526                 printf("Unable to load: libtiff.\n");
527                 printf("Try setting the BF_TIFF_LIB environment variable if you want this support.\n");
528                 printf("Example: setenv BF_TIFF_LIB /usr/lib/libtiff.so\n");
529         }
530
531         /* OK we are ready for it */
532
533         for(a=1; a<argc; a++) {
534                 if (G.afbreek==1) break;
535
536                 if(argv[a][0] == '-') {
537                         switch(argv[a][1]) {
538                         case '-':  /* -- ends argument processing */
539                                 a = argc;
540                                 break;
541                                 
542                         case 'p':       /* prefsize */
543                                 a+= 4;
544                                 break;
545
546                         case 'g':
547                                 {
548                                 /**
549                                 gameengine parameters are automaticly put into system
550                                 -g [paramname = value]
551                                 -g [boolparamname]
552                                 example:
553                                 -g novertexarrays
554                                 -g maxvertexarraysize = 512
555                                 */
556
557                                         if(++a < argc)
558                                         {
559                                                 char* paramname = argv[a];
560                                                 /* check for single value versus assignment */
561                                                 if (a+1 < argc && (*(argv[a+1]) == '='))
562                                                 {
563                                                         a++;
564                                                         if (a+1 < argc)
565                                                         {
566                                                                 a++;
567                                                                 /* assignment */
568                                                                 SYS_WriteCommandLineString(syshandle,paramname,argv[a]);
569                                                         }  else
570                                                         {
571                                                                 printf("error: argument assignment (%s) without value.\n",paramname);
572                                                         }
573                                                         /* name arg eaten */
574
575                                                 } else
576                                                 {
577                                                         SYS_WriteCommandLineInt(syshandle,argv[a],1);
578
579                                                         /* doMipMap */
580                                                         if (!strcmp(argv[a],"nomipmap"))
581                                                         {
582                                                                 set_mipmap(0); //doMipMap = 0;
583                                                         }
584                                                         /* linearMipMap */
585                                                         if (!strcmp(argv[a],"linearmipmap"))
586                                                         {
587                                                                 set_linear_mipmap(1); //linearMipMap = 1;
588                                                         }
589
590
591                                                 } /* if (*(argv[a+1]) == '=') */
592                                         } /*    if(++a < argc)  */
593                                         break;
594                                 }
595                         case 'f':
596                                 a++;
597                                 if (G.scene) {
598                                         if (a < argc) {
599                                                 int frame= MIN2(MAXFRAME, MAX2(1, atoi(argv[a])));
600                                                 Render *re= RE_NewRender(G.scene->id.name);
601                                                 RE_BlenderAnim(re, G.scene, frame, frame);
602                                         }
603                                 } else {
604                                         printf("\nError: no blend loaded. cannot use '-f'.\n");
605                                 }
606                                 break;
607                         case 'a':
608                                 if (G.scene) {
609                                         Render *re= RE_NewRender(G.scene->id.name);
610                                         RE_BlenderAnim(re, G.scene, G.scene->r.sfra, G.scene->r.efra);
611                                 } else {
612                                         printf("\nError: no blend loaded. cannot use '-a'.\n");
613                                 }
614                                 break;
615                         case 'S':
616                                 if(++a < argc) {
617                                         set_scene_name(argv[a]);
618                                 }
619                                 break;
620                         case 's':
621                                 a++;
622                                 if(G.scene) {
623                                         int frame= MIN2(MAXFRAME, MAX2(1, atoi(argv[a])));
624                                         if (a < argc) (G.scene->r.sfra) = frame;
625                                 } else {
626                                         printf("\nError: no blend loaded. cannot use '-s'.\n");
627                                 }
628                                 break;
629                         case 'e':
630                                 a++;
631                                 if(G.scene) {
632                                         int frame= MIN2(MAXFRAME, MAX2(1, atoi(argv[a])));
633                                         if (a < argc) (G.scene->r.efra) = frame;
634                                 } else {
635                                         printf("\nError: no blend loaded. cannot use '-e'.\n");
636                                 }
637                                 break;
638                         case 'P':
639                                 a++;
640                                 if (a < argc) BPY_run_python_script (argv[a]);
641                                 else printf("\nError: you must specify a Python script after '-P '.\n");
642                                 break;
643                         case 'o':
644                                 a++;
645                                 if (a < argc){
646                                         if(G.scene) {
647                                                 BLI_strncpy(G.scene->r.pic, argv[a], FILE_MAXDIR);
648                                         } else {
649                                                 printf("\nError: no blend loaded. cannot use '-o'.\n");
650                                         }
651                                 } else {
652                                         printf("\nError: you must specify a path after '-o '.\n");
653                                 }
654                                 break;
655                         case 'F':
656                                 a++;
657                                 if (a < argc){
658                                         if(!G.scene) {
659                                                 printf("\nError: no blend loaded. order the arguments so '-F ' is after the blend is loaded.\n");
660                                         } else {
661                                                 if      (!strcmp(argv[a],"TGA")) G.scene->r.imtype = R_TARGA;
662                                                 else if (!strcmp(argv[a],"IRIS")) G.scene->r.imtype = R_IRIS;
663                                                 else if (!strcmp(argv[a],"HAMX")) G.scene->r.imtype = R_HAMX;
664 #ifdef WITH_DDS
665                                                 else if (!strcmp(argv[a],"DDS")) G.scene->r.imtype = R_DDS;
666 #endif
667                                                 else if (!strcmp(argv[a],"JPEG")) G.scene->r.imtype = R_JPEG90;
668                                                 else if (!strcmp(argv[a],"MOVIE")) G.scene->r.imtype = R_MOVIE;
669                                                 else if (!strcmp(argv[a],"IRIZ")) G.scene->r.imtype = R_IRIZ;
670                                                 else if (!strcmp(argv[a],"RAWTGA")) G.scene->r.imtype = R_RAWTGA;
671                                                 else if (!strcmp(argv[a],"AVIRAW")) G.scene->r.imtype = R_AVIRAW;
672                                                 else if (!strcmp(argv[a],"AVIJPEG")) G.scene->r.imtype = R_AVIJPEG;
673                                                 else if (!strcmp(argv[a],"PNG")) G.scene->r.imtype = R_PNG;
674                                                 else if (!strcmp(argv[a],"AVICODEC")) G.scene->r.imtype = R_AVICODEC;
675                                                 else if (!strcmp(argv[a],"QUICKTIME")) G.scene->r.imtype = R_QUICKTIME;
676                                                 else if (!strcmp(argv[a],"BMP")) G.scene->r.imtype = R_BMP;
677                                                 else if (!strcmp(argv[a],"HDR")) G.scene->r.imtype = R_RADHDR;
678                                                 else if (!strcmp(argv[a],"TIFF")) G.scene->r.imtype = R_IRIS;
679 #ifdef WITH_OPENEXR
680                                                 else if (!strcmp(argv[a],"EXR")) G.scene->r.imtype = R_OPENEXR;
681 #endif
682                                                 else if (!strcmp(argv[a],"MPEG")) G.scene->r.imtype = R_FFMPEG;
683                                                 else if (!strcmp(argv[a],"FRAMESERVER")) G.scene->r.imtype = R_FRAMESERVER;
684                                                 else if (!strcmp(argv[a],"CINEON")) G.scene->r.imtype = R_CINEON;
685                                                 else if (!strcmp(argv[a],"DPX")) G.scene->r.imtype = R_DPX;
686                                                 else printf("\nError: Format from '-F' not known or not compiled in this release.\n");
687                                         }
688                                 } else {
689                                         printf("\nError: no blend loaded. cannot use '-x'.\n");
690                                 }
691                                 break;
692                                 
693                         case 't':
694                                 a++;
695                                 if(G.background) {
696                                         RE_set_max_threads(atoi(argv[a]));
697                                 }
698                                 break;
699                         case 'x': /* extension */
700                                 a++;
701                                 if (a < argc) {
702                                         if(G.scene) {
703                                                 if (argv[a][0] == '0') {
704                                                         G.scene->r.scemode &= ~R_EXTENSION;
705                                                 } else if (argv[a][0] == '1') {
706                                                         G.scene->r.scemode |= R_EXTENSION;
707                                                 } else {
708                                                         printf("\nError: Use '-x 1' or '-x 0' To set the extension option.\n");
709                                                 }
710                                         } else {
711                                                 printf("\nError: no blend loaded. order the arguments so '-o ' is after '-x '.\n");
712                                         }
713                                 } else {
714                                         printf("\nError: you must specify a path after '- '.\n");
715                                 }
716                                 break;
717                         }
718                 }
719                 else {
720                         
721                         /* Make the path absolute because its needed for relative linked blends to be found */
722                         int abs = 0;
723                         int filelen;
724                         char cwd[FILE_MAXDIR + FILE_MAXFILE];
725                         char filename[FILE_MAXDIR + FILE_MAXFILE];
726                         cwd[0] = filename[0] = '\0';
727                         
728                         BLI_strncpy(filename, argv[a], sizeof(filename));
729                         filelen = strlen(filename);
730                         
731                         /* relative path checks, could do more tests here... */
732 #ifdef WIN32
733                         /* Account for X:/ and X:\ - should be enough */
734                         if (filelen >= 3 && filename[1] == ':' && (filename[2] == '\\' || filename[2] == '/'))
735                                 abs = 1;
736 #else
737                         if (filelen >= 2 && filename[0] == '/')
738                                 abs = 1 ;
739 #endif
740                         if (!abs) {
741                                 BLI_getwdN(cwd); /* incase the full path to the blend isnt used */
742                                 
743                                 if (cwd[0] == '\0') {
744                                         printf(
745                                         "Could not get the current working directory - $PWD for an unknown reason.\n\t"
746                                         "Relative linked files will not load if the entire blend path is not used.\n\t"
747                                         "The 'Play' button may also fail.\n"
748                                         );
749                                 } else {
750                                         /* uses the blend path relative to cwd important for loading relative linked files.
751                                         *
752                                         * cwd should contain c:\ etc on win32 so the relbase can be NULL
753                                         * relbase being NULL also prevents // being misunderstood as relative to the current
754                                         * blend file which isnt a feature we want to use in this case since were dealing
755                                         * with a path from the command line, rather then from inside Blender */
756                                         
757                                         BLI_make_file_string(NULL, filename, cwd, argv[a]); 
758                                 }
759                         }
760                         
761                         if (G.background) {
762                                 BKE_read_file(filename, NULL);
763                                 sound_initialize_sounds();
764
765                                 /* happens for the UI on file reading too */
766                                 BKE_reset_undo();
767                                 BKE_write_undo("original");     /* save current state */
768                         } else {
769                                 /* we are not running in background mode here, but start blender in UI mode with 
770                                    a file - this should do everything a 'load file' does */
771                                 BIF_read_file(filename);
772                         }
773                 }
774         }
775
776         if(G.background) {
777                 /* actually incorrect, but works for now (ton) */
778                 exit_usiblender();
779         }
780
781         setscreen(G.curscreen);
782
783         if(G.main->scene.first==0) {
784                 sce= add_scene("1");
785                 set_scene(sce);
786         }
787
788         screenmain();
789
790         return 0;
791 } /* end of int main(argc,argv) */
792
793 static void error_cb(char *err)
794 {
795         error("%s", err);
796 }
797
798 static void mem_error_cb(char *errorStr)
799 {
800         fprintf(stderr, errorStr);
801         fflush(stderr);
802 }
803
804 void setCallbacks(void)
805 {
806         /* Error output from the alloc routines: */
807         MEM_set_error_callback(mem_error_cb);
808
809
810         /* BLI_blenlib: */
811
812         BLI_setErrorCallBack(error_cb); /* */
813         BLI_setInterruptCallBack(blender_test_break);
814
815 }