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