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