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