4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
30 #if defined(__linux__) && defined(__GNUC__)
35 #define OSX_SSE_FPE (defined(__APPLE__) && (defined(__i386__) || defined(__x86_64__)))
38 #include <xmmintrin.h>
44 /* for setuid / getuid */
46 #include <sys/types.h>
50 /* This little block needed for linking to Blender... */
52 #include "MEM_guardedalloc.h"
55 #include "BLI_winstuff.h"
59 #include "BLI_threads.h"
60 #include "BLI_scanfill.h" // for BLI_setErrorCallBack, TODO, move elsewhere
61 #include "BLI_utildefines.h"
64 #include "DNA_scene_types.h"
66 #include "BLI_blenlib.h"
68 #include "BKE_utildefines.h"
69 #include "BKE_blender.h"
70 #include "BKE_context.h"
72 #include "BKE_global.h"
74 #include "BKE_material.h"
75 #include "BKE_packedFile.h"
76 #include "BKE_scene.h"
78 #include "BKE_report.h"
79 #include "BKE_sound.h"
81 #include "IMB_imbuf.h" // for IMB_init
84 #include "BPY_extern.h"
87 #include "RE_pipeline.h"
89 //XXX #include "playanim_ext.h"
90 #include "ED_datafiles.h"
94 #include "RNA_define.h"
97 #include "GPU_extensions.h"
99 /* for passing information between creator and gameengine */
100 #ifdef WITH_GAMEENGINE
101 #include "GEN_messaging.h"
102 #include "SYS_System.h"
104 #define SYS_SystemHandle int
110 # include <sys/types.h>
111 # include <floatingpoint.h>
112 # include <sys/rtprio.h>
116 #include "binreloc.h"
121 extern char build_date[];
122 extern char build_time[];
123 extern char build_rev[];
124 extern char build_platform[];
125 extern char build_type[];
126 extern char build_cflags[];
127 extern char build_cxxflags[];
128 extern char build_linkflags[];
129 extern char build_system[];
132 /* Local Function prototypes */
133 static int print_help(int argc, char **argv, void *data);
134 static int print_version(int argc, char **argv, void *data);
136 /* for the callbacks: */
138 extern int pluginapi_force_ref(void); /* from blenpluginapi:pluginapi.c */
140 char bprogname[FILE_MAXDIR+FILE_MAXFILE]; /* from blenpluginapi:pluginapi.c */
141 char btempdir[FILE_MAXDIR+FILE_MAXFILE];
143 #define BLEND_VERSION_STRING_FMT "Blender %d.%02d (sub %d) Build\n", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION
145 /* Initialise callbacks for the modules that need them */
146 static void setCallbacks(void);
148 /* set breakpoints here when running in debug mode, useful to catch floating point errors */
149 #if defined(__sgi) || defined(__linux__) || defined(_WIN32) || OSX_SSE_FPE
150 static void fpe_handler(int UNUSED(sig))
152 // printf("SIGFPE trapped\n");
156 /* handling ctrl-c event in console */
157 static void blender_esc(int sig)
159 static int count = 0;
161 G.afbreek = 1; /* forces render loop to read queue, not sure if its needed */
165 printf("\nBlender killed\n");
168 printf("\nSent an internal break event. Press ^C again to kill Blender\n");
173 /* buildinfo can have quotes */
175 static void strip_quotes(char *str)
178 int len= strlen(str) - 1;
179 memmove(str, str+1, len);
180 if(str[len-1] == '"') {
187 static int print_version(int UNUSED(argc), char **UNUSED(argv), void *UNUSED(data))
189 printf (BLEND_VERSION_STRING_FMT);
191 printf ("\tbuild date: %s\n", build_date);
192 printf ("\tbuild time: %s\n", build_time);
193 printf ("\tbuild revision: %s\n", build_rev);
194 printf ("\tbuild platform: %s\n", build_platform);
195 printf ("\tbuild type: %s\n", build_type);
196 printf ("\tbuild c flags: %s\n", build_cflags);
197 printf ("\tbuild c++ flags: %s\n", build_cxxflags);
198 printf ("\tbuild link flags: %s\n", build_linkflags);
199 printf ("\tbuild system: %s\n", build_system);
206 static int print_help(int UNUSED(argc), char **UNUSED(argv), void *data)
208 bArgs *ba = (bArgs*)data;
210 printf (BLEND_VERSION_STRING_FMT);
211 printf ("Usage: blender [args ...] [file] [args ...]\n\n");
213 printf ("Render Options:\n");
214 BLI_argsPrintArgDoc(ba, "--background");
215 BLI_argsPrintArgDoc(ba, "--render-anim");
216 BLI_argsPrintArgDoc(ba, "--scene");
217 BLI_argsPrintArgDoc(ba, "--render-frame");
218 BLI_argsPrintArgDoc(ba, "--frame-start");
219 BLI_argsPrintArgDoc(ba, "--frame-end");
220 BLI_argsPrintArgDoc(ba, "--frame-jump");
221 BLI_argsPrintArgDoc(ba, "--render-output");
222 BLI_argsPrintArgDoc(ba, "--engine");
225 printf ("Format Options:\n");
226 BLI_argsPrintArgDoc(ba, "--render-format");
227 BLI_argsPrintArgDoc(ba, "--use-extension");
228 BLI_argsPrintArgDoc(ba, "--threads");
231 printf ("Animation Playback Options:\n");
232 BLI_argsPrintArgDoc(ba, "-a");
235 printf ("Window Options:\n");
236 BLI_argsPrintArgDoc(ba, "--window-border");
237 BLI_argsPrintArgDoc(ba, "--window-borderless");
238 BLI_argsPrintArgDoc(ba, "--window-geometry");
241 printf ("Game Engine Specific Options:\n");
242 BLI_argsPrintArgDoc(ba, "-g");
245 printf ("Misc Options:\n");
246 BLI_argsPrintArgDoc(ba, "--debug");
247 BLI_argsPrintArgDoc(ba, "--debug-fpe");
251 BLI_argsPrintArgDoc(ba, "-nojoystick");
252 BLI_argsPrintArgDoc(ba, "-noglsl");
253 BLI_argsPrintArgDoc(ba, "-noaudio");
254 BLI_argsPrintArgDoc(ba, "-setaudio");
258 BLI_argsPrintArgDoc(ba, "--help");
262 BLI_argsPrintArgDoc(ba, "--enable-autoexec");
263 BLI_argsPrintArgDoc(ba, "--disable-autoexec");
267 BLI_argsPrintArgDoc(ba, "--python");
268 BLI_argsPrintArgDoc(ba, "--python-console");
271 BLI_argsPrintArgDoc(ba, "-R");
273 BLI_argsPrintArgDoc(ba, "--version");
275 BLI_argsPrintArgDoc(ba, "--");
277 printf ("Other Options:\n");
278 BLI_argsPrintOtherDoc(ba);
280 printf ("Argument Parsing:\n");
281 printf ("\targuments must be separated by white space. eg\n");
282 printf ("\t\t\"blender -ba test.blend\"\n");
283 printf ("\t...will ignore the 'a'\n");
284 printf ("\t\t\"blender -b test.blend -f8\"\n");
285 printf ("\t...will ignore 8 because there is no space between the -f and the frame value\n\n");
287 printf ("Argument Order:\n");
288 printf ("Arguments are executed in the order they are given. eg\n");
289 printf ("\t\t\"blender --background test.blend --render-frame 1 --render-output /tmp\"\n");
290 printf ("\t...will not render to /tmp because '--render-frame 1' renders before the output path is set\n");
291 printf ("\t\t\"blender --background --render-output /tmp test.blend --render-frame 1\"\n");
292 printf ("\t...will not render to /tmp because loading the blend file overwrites the render output that was set\n");
293 printf ("\t\t\"blender --background test.blend --render-output /tmp --render-frame 1\" works as expected.\n\n");
295 printf ("\nEnvironment Variables:\n");
296 printf (" $BLENDER_USER_CONFIG Directory for user configuration files.\n");
297 printf (" $BLENDER_SYSTEM_CONFIG Directory for system wide configuration files.\n");
298 printf (" $BLENDER_USER_SCRIPTS Directory for user scripts.\n");
299 printf (" $BLENDER_SYSTEM_SCRIPTS Directory for system wide scripts.\n");
300 printf (" $BLENDER_USER_DATAFILES Directory for user data files (icons, translations, ..).\n");
301 printf (" $BLENDER_SYSTEM_DATAFILES Directory for system wide data files.\n");
302 printf (" $BLENDER_SYSTEM_PYTHON Directory for system python libraries.\n");
304 printf (" $TEMP Store temporary files here.\n");
306 printf (" $TMP or $TMPDIR Store temporary files here.\n");
309 printf (" $SDL_AUDIODRIVER LibSDL audio driver - alsa, esd, dma.\n");
311 printf (" $PYTHONHOME Path to the python directory, eg. /usr/lib/python.\n\n");
319 double PIL_check_seconds_timer(void);
321 /* XXX This was here to fix a crash when running python scripts
322 * with -P that used the screen.
324 * static void main_init_screen( void )
326 setscreen(G.curscreen);
328 if(G.main->scene.first==0) {
329 set_scene( add_scene("1") );
333 static int end_arguments(int UNUSED(argc), char **UNUSED(argv), void *UNUSED(data))
338 static int enable_python(int UNUSED(argc), char **UNUSED(argv), void *UNUSED(data))
340 G.f |= G_SCRIPT_AUTOEXEC;
344 static int disable_python(int UNUSED(argc), char **UNUSED(argv), void *UNUSED(data))
346 G.f &= ~G_SCRIPT_AUTOEXEC;
350 static int background_mode(int UNUSED(argc), char **UNUSED(argv), void *UNUSED(data))
356 static int debug_mode(int UNUSED(argc), char **UNUSED(argv), void *data)
358 G.f |= G_DEBUG; /* std output printf's */
359 printf(BLEND_VERSION_STRING_FMT);
360 MEM_set_memory_debug();
363 printf("Build: %s %s %s %s\n", build_date, build_time, build_platform, build_type);
364 #endif // NAN_BUILDINFO
370 static int set_fpe(int UNUSED(argc), char **UNUSED(argv), void *UNUSED(data))
372 #if defined(__sgi) || defined(__linux__) || defined(_WIN32) || OSX_SSE_FPE
373 /* zealous but makes float issues a heck of a lot easier to find!
374 * set breakpoints on fpe_handler */
375 signal(SIGFPE, fpe_handler);
377 # if defined(__linux__) && defined(__GNUC__)
378 feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW );
379 # endif /* defined(__linux__) && defined(__GNUC__) */
381 /* OSX uses SSE for floating point by default, so here
382 * use SSE instructions to throw floating point exceptions */
383 _MM_SET_EXCEPTION_MASK(_MM_MASK_MASK &~
384 (_MM_MASK_OVERFLOW|_MM_MASK_INVALID|_MM_MASK_DIV_ZERO));
385 # endif /* OSX_SSE_FPE */
386 # if defined(_WIN32) && defined(_MSC_VER)
387 _controlfp_s(NULL, 0, _MCW_EM); /* enables all fp exceptions */
388 _controlfp_s(NULL, _EM_DENORMAL | _EM_UNDERFLOW | _EM_INEXACT, _MCW_EM); /* hide the ones we don't care about */
389 # endif /* _WIN32 && _MSC_VER */
395 static int playback_mode(int UNUSED(argc), char **UNUSED(argv), void *UNUSED(data))
397 /* not if -b was given first */
398 if (G.background == 0) {
400 // XXX playanim(argc, argv); /* not the same argc and argv as before */
407 static int prefsize(int argc, char **argv, void *UNUSED(data))
409 int stax, stay, sizx, sizy;
412 printf ("-p requires four arguments\n");
421 WM_setprefsize(stax, stay, sizx, sizy);
426 static int with_borders(int UNUSED(argc), char **UNUSED(argv), void *UNUSED(data))
428 WM_setinitialstate_normal();
432 static int without_borders(int UNUSED(argc), char **UNUSED(argv), void *UNUSED(data))
434 WM_setinitialstate_fullscreen();
438 static int register_extension(int UNUSED(argc), char **UNUSED(argv), void *data)
441 char *path = BLI_argsArgv(data)[0];
442 RegisterBlendExtension(path);
444 (void)data; /* unused */
450 static int no_joystick(int UNUSED(argc), char **UNUSED(argv), void *data)
452 #ifndef WITH_GAMEENGINE
455 SYS_SystemHandle *syshandle = data;
458 don't initialize joysticks if user doesn't want to use joysticks
459 failed joystick initialization delays over 5 seconds, before game engine start
461 SYS_WriteCommandLineInt(*syshandle, "nojoystick",1);
462 if (G.f & G_DEBUG) printf("disabling nojoystick\n");
468 static int no_glsl(int UNUSED(argc), char **UNUSED(argv), void *UNUSED(data))
470 GPU_extensions_disable();
474 static int no_audio(int UNUSED(argc), char **UNUSED(argv), void *UNUSED(data))
476 sound_force_device(0);
480 static int set_audio(int argc, char **argv, void *UNUSED(data))
483 printf("-setaudio require one argument\n");
487 sound_force_device(sound_define_from_str(argv[1]));
491 static int set_output(int argc, char **argv, void *data)
495 if (CTX_data_scene(C)) {
496 Scene *scene= CTX_data_scene(C);
497 BLI_strncpy(scene->r.pic, argv[1], FILE_MAXDIR);
499 printf("\nError: no blend loaded. cannot use '-o / --render-output'.\n");
503 printf("\nError: you must specify a path after '-o / --render-output'.\n");
508 static int set_engine(int argc, char **argv, void *data)
513 if (!strcmp(argv[1],"help"))
515 RenderEngineType *type = NULL;
517 for( type = R_engines.first; type; type = type->next )
519 printf("\t%s\n", type->idname);
525 if (CTX_data_scene(C)==NULL)
527 printf("\nError: no blend loaded. order the arguments so '-E / --engine ' is after a blend is loaded.\n");
531 Scene *scene= CTX_data_scene(C);
532 RenderData *rd = &scene->r;
533 RenderEngineType *type = NULL;
535 for( type = R_engines.first; type; type = type->next )
537 if (!strcmp(argv[1],type->idname))
539 BLI_strncpy(rd->engine, type->idname, sizeof(rd->engine));
549 printf("\nEngine not specified.\n");
554 static int set_image_type(int argc, char **argv, void *data)
558 char *imtype = argv[1];
559 if (CTX_data_scene(C)==NULL) {
560 printf("\nError: no blend loaded. order the arguments so '-F / --render-format' is after the blend is loaded.\n");
562 Scene *scene= CTX_data_scene(C);
563 if (!strcmp(imtype,"TGA")) scene->r.imtype = R_TARGA;
564 else if (!strcmp(imtype,"IRIS")) scene->r.imtype = R_IRIS;
566 else if (!strcmp(imtype,"DDS")) scene->r.imtype = R_DDS;
568 else if (!strcmp(imtype,"JPEG")) scene->r.imtype = R_JPEG90;
569 else if (!strcmp(imtype,"IRIZ")) scene->r.imtype = R_IRIZ;
570 else if (!strcmp(imtype,"RAWTGA")) scene->r.imtype = R_RAWTGA;
571 else if (!strcmp(imtype,"AVIRAW")) scene->r.imtype = R_AVIRAW;
572 else if (!strcmp(imtype,"AVIJPEG")) scene->r.imtype = R_AVIJPEG;
573 else if (!strcmp(imtype,"PNG")) scene->r.imtype = R_PNG;
574 else if (!strcmp(imtype,"AVICODEC")) scene->r.imtype = R_AVICODEC;
575 else if (!strcmp(imtype,"QUICKTIME")) scene->r.imtype = R_QUICKTIME;
576 else if (!strcmp(imtype,"BMP")) scene->r.imtype = R_BMP;
578 else if (!strcmp(imtype,"HDR")) scene->r.imtype = R_RADHDR;
581 else if (!strcmp(imtype,"TIFF")) scene->r.imtype = R_TIFF;
584 else if (!strcmp(imtype,"EXR")) scene->r.imtype = R_OPENEXR;
585 else if (!strcmp(imtype,"MULTILAYER")) scene->r.imtype = R_MULTILAYER;
587 else if (!strcmp(imtype,"MPEG")) scene->r.imtype = R_FFMPEG;
588 else if (!strcmp(imtype,"FRAMESERVER")) scene->r.imtype = R_FRAMESERVER;
590 else if (!strcmp(imtype,"CINEON")) scene->r.imtype = R_CINEON;
591 else if (!strcmp(imtype,"DPX")) scene->r.imtype = R_DPX;
594 else if (!strcmp(imtype,"JP2")) scene->r.imtype = R_JP2;
596 else printf("\nError: Format from '-F / --render-format' not known or not compiled in this release.\n");
600 printf("\nError: you must specify a format after '-F / --render-foramt'.\n");
605 static int set_threads(int argc, char **argv, void *UNUSED(data))
609 RE_set_max_threads(atoi(argv[1]));
611 printf("Warning: threads can only be set in background mode\n");
615 printf("\nError: you must specify a number of threads between 0 and 8 '-t / --threads'.\n");
620 static int set_extension(int argc, char **argv, void *data)
624 if (CTX_data_scene(C)) {
625 Scene *scene= CTX_data_scene(C);
626 if (argv[1][0] == '0') {
627 scene->r.scemode &= ~R_EXTENSION;
628 } else if (argv[1][0] == '1') {
629 scene->r.scemode |= R_EXTENSION;
631 printf("\nError: Use '-x 1 / -x 0' To set the extension option or '--use-extension'\n");
634 printf("\nError: no blend loaded. order the arguments so '-o ' is after '-x '.\n");
638 printf("\nError: you must specify a path after '- '.\n");
643 static int set_ge_parameters(int argc, char **argv, void *data)
646 #ifdef WITH_GAMEENGINE
647 SYS_SystemHandle syshandle = *(SYS_SystemHandle*)data;
653 gameengine parameters are automaticly put into system
654 -g [paramname = value]
658 -g maxvertexarraysize = 512
663 char* paramname = argv[a];
664 /* check for single value versus assignment */
665 if (a+1 < argc && (*(argv[a+1]) == '='))
672 #ifdef WITH_GAMEENGINE
673 SYS_WriteCommandLineString(syshandle,paramname,argv[a]);
677 printf("error: argument assignment (%s) without value.\n",paramname);
683 #ifdef WITH_GAMEENGINE
684 SYS_WriteCommandLineInt(syshandle,argv[a],1);
687 if (!strcmp(argv[a],"nomipmap"))
689 GPU_set_mipmap(0); //doMipMap = 0;
692 if (!strcmp(argv[a],"linearmipmap"))
694 GPU_set_linear_mipmap(1); //linearMipMap = 1;
698 } /* if (*(argv[a+1]) == '=') */
704 static int render_frame(int argc, char **argv, void *data)
707 if (CTX_data_scene(C)) {
708 Main *bmain= CTX_data_main(C);
709 Scene *scene= CTX_data_scene(C);
712 Render *re = RE_NewRender(scene->id.name);
718 frame= scene->r.sfra + atoi(argv[1]+1);
721 frame= (scene->r.efra - atoi(argv[1]+1)) + 1;
724 frame= atoi(argv[1]);
728 BKE_reports_init(&reports, RPT_PRINT);
730 frame = MIN2(MAXFRAME, MAX2(MINAFRAME, frame));
732 RE_BlenderAnim(re, bmain, scene, scene->lay, frame, frame, scene->r.frame_step, &reports);
735 printf("\nError: frame number must follow '-f / --render-frame'.\n");
739 printf("\nError: no blend loaded. cannot use '-f / --render-frame'.\n");
744 static int render_animation(int UNUSED(argc), char **UNUSED(argv), void *data)
747 if (CTX_data_scene(C)) {
748 Main *bmain= CTX_data_main(C);
749 Scene *scene= CTX_data_scene(C);
750 Render *re= RE_NewRender(scene->id.name);
752 BKE_reports_init(&reports, RPT_PRINT);
753 RE_BlenderAnim(re, bmain, scene, scene->lay, scene->r.sfra, scene->r.efra, scene->r.frame_step, &reports);
755 printf("\nError: no blend loaded. cannot use '-a'.\n");
760 static int set_scene(int argc, char **argv, void *data)
764 Scene *sce= set_scene_name(CTX_data_main(C), argv[1]);
766 CTX_data_scene_set(C, sce);
770 printf("\nError: Scene name must follow '-S / --scene'.\n");
775 static int set_start_frame(int argc, char **argv, void *data)
778 if (CTX_data_scene(C)) {
779 Scene *scene= CTX_data_scene(C);
781 int frame = atoi(argv[1]);
782 (scene->r.sfra) = CLAMPIS(frame, MINFRAME, MAXFRAME);
785 printf("\nError: frame number must follow '-s / --frame-start'.\n");
789 printf("\nError: no blend loaded. cannot use '-s / --frame-start'.\n");
794 static int set_end_frame(int argc, char **argv, void *data)
797 if (CTX_data_scene(C)) {
798 Scene *scene= CTX_data_scene(C);
800 int frame = atoi(argv[1]);
801 (scene->r.efra) = CLAMPIS(frame, MINFRAME, MAXFRAME);
804 printf("\nError: frame number must follow '-e / --frame-end'.\n");
808 printf("\nError: no blend loaded. cannot use '-e / --frame-end'.\n");
813 static int set_skip_frame(int argc, char **argv, void *data)
816 if (CTX_data_scene(C)) {
817 Scene *scene= CTX_data_scene(C);
819 int frame = atoi(argv[1]);
820 (scene->r.frame_step) = CLAMPIS(frame, 1, MAXFRAME);
823 printf("\nError: number of frames to step must follow '-j / --frame-jump'.\n");
827 printf("\nError: no blend loaded. cannot use '-j / --frame-jump'.\n");
832 /* macro for ugly context setup/reset */
834 #define BPY_CTX_SETUP(_cmd) \
836 wmWindowManager *wm= CTX_wm_manager(C); \
837 wmWindow *prevwin= CTX_wm_window(C); \
838 Scene *prevscene= CTX_data_scene(C); \
839 if(wm->windows.first) { \
840 CTX_wm_window_set(C, wm->windows.first); \
842 CTX_wm_window_set(C, prevwin); \
845 fprintf(stderr, "Python script \"%s\" running with missing context data.\n", argv[1]); \
848 CTX_data_scene_set(C, prevscene); \
851 #endif /* WITH_PYTHON */
853 static int run_python(int argc, char **argv, void *data)
858 /* workaround for scripts not getting a bpy.context.scene, causes internal errors elsewhere */
860 /* Make the path absolute because its needed for relative linked blends to be found */
861 char filename[FILE_MAXDIR + FILE_MAXFILE];
862 BLI_strncpy(filename, argv[1], sizeof(filename));
863 BLI_path_cwd(filename);
865 BPY_CTX_SETUP(BPY_filepath_exec(C, filename, NULL))
869 printf("\nError: you must specify a Python script after '-P / --python'.\n");
873 (void)argc; (void)argv; (void)data; /* unused */
874 printf("This blender was built without python support\n");
876 #endif /* WITH_PYTHON */
879 static int run_python_console(int UNUSED(argc), char **argv, void *data)
884 BPY_CTX_SETUP(BPY_string_exec(C, "__import__('code').interact()"))
888 (void)argv; (void)data; /* unused */
889 printf("This blender was built without python support\n");
891 #endif /* WITH_PYTHON */
894 static int load_file(int UNUSED(argc), char **argv, void *data)
898 /* Make the path absolute because its needed for relative linked blends to be found */
899 char filename[FILE_MAXDIR + FILE_MAXFILE];
900 BLI_strncpy(filename, argv[0], sizeof(filename));
901 BLI_path_cwd(filename);
904 int retval = BKE_read_file(C, filename, NULL);
906 /*we successfully loaded a blend file, get sure that
908 if (retval != BKE_READ_FILE_FAIL) {
909 wmWindowManager *wm= CTX_wm_manager(C);
911 /* special case, 2.4x files */
912 if(wm==NULL && CTX_data_main(C)->wm.first==NULL) {
913 extern void wm_add_default(bContext *C);
915 /* wm_add_default() needs the screen to be set. */
916 CTX_wm_screen_set(C, CTX_data_main(C)->screen.first);
920 CTX_wm_manager_set(C, NULL); /* remove wm to force check */
923 if (CTX_wm_manager(C) == NULL) CTX_wm_manager_set(C, wm); /* reset wm */
926 /* WM_read_file() runs normally but since we're in background mode do here */
928 /* run any texts that were loaded in and flagged as modules */
930 BPY_modules_load_user(C);
933 /* happens for the UI on file reading too (huh? (ton))*/
934 // XXX BKE_reset_undo();
935 // BKE_write_undo("original"); /* save current state */
937 /* we are not running in background mode here, but start blender in UI mode with
938 a file - this should do everything a 'load file' does */
940 BKE_reports_init(&reports, RPT_PRINT);
941 WM_read_file(C, filename, &reports);
942 BKE_reports_clear(&reports);
950 void setupArguments(bContext *C, bArgs *ba, SYS_SystemHandle *syshandle)
952 static char output_doc[] = "<path>"
953 "\n\tSet the render path and file name."
954 "\n\tUse // at the start of the path to"
955 "\n\t\trender relative to the blend file."
956 "\n\tThe # characters are replaced by the frame number, and used to define zero padding."
957 "\n\t\tani_##_test.png becomes ani_01_test.png"
958 "\n\t\ttest-######.png becomes test-000001.png"
959 "\n\t\tWhen the filename does not contain #, The suffix #### is added to the filename"
960 "\n\tThe frame number will be added at the end of the filename."
961 "\n\t\teg: blender -b foobar.blend -o //render_ -F PNG -x 1 -a"
962 "\n\t\t//render_ becomes //render_####, writing frames as //render_0001.png//";
964 static char format_doc[] = "<format>"
965 "\n\tSet the render format, Valid options are..."
966 "\n\t\tTGA IRIS JPEG MOVIE IRIZ RAWTGA"
967 "\n\t\tAVIRAW AVIJPEG PNG BMP FRAMESERVER"
968 "\n\t(formats that can be compiled into blender, not available on all systems)"
969 "\n\t\tHDR TIFF EXR MULTILAYER MPEG AVICODEC QUICKTIME CINEON DPX DDS";
971 static char playback_doc[] = "<options> <file(s)>"
972 "\n\tPlayback <file(s)>, only operates this way when not running in background."
973 "\n\t\t-p <sx> <sy>\tOpen with lower left corner at <sx>, <sy>"
974 "\n\t\t-m\t\tRead from disk (Don't buffer)"
975 "\n\t\t-f <fps> <fps-base>\t\tSpecify FPS to start with"
976 "\n\t\t-j <frame>\tSet frame step to <frame>";
978 static char game_doc[] = "Game Engine specific options"
979 "\n\t-g fixedtime\t\tRun on 50 hertz without dropping frames"
980 "\n\t-g vertexarrays\t\tUse Vertex Arrays for rendering (usually faster)"
981 "\n\t-g nomipmap\t\tNo Texture Mipmapping"
982 "\n\t-g linearmipmap\t\tLinear Texture Mipmapping instead of Nearest (default)";
984 static char debug_doc[] = "\n\tTurn debugging on\n"
985 "\n\t* Prints every operator call and their arguments"
986 "\n\t* Disables mouse grab (to interact with a debugger in some cases)"
987 "\n\t* Keeps python sys.stdin rather then setting it to None";
989 //BLI_argsAdd(ba, pass, short_arg, long_arg, doc, cb, C);
991 /* end argument processing after -- */
992 BLI_argsAdd(ba, -1, "--", NULL, "\n\tEnds option processing, following arguments passed unchanged. Access via python's sys.argv", end_arguments, NULL);
994 /* first pass: background mode, disable python and commands that exit after usage */
995 BLI_argsAdd(ba, 1, "-h", "--help", "\n\tPrint this help text and exit", print_help, ba);
997 BLI_argsAdd(ba, 1, "/?", NULL, "\n\tPrint this help text and exit (windows only)", print_help, ba);
999 BLI_argsAdd(ba, 1, "-v", "--version", "\n\tPrint Blender version and exit", print_version, NULL);
1001 BLI_argsAdd(ba, 1, "-y", "--enable-autoexec", "\n\tEnable automatic python script execution (default)", enable_python, NULL);
1002 BLI_argsAdd(ba, 1, "-Y", "--disable-autoexec", "\n\tDisable automatic python script execution (pydrivers, pyconstraints, pynodes)", disable_python, NULL);
1004 BLI_argsAdd(ba, 1, "-b", "--background", "<file>\n\tLoad <file> in background (often used for UI-less rendering)", background_mode, NULL);
1006 BLI_argsAdd(ba, 1, "-a", NULL, playback_doc, playback_mode, NULL);
1008 BLI_argsAdd(ba, 1, "-d", "--debug", debug_doc, debug_mode, ba);
1009 BLI_argsAdd(ba, 1, NULL, "--debug-fpe", "\n\tEnable floating point exceptions", set_fpe, NULL);
1011 /* second pass: custom window stuff */
1012 BLI_argsAdd(ba, 2, "-p", "--window-geometry", "<sx> <sy> <w> <h>\n\tOpen with lower left corner at <sx>, <sy> and width and height as <w>, <h>", prefsize, NULL);
1013 BLI_argsAdd(ba, 2, "-w", "--window-border", "\n\tForce opening with borders (default)", with_borders, NULL);
1014 BLI_argsAdd(ba, 2, "-W", "--window-borderless", "\n\tForce opening without borders", without_borders, NULL);
1015 BLI_argsAdd(ba, 2, "-R", NULL, "\n\tRegister .blend extension (windows only)", register_extension, ba);
1017 /* third pass: disabling things and forcing settings */
1018 BLI_argsAddCase(ba, 3, "-nojoystick", 1, NULL, 0, "\n\tDisable joystick support", no_joystick, syshandle);
1019 BLI_argsAddCase(ba, 3, "-noglsl", 1, NULL, 0, "\n\tDisable GLSL shading", no_glsl, NULL);
1020 BLI_argsAddCase(ba, 3, "-noaudio", 1, NULL, 0, "\n\tForce sound system to None", no_audio, NULL);
1021 BLI_argsAddCase(ba, 3, "-setaudio", 1, NULL, 0, "\n\tForce sound system to a specific device\n\tNULL SDL OPENAL JACK", set_audio, NULL);
1023 /* fourth pass: processing arguments */
1024 BLI_argsAdd(ba, 4, "-g", NULL, game_doc, set_ge_parameters, syshandle);
1025 BLI_argsAdd(ba, 4, "-f", "--render-frame", "<frame>\n\tRender frame <frame> and save it.\n\t+<frame> start frame relative, -<frame> end frame relative.", render_frame, C);
1026 BLI_argsAdd(ba, 4, "-a", "--render-anim", "\n\tRender frames from start to end (inclusive)", render_animation, C);
1027 BLI_argsAdd(ba, 4, "-S", "--scene", "<name>\n\tSet the active scene <name> for rendering", set_scene, C);
1028 BLI_argsAdd(ba, 4, "-s", "--frame-start", "<frame>\n\tSet start to frame <frame> (use before the -a argument)", set_start_frame, C);
1029 BLI_argsAdd(ba, 4, "-e", "--frame-end", "<frame>\n\tSet end to frame <frame> (use before the -a argument)", set_end_frame, C);
1030 BLI_argsAdd(ba, 4, "-j", "--frame-jump", "<frames>\n\tSet number of frames to step forward after each rendered frame", set_skip_frame, C);
1031 BLI_argsAdd(ba, 4, "-P", "--python", "<filename>\n\tRun the given Python script (filename or Blender Text)", run_python, C);
1032 BLI_argsAdd(ba, 4, NULL, "--python-console", "\n\tRun blender with an interactive console", run_python_console, C);
1034 BLI_argsAdd(ba, 4, "-o", "--render-output", output_doc, set_output, C);
1035 BLI_argsAdd(ba, 4, "-E", "--engine", "<engine>\n\tSpecify the render engine\n\tuse -E help to list available engines", set_engine, C);
1037 BLI_argsAdd(ba, 4, "-F", "--render-format", format_doc, set_image_type, C);
1038 BLI_argsAdd(ba, 4, "-t", "--threads", "<threads>\n\tUse amount of <threads> for rendering in background\n\t[1-" STRINGIFY(BLENDER_MAX_THREADS) "], 0 for systems processor count.", set_threads, NULL);
1039 BLI_argsAdd(ba, 4, "-x", "--use-extension", "<bool>\n\tSet option to add the file extension to the end of the file", set_extension, C);
1043 int main(int argc, char **argv)
1045 SYS_SystemHandle syshandle;
1046 bContext *C= CTX_create();
1049 #ifdef WITH_BINRELOC
1055 /* patch to ignore argument finder gives us (pid?) */
1056 if (argc==2 && strncmp(argv[1], "-psn_", 5)==0) {
1057 extern int GHOST_HACK_getFirstFile(char buf[]);
1058 static char firstfilebuf[512];
1062 if (GHOST_HACK_getFirstFile(firstfilebuf)) {
1064 argv[1]= firstfilebuf;
1074 // copy path to executable in bprogname. playanim and creting runtimes
1077 BLI_where_am_i(bprogname, argv[0]);
1080 strip_quotes(build_date);
1081 strip_quotes(build_time);
1082 strip_quotes(build_rev);
1083 strip_quotes(build_platform);
1084 strip_quotes(build_type);
1085 strip_quotes(build_cflags);
1086 strip_quotes(build_cxxflags);
1087 strip_quotes(build_linkflags);
1088 strip_quotes(build_system);
1091 BLI_threadapi_init();
1096 /* Hack - force inclusion of the plugin api functions,
1097 * see blenpluginapi:pluginapi.c
1099 pluginapi_force_ref();
1103 initglobals(); /* blender.c */
1107 #ifdef WITH_GAMEENGINE
1108 syshandle = SYS_GetSystem();
1109 GEN_init_messaging_system();
1114 /* first test for background */
1115 ba = BLI_argsInit(argc, argv); /* skip binary path */
1116 setupArguments(C, ba, &syshandle);
1118 BLI_argsParse(ba, 1, NULL, NULL);
1121 setuid(getuid()); /* end superuser */
1125 /* for all platforms, even windos has it! */
1126 if(G.background) signal(SIGINT, blender_esc); /* ctrl c out bg render */
1128 /* background render uses this font too */
1129 BKE_font_register_builtin(datatoc_Bfont, datatoc_Bfont_size);
1131 /* Initialiaze ffmpeg if built in, also needed for bg mode if videos are
1132 rendered via ffmpeg */
1135 init_def_material();
1137 if(G.background==0) {
1138 BLI_argsParse(ba, 2, NULL, NULL);
1139 BLI_argsParse(ba, 3, NULL, NULL);
1141 WM_init(C, argc, argv);
1143 /* this is properly initialized with user defs, but this is default */
1144 BLI_where_is_temp( btempdir, 1 ); /* call after loading the startup.blend so we can read U.tempdir */
1147 BLI_setenv("SDL_VIDEODRIVER", "dummy");
1151 BLI_argsParse(ba, 3, NULL, NULL);
1153 WM_init(C, argc, argv);
1155 BLI_where_is_temp( btempdir, 0 ); /* call after loading the startup.blend so we can read U.tempdir */
1159 * NOTE: the U.pythondir string is NULL until WM_init() is executed,
1160 * so we provide the BPY_ function below to append the user defined
1161 * pythondir to Python's sys.path at this point. Simply putting
1162 * WM_init() before BPY_python_start() crashes Blender at startup.
1163 * Update: now this function also inits the bpymenus, which also depend
1167 // TODO - U.pythondir
1169 printf("\n* WARNING * - Blender compiled without Python!\nthis is not intended for typical usage\n\n");
1172 CTX_py_init_set(C, 1);
1175 /* OK we are ready for it */
1176 BLI_argsParse(ba, 4, load_file, C);
1181 /* actually incorrect, but works for now (ton) */
1186 if((G.fileflags & G_FILE_AUTOPLAY) && (G.f & G_SCRIPT_AUTOEXEC))
1191 else if(!G.file_loaded)
1198 /*XXX if (scr_init==0) {
1202 screenmain();*/ /* main display loop */
1205 } /* end of int main(argc,argv) */
1207 static void error_cb(char *err)
1210 printf("%s\n", err); /* XXX do this in WM too */
1213 static void mem_error_cb(const char *errorStr)
1215 fputs(errorStr, stderr);
1219 static void setCallbacks(void)
1221 /* Error output from the alloc routines: */
1222 MEM_set_error_callback(mem_error_cb);
1227 BLI_setErrorCallBack(error_cb); /* */
1228 // XXX BLI_setInterruptCallBack(blender_test_break);