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