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