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