2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file creator/creator.c
33 #if defined(__linux__) && defined(__GNUC__)
38 #if (defined(__APPLE__) && (defined(__i386__) || defined(__x86_64__)))
40 # include <xmmintrin.h>
45 # include <process.h> /* getpid */
47 # include <unistd.h> /* getpid */
56 #if defined(__linux__) || defined(__APPLE__)
57 # include <execinfo.h>
58 #elif defined(_MSV_VER)
68 /* This little block needed for linking to Blender... */
70 #include "MEM_guardedalloc.h"
73 # include "BLI_winstuff.h"
77 #include "BLI_threads.h"
78 #include "BLI_utildefines.h"
79 #include "BLI_callbacks.h"
80 #include "BLI_blenlib.h"
81 #include "BLI_mempool.h"
84 #include "DNA_scene_types.h"
85 #include "DNA_userdef_types.h"
87 #include "BKE_blender.h"
88 #include "BKE_brush.h"
89 #include "BKE_context.h"
90 #include "BKE_depsgraph.h" /* for DAG_on_visible_update */
92 #include "BKE_global.h"
93 #include "BKE_library.h"
95 #include "BKE_material.h"
96 #include "BKE_modifier.h"
97 #include "BKE_packedFile.h"
98 #include "BKE_scene.h"
100 #include "BKE_report.h"
101 #include "BKE_sound.h"
102 #include "BKE_image.h"
104 #include "IMB_imbuf.h" /* for IMB_init */
107 #include "BPY_extern.h"
110 #include "RE_engine.h"
111 #include "RE_pipeline.h"
113 #include "ED_datafiles.h"
117 #include "RNA_define.h"
119 #include "GPU_draw.h"
120 #include "GPU_extensions.h"
122 #ifdef WITH_FREESTYLE
123 # include "FRS_freestyle.h"
126 #ifdef WITH_BUILDINFO_HEADER
130 /* for passing information between creator and gameengine */
131 #ifdef WITH_GAMEENGINE
132 # include "BL_System.h"
134 # define SYS_SystemHandle int
140 # include <sys/types.h>
141 # include <floatingpoint.h>
142 # include <sys/rtprio.h>
146 # include "binreloc.h"
150 # include "libmv-capi.h"
153 /* from buildinfo.c */
155 extern char build_date[];
156 extern char build_time[];
157 extern char build_hash[];
158 extern unsigned long build_commit_timestamp;
160 /* TODO(sergey): ideally size need to be in sync with buildinfo.c */
161 extern char build_commit_date[16];
162 extern char build_commit_time[16];
164 extern char build_branch[];
165 extern char build_platform[];
166 extern char build_type[];
167 extern char build_cflags[];
168 extern char build_cxxflags[];
169 extern char build_linkflags[];
170 extern char build_system[];
173 /* Local Function prototypes */
174 #ifdef WITH_PYTHON_MODULE
175 int main_python_enter(int argc, const char **argv);
176 void main_python_exit(void);
178 static int print_help(int argc, const char **argv, void *data);
179 static int print_version(int argc, const char **argv, void *data);
182 /* for the callbacks: */
183 #ifndef WITH_PYTHON_MODULE
184 #define BLEND_VERSION_FMT "Blender %d.%02d (sub %d)"
185 #define BLEND_VERSION_ARG BLENDER_VERSION / 100, BLENDER_VERSION % 100, BLENDER_SUBVERSION
186 /* pass directly to printf */
187 #define BLEND_VERSION_STRING_FMT BLEND_VERSION_FMT "\n", BLEND_VERSION_ARG
190 /* Initialize callbacks for the modules that need them */
191 static void setCallbacks(void);
193 #ifndef WITH_PYTHON_MODULE
195 static bool use_crash_handler = true;
197 /* set breakpoints here when running in debug mode, useful to catch floating point errors */
198 #if defined(__linux__) || defined(_WIN32) || defined(OSX_SSE_FPE)
199 static void fpe_handler(int UNUSED(sig))
201 // printf("SIGFPE trapped\n");
205 /* handling ctrl-c event in console */
206 #if !(defined(WITH_PYTHON_MODULE) || defined(WITH_HEADLESS))
207 static void blender_esc(int sig)
209 static int count = 0;
211 G.is_break = TRUE; /* forces render loop to read queue, not sure if its needed */
215 printf("\nBlender killed\n");
218 printf("\nSent an internal break event. Press ^C again to kill Blender\n");
224 static int print_version(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
226 printf(BLEND_VERSION_STRING_FMT);
228 printf("\tbuild date: %s\n", build_date);
229 printf("\tbuild time: %s\n", build_time);
230 printf("\tbuild commit date: %s\n", build_commit_date);
231 printf("\tbuild commit time: %s\n", build_commit_time);
232 printf("\tbuild hash: %s\n", build_hash);
233 printf("\tbuild platform: %s\n", build_platform);
234 printf("\tbuild type: %s\n", build_type);
235 printf("\tbuild c flags: %s\n", build_cflags);
236 printf("\tbuild c++ flags: %s\n", build_cxxflags);
237 printf("\tbuild link flags: %s\n", build_linkflags);
238 printf("\tbuild system: %s\n", build_system);
245 static int print_help(int UNUSED(argc), const char **UNUSED(argv), void *data)
247 bArgs *ba = (bArgs *)data;
249 printf(BLEND_VERSION_STRING_FMT);
250 printf("Usage: blender [args ...] [file] [args ...]\n\n");
252 printf("Render Options:\n");
253 BLI_argsPrintArgDoc(ba, "--background");
254 BLI_argsPrintArgDoc(ba, "--render-anim");
255 BLI_argsPrintArgDoc(ba, "--scene");
256 BLI_argsPrintArgDoc(ba, "--render-frame");
257 BLI_argsPrintArgDoc(ba, "--frame-start");
258 BLI_argsPrintArgDoc(ba, "--frame-end");
259 BLI_argsPrintArgDoc(ba, "--frame-jump");
260 BLI_argsPrintArgDoc(ba, "--render-output");
261 BLI_argsPrintArgDoc(ba, "--engine");
262 BLI_argsPrintArgDoc(ba, "--threads");
265 printf("Format Options:\n");
266 BLI_argsPrintArgDoc(ba, "--render-format");
267 BLI_argsPrintArgDoc(ba, "--use-extension");
270 printf("Animation Playback Options:\n");
271 BLI_argsPrintArgDoc(ba, "-a");
274 printf("Window Options:\n");
275 BLI_argsPrintArgDoc(ba, "--window-border");
276 BLI_argsPrintArgDoc(ba, "--window-borderless");
277 BLI_argsPrintArgDoc(ba, "--window-geometry");
278 BLI_argsPrintArgDoc(ba, "--start-console");
279 BLI_argsPrintArgDoc(ba, "--no-native-pixels");
283 printf("Game Engine Specific Options:\n");
284 BLI_argsPrintArgDoc(ba, "-g");
287 printf("Python Options:\n");
288 BLI_argsPrintArgDoc(ba, "--enable-autoexec");
289 BLI_argsPrintArgDoc(ba, "--disable-autoexec");
293 BLI_argsPrintArgDoc(ba, "--python");
294 BLI_argsPrintArgDoc(ba, "--python-text");
295 BLI_argsPrintArgDoc(ba, "--python-console");
296 BLI_argsPrintArgDoc(ba, "--addons");
300 printf("Debug Options:\n");
301 BLI_argsPrintArgDoc(ba, "--debug");
302 BLI_argsPrintArgDoc(ba, "--debug-value");
305 BLI_argsPrintArgDoc(ba, "--debug-events");
307 BLI_argsPrintArgDoc(ba, "--debug-ffmpeg");
309 BLI_argsPrintArgDoc(ba, "--debug-handlers");
311 BLI_argsPrintArgDoc(ba, "--debug-libmv");
313 BLI_argsPrintArgDoc(ba, "--debug-memory");
314 BLI_argsPrintArgDoc(ba, "--debug-jobs");
315 BLI_argsPrintArgDoc(ba, "--debug-python");
317 BLI_argsPrintArgDoc(ba, "--debug-wm");
318 BLI_argsPrintArgDoc(ba, "--debug-all");
321 BLI_argsPrintArgDoc(ba, "--debug-fpe");
322 BLI_argsPrintArgDoc(ba, "--disable-crash-handler");
325 printf("Misc Options:\n");
326 BLI_argsPrintArgDoc(ba, "--factory-startup");
328 BLI_argsPrintArgDoc(ba, "--env-system-config");
329 BLI_argsPrintArgDoc(ba, "--env-system-datafiles");
330 BLI_argsPrintArgDoc(ba, "--env-system-scripts");
331 BLI_argsPrintArgDoc(ba, "--env-system-python");
333 BLI_argsPrintArgDoc(ba, "-nojoystick");
334 BLI_argsPrintArgDoc(ba, "-noglsl");
335 BLI_argsPrintArgDoc(ba, "-noaudio");
336 BLI_argsPrintArgDoc(ba, "-setaudio");
340 BLI_argsPrintArgDoc(ba, "--help");
343 BLI_argsPrintArgDoc(ba, "-R");
344 BLI_argsPrintArgDoc(ba, "-r");
346 BLI_argsPrintArgDoc(ba, "--version");
348 BLI_argsPrintArgDoc(ba, "--");
350 printf("Other Options:\n");
351 BLI_argsPrintOtherDoc(ba);
353 printf("Argument Parsing:\n");
354 printf("\targuments must be separated by white space. eg\n");
355 printf("\t\t\"blender -ba test.blend\"\n");
356 printf("\t...will ignore the 'a'\n");
357 printf("\t\t\"blender -b test.blend -f8\"\n");
358 printf("\t...will ignore 8 because there is no space between the -f and the frame value\n\n");
360 printf("Argument Order:\n");
361 printf("\targuments are executed in the order they are given. eg\n");
362 printf("\t\t\"blender --background test.blend --render-frame 1 --render-output /tmp\"\n");
363 printf("\t...will not render to /tmp because '--render-frame 1' renders before the output path is set\n");
364 printf("\t\t\"blender --background --render-output /tmp test.blend --render-frame 1\"\n");
365 printf("\t...will not render to /tmp because loading the blend file overwrites the render output that was set\n");
366 printf("\t\t\"blender --background test.blend --render-output /tmp --render-frame 1\" works as expected.\n\n");
368 printf("\nEnvironment Variables:\n");
369 printf(" $BLENDER_USER_CONFIG Directory for user configuration files.\n");
370 printf(" $BLENDER_USER_SCRIPTS Directory for user scripts.\n");
371 printf(" $BLENDER_SYSTEM_SCRIPTS Directory for system wide scripts.\n");
372 printf(" Directory for user data files (icons, translations, ..).\n");
373 printf(" $BLENDER_SYSTEM_DATAFILES Directory for system wide data files.\n");
374 printf(" $BLENDER_SYSTEM_PYTHON Directory for system python libraries.\n");
376 printf(" $TEMP Store temporary files here.\n");
378 printf(" $TMP or $TMPDIR Store temporary files here.\n");
381 printf(" $SDL_AUDIODRIVER LibSDL audio driver - alsa, esd, dma.\n");
383 printf(" $PYTHONHOME Path to the python directory, eg. /usr/lib/python.\n\n");
390 static int end_arguments(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
395 static int enable_python(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
397 G.f |= G_SCRIPT_AUTOEXEC;
398 G.f |= G_SCRIPT_OVERRIDE_PREF;
402 static int disable_python(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
404 G.f &= ~G_SCRIPT_AUTOEXEC;
405 G.f |= G_SCRIPT_OVERRIDE_PREF;
409 static int disable_crash_handler(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
411 use_crash_handler = false;
415 static int background_mode(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
421 static int debug_mode(int UNUSED(argc), const char **UNUSED(argv), void *data)
423 G.debug |= G_DEBUG; /* std output printf's */
424 printf(BLEND_VERSION_STRING_FMT);
425 MEM_set_memory_debug();
427 BLI_mempool_set_memory_debug();
430 #ifdef WITH_BUILDINFO
431 printf("Build: %s %s %s %s\n", build_date, build_time, build_platform, build_type);
438 static int debug_mode_generic(int UNUSED(argc), const char **UNUSED(argv), void *data)
440 G.debug |= GET_INT_FROM_POINTER(data);
445 static int debug_mode_libmv(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
447 libmv_startDebugLogging();
453 static int debug_mode_memory(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
455 MEM_set_memory_debug();
459 static int set_debug_value(int argc, const char **argv, void *UNUSED(data))
462 G.debug_value = atoi(argv[1]);
467 printf("\nError: you must specify debug value to set.\n");
472 static int set_fpe(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
474 #if defined(__linux__) || defined(_WIN32) || defined(OSX_SSE_FPE)
475 /* zealous but makes float issues a heck of a lot easier to find!
476 * set breakpoints on fpe_handler */
477 signal(SIGFPE, fpe_handler);
479 # if defined(__linux__) && defined(__GNUC__)
480 feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
481 # endif /* defined(__linux__) && defined(__GNUC__) */
482 # if defined(OSX_SSE_FPE)
483 /* OSX uses SSE for floating point by default, so here
484 * use SSE instructions to throw floating point exceptions */
485 _MM_SET_EXCEPTION_MASK(_MM_MASK_MASK & ~
486 (_MM_MASK_OVERFLOW | _MM_MASK_INVALID | _MM_MASK_DIV_ZERO));
487 # endif /* OSX_SSE_FPE */
488 # if defined(_WIN32) && defined(_MSC_VER)
489 _controlfp_s(NULL, 0, _MCW_EM); /* enables all fp exceptions */
490 _controlfp_s(NULL, _EM_DENORMAL | _EM_UNDERFLOW | _EM_INEXACT, _MCW_EM); /* hide the ones we don't care about */
491 # endif /* _WIN32 && _MSC_VER */
497 #if defined(__linux__) || defined(__APPLE__)
500 static void blender_crash_handler_backtrace(FILE *fp)
508 fputs("\n# backtrace\n", fp);
510 /* include a backtrace for good measure */
511 nptrs = backtrace(buffer, SIZE);
512 strings = backtrace_symbols(buffer, nptrs);
513 for (i = 0; i < nptrs; i++) {
514 fputs(strings[i], fp);
522 #elif defined(_MSC_VER)
524 static void blender_crash_handler_backtrace(FILE *fp)
529 #define MAXSYMBOL 256
532 unsigned short nframes;
533 SYMBOL_INFO *symbolinfo;
536 process = GetCurrentProcess();
538 SymInitialize(process, NULL, TRUE);
540 nframes = CaptureStackBackTrace(0, SIZE, stack, NULL);
541 symbolinfo = MEM_callocN(sizeof(SYMBOL_INFO) + MAXSYMBOL * sizeof( char ), "crash Symbol table");
542 symbolinfo->MaxNameLen = MAXSYMBOL - 1;
543 symbolinfo->SizeOfStruct = sizeof(SYMBOL_INFO);
545 for (i = 0; i < nframes; i++) {
546 SymFromAddr(process, ( DWORD64 )( stack[ i ] ), 0, symbolinfo);
548 fprintf(fp, "%u: %s - 0x%0X\n", nframes - i - 1, symbolinfo->Name, symbolinfo->Address);
551 MEM_freeN(symbolinfo);
555 #else /* non msvc/osx/linux */
557 static void blender_crash_handler_backtrace(FILE *fp)
564 static void blender_crash_handler(int signum)
569 char fname[FILE_MAX];
571 if (!G.main->name[0]) {
572 BLI_make_file_string("/", fname, BLI_temporary_dir(), "crash.blend");
575 BLI_strncpy(fname, G.main->name, sizeof(fname));
576 BLI_replace_extension(fname, sizeof(fname), ".crash.blend");
579 printf("Writing: %s\n", fname);
582 BKE_undo_save_file(fname);
588 wmWindowManager *wm = G.main->wm.first;
590 char fname[FILE_MAX];
592 if (!G.main->name[0]) {
593 BLI_join_dirfile(fname, sizeof(fname), BLI_temporary_dir(), "blender.crash.txt");
596 BLI_join_dirfile(fname, sizeof(fname), BLI_temporary_dir(), BLI_path_basename(G.main->name));
597 BLI_replace_extension(fname, sizeof(fname), ".crash.txt");
600 printf("Writing: %s\n", fname);
604 BLI_snprintf(header, sizeof(header), "# " BLEND_VERSION_FMT ", Unknown revision\n", BLEND_VERSION_ARG);
606 BLI_snprintf(header, sizeof(header), "# " BLEND_VERSION_FMT ", Commit date: %s %s, Hash %s\n",
607 BLEND_VERSION_ARG, build_commit_date, build_commit_time, build_hash);
610 /* open the crash log */
612 fp = BLI_fopen(fname, "wb");
614 fprintf(stderr, "Unable to save '%s': %s\n",
615 fname, errno ? strerror(errno) : "Unknown error opening file");
619 BKE_report_write_file_fp(fp, &wm->reports, header);
622 blender_crash_handler_backtrace(fp);
629 signal(signum, SIG_DFL);
631 kill(getpid(), signum);
633 TerminateProcess(GetCurrentProcess(), signum);
638 static int set_factory_startup(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
640 G.factory_startup = 1;
644 static int set_env(int argc, const char **argv, void *UNUSED(data))
646 /* "--env-system-scripts" --> "BLENDER_SYSTEM_SCRIPTS" */
648 char env[64] = "BLENDER";
649 char *ch_dst = env + 7; /* skip BLENDER */
650 const char *ch_src = argv[0] + 5; /* skip --env */
653 printf("%s requires one argument\n", argv[0]);
657 for (; *ch_src; ch_src++, ch_dst++) {
658 *ch_dst = (*ch_src == '-') ? '_' : (*ch_src) - 32; /* toupper() */
662 BLI_setenv(env, argv[1]);
666 static int playback_mode(int argc, const char **argv, void *UNUSED(data))
668 /* not if -b was given first */
669 if (G.background == 0) {
671 /* Setup FFmpeg with current debug flags. */
675 WM_main_playanim(argc, argv); /* not the same argc and argv as before */
676 exit(0); /* 2.4x didn't do this */
682 static int prefsize(int argc, const char **argv, void *UNUSED(data))
684 int stax, stay, sizx, sizy;
687 fprintf(stderr, "-p requires four arguments\n");
691 stax = atoi(argv[1]);
692 stay = atoi(argv[2]);
693 sizx = atoi(argv[3]);
694 sizy = atoi(argv[4]);
696 WM_init_state_size_set(stax, stay, sizx, sizy);
701 static int native_pixels(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
703 WM_init_native_pixels(false);
707 static int with_borders(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
709 WM_init_state_normal_set();
713 static int without_borders(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
715 WM_init_state_fullscreen_set();
719 extern bool wm_start_with_console; /* wm_init_exit.c */
720 static int start_with_console(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
722 wm_start_with_console = true;
726 static int register_extension(int UNUSED(argc), const char **UNUSED(argv), void *data)
731 RegisterBlendExtension();
733 (void)data; /* unused */
738 static int no_joystick(int UNUSED(argc), const char **UNUSED(argv), void *data)
740 #ifndef WITH_GAMEENGINE
743 SYS_SystemHandle *syshandle = data;
746 * don't initialize joysticks if user doesn't want to use joysticks
747 * failed joystick initialization delays over 5 seconds, before game engine start
749 SYS_WriteCommandLineInt(*syshandle, "nojoystick", 1);
750 if (G.debug & G_DEBUG) printf("disabling nojoystick\n");
756 static int no_glsl(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
758 GPU_extensions_disable();
762 static int no_audio(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
764 sound_force_device(0);
768 static int set_audio(int argc, const char **argv, void *UNUSED(data))
771 fprintf(stderr, "-setaudio require one argument\n");
775 sound_force_device(sound_define_from_str(argv[1]));
779 static int set_output(int argc, const char **argv, void *data)
783 Scene *scene = CTX_data_scene(C);
785 BLI_strncpy(scene->r.pic, argv[1], sizeof(scene->r.pic));
788 printf("\nError: no blend loaded. cannot use '-o / --render-output'.\n");
793 printf("\nError: you must specify a path after '-o / --render-output'.\n");
798 static int set_engine(int argc, const char **argv, void *data)
802 if (!strcmp(argv[1], "help")) {
803 RenderEngineType *type = NULL;
804 printf("Blender Engine Listing:\n");
805 for (type = R_engines.first; type; type = type->next) {
806 printf("\t%s\n", type->idname);
811 Scene *scene = CTX_data_scene(C);
813 RenderData *rd = &scene->r;
815 if (BLI_findstring(&R_engines, argv[1], offsetof(RenderEngineType, idname))) {
816 BLI_strncpy_utf8(rd->engine, argv[1], sizeof(rd->engine));
820 printf("\nError: no blend loaded. order the arguments so '-E / --engine ' is after a blend is loaded.\n");
827 printf("\nEngine not specified, give 'help' for a list of available engines.\n");
832 static int set_image_type(int argc, const char **argv, void *data)
836 const char *imtype = argv[1];
837 Scene *scene = CTX_data_scene(C);
839 const char imtype_new = BKE_imtype_from_arg(imtype);
841 if (imtype_new == R_IMF_IMTYPE_INVALID) {
842 printf("\nError: Format from '-F / --render-format' not known or not compiled in this release.\n");
845 scene->r.im_format.imtype = imtype_new;
849 printf("\nError: no blend loaded. order the arguments so '-F / --render-format' is after the blend is loaded.\n");
854 printf("\nError: you must specify a format after '-F / --render-foramt'.\n");
859 static int set_threads(int argc, const char **argv, void *UNUSED(data))
862 int threads = atoi(argv[1]);
864 if (threads >= 0 && threads <= BLENDER_MAX_THREADS) {
865 BLI_system_num_threads_override_set(threads);
868 printf("Error, threads has to be in range 0-%d\n", BLENDER_MAX_THREADS);
873 printf("\nError: you must specify a number of threads between 0 and %d '-t / --threads'.\n", BLENDER_MAX_THREADS);
878 static int set_verbosity(int argc, const char **argv, void *UNUSED(data))
881 int level = atoi(argv[1]);
884 libmv_setLoggingVerbosity(level);
892 printf("\nError: you must specify a verbosity level.\n");
897 static int set_extension(int argc, const char **argv, void *data)
901 Scene *scene = CTX_data_scene(C);
903 if (argv[1][0] == '0') {
904 scene->r.scemode &= ~R_EXTENSION;
906 else if (argv[1][0] == '1') {
907 scene->r.scemode |= R_EXTENSION;
910 printf("\nError: Use '-x 1 / -x 0' To set the extension option or '--use-extension'\n");
914 printf("\nError: no blend loaded. order the arguments so '-o ' is after '-x '.\n");
919 printf("\nError: you must specify a path after '- '.\n");
924 static int set_ge_parameters(int argc, const char **argv, void *data)
927 #ifdef WITH_GAMEENGINE
928 SYS_SystemHandle syshandle = *(SYS_SystemHandle *)data;
934 * gameengine parameters are automatically put into system
935 * -g [paramname = value]
939 * -g maxvertexarraysize = 512
943 const char *paramname = argv[a];
944 /* check for single value versus assignment */
945 if (a + 1 < argc && (*(argv[a + 1]) == '=')) {
950 #ifdef WITH_GAMEENGINE
951 SYS_WriteCommandLineString(syshandle, paramname, argv[a]);
955 printf("error: argument assignment (%s) without value.\n", paramname);
962 #ifdef WITH_GAMEENGINE
963 SYS_WriteCommandLineInt(syshandle, argv[a], 1);
966 if (!strcmp(argv[a], "nomipmap")) {
967 GPU_set_mipmap(0); //doMipMap = 0;
970 if (!strcmp(argv[a], "linearmipmap")) {
971 GPU_set_linear_mipmap(1); //linearMipMap = 1;
975 } /* if (*(argv[a + 1]) == '=') */
981 static int render_frame(int argc, const char **argv, void *data)
984 Scene *scene = CTX_data_scene(C);
986 Main *bmain = CTX_data_main(C);
989 Render *re = RE_NewRender(scene->id.name);
995 frame = scene->r.sfra + atoi(argv[1] + 1);
998 frame = (scene->r.efra - atoi(argv[1] + 1)) + 1;
1001 frame = atoi(argv[1]);
1005 BKE_reports_init(&reports, RPT_PRINT);
1007 frame = CLAMPIS(frame, MINAFRAME, MAXFRAME);
1009 RE_SetReports(re, &reports);
1010 RE_BlenderAnim(re, bmain, scene, NULL, scene->lay, frame, frame, scene->r.frame_step);
1011 RE_SetReports(re, NULL);
1015 printf("\nError: frame number must follow '-f / --render-frame'.\n");
1020 printf("\nError: no blend loaded. cannot use '-f / --render-frame'.\n");
1025 static int render_animation(int UNUSED(argc), const char **UNUSED(argv), void *data)
1028 Scene *scene = CTX_data_scene(C);
1030 Main *bmain = CTX_data_main(C);
1031 Render *re = RE_NewRender(scene->id.name);
1033 BKE_reports_init(&reports, RPT_PRINT);
1034 RE_SetReports(re, &reports);
1035 RE_BlenderAnim(re, bmain, scene, NULL, scene->lay, scene->r.sfra, scene->r.efra, scene->r.frame_step);
1036 RE_SetReports(re, NULL);
1039 printf("\nError: no blend loaded. cannot use '-a'.\n");
1044 static int set_scene(int argc, const char **argv, void *data)
1048 Scene *scene = BKE_scene_set_name(CTX_data_main(C), argv[1]);
1050 CTX_data_scene_set(C, scene);
1055 printf("\nError: Scene name must follow '-S / --scene'.\n");
1060 static int set_start_frame(int argc, const char **argv, void *data)
1063 Scene *scene = CTX_data_scene(C);
1066 int frame = atoi(argv[1]);
1067 (scene->r.sfra) = CLAMPIS(frame, MINFRAME, MAXFRAME);
1071 printf("\nError: frame number must follow '-s / --frame-start'.\n");
1076 printf("\nError: no blend loaded. cannot use '-s / --frame-start'.\n");
1081 static int set_end_frame(int argc, const char **argv, void *data)
1084 Scene *scene = CTX_data_scene(C);
1087 int frame = atoi(argv[1]);
1088 (scene->r.efra) = CLAMPIS(frame, MINFRAME, MAXFRAME);
1092 printf("\nError: frame number must follow '-e / --frame-end'.\n");
1097 printf("\nError: no blend loaded. cannot use '-e / --frame-end'.\n");
1102 static int set_skip_frame(int argc, const char **argv, void *data)
1105 Scene *scene = CTX_data_scene(C);
1108 int frame = atoi(argv[1]);
1109 (scene->r.frame_step) = CLAMPIS(frame, 1, MAXFRAME);
1113 printf("\nError: number of frames to step must follow '-j / --frame-jump'.\n");
1118 printf("\nError: no blend loaded. cannot use '-j / --frame-jump'.\n");
1123 /* macro for ugly context setup/reset */
1125 #define BPY_CTX_SETUP(_cmd) \
1127 wmWindowManager *wm = CTX_wm_manager(C); \
1128 wmWindow *prevwin = CTX_wm_window(C); \
1129 Scene *prevscene = CTX_data_scene(C); \
1130 if (wm->windows.first) { \
1131 CTX_wm_window_set(C, wm->windows.first); \
1133 CTX_wm_window_set(C, prevwin); \
1136 fprintf(stderr, "Python script \"%s\" " \
1137 "running with missing context data.\n", argv[1]); \
1140 CTX_data_scene_set(C, prevscene); \
1143 #endif /* WITH_PYTHON */
1145 static int run_python_file(int argc, const char **argv, void *data)
1150 /* workaround for scripts not getting a bpy.context.scene, causes internal errors elsewhere */
1152 /* Make the path absolute because its needed for relative linked blends to be found */
1153 char filename[FILE_MAX];
1154 BLI_strncpy(filename, argv[1], sizeof(filename));
1155 BLI_path_cwd(filename);
1157 BPY_CTX_SETUP(BPY_filepath_exec(C, filename, NULL));
1162 printf("\nError: you must specify a filepath after '%s'.\n", argv[0]);
1166 (void)argc; (void)argv; (void)data; /* unused */
1167 printf("This blender was built without python support\n");
1169 #endif /* WITH_PYTHON */
1172 static int run_python_text(int argc, const char **argv, void *data)
1177 /* workaround for scripts not getting a bpy.context.scene, causes internal errors elsewhere */
1179 /* Make the path absolute because its needed for relative linked blends to be found */
1180 struct Text *text = (struct Text *)BKE_libblock_find_name(ID_TXT, argv[1]);
1183 BPY_CTX_SETUP(BPY_text_exec(C, text, NULL, false));
1187 printf("\nError: text block not found %s.\n", argv[1]);
1192 printf("\nError: you must specify a text block after '%s'.\n", argv[0]);
1196 (void)argc; (void)argv; (void)data; /* unused */
1197 printf("This blender was built without python support\n");
1199 #endif /* WITH_PYTHON */
1202 static int run_python_console(int UNUSED(argc), const char **argv, void *data)
1207 BPY_CTX_SETUP(BPY_string_exec(C, "__import__('code').interact()"));
1211 (void)argv; (void)data; /* unused */
1212 printf("This blender was built without python support\n");
1214 #endif /* WITH_PYTHON */
1217 static int set_addons(int argc, const char **argv, void *data)
1219 /* workaround for scripts not getting a bpy.context.scene, causes internal errors elsewhere */
1222 const int slen = strlen(argv[1]) + 128;
1223 char *str = malloc(slen);
1225 BLI_snprintf(str, slen, "[__import__('addon_utils').enable(i, default_set=False) for i in '%s'.split(',')]", argv[1]);
1226 BPY_CTX_SETUP(BPY_string_exec(C, str));
1229 (void)argv; (void)data; /* unused */
1230 #endif /* WITH_PYTHON */
1234 printf("\nError: you must specify a comma separated list after '--addons'.\n");
1239 static int load_file(int UNUSED(argc), const char **argv, void *data)
1243 /* Make the path absolute because its needed for relative linked blends to be found */
1244 char filename[FILE_MAX];
1246 /* note, we could skip these, but so far we always tried to load these files */
1247 if (argv[0][0] == '-') {
1248 fprintf(stderr, "unknown argument, loading as file: %s\n", argv[0]);
1251 BLI_strncpy(filename, argv[0], sizeof(filename));
1252 BLI_path_cwd(filename);
1255 int retval = BKE_read_file(C, filename, NULL);
1257 /* we successfully loaded a blend file, get sure that
1258 * pointcache works */
1259 if (retval != BKE_READ_FILE_FAIL) {
1260 wmWindowManager *wm = CTX_wm_manager(C);
1261 Main *bmain = CTX_data_main(C);
1263 /* special case, 2.4x files */
1264 if (wm == NULL && CTX_data_main(C)->wm.first == NULL) {
1265 extern void wm_add_default(bContext *C);
1267 /* wm_add_default() needs the screen to be set. */
1268 CTX_wm_screen_set(C, CTX_data_main(C)->screen.first);
1272 CTX_wm_manager_set(C, NULL); /* remove wm to force check */
1274 G.relbase_valid = 1;
1275 if (CTX_wm_manager(C) == NULL) CTX_wm_manager_set(C, wm); /* reset wm */
1277 DAG_on_visible_update(bmain, TRUE);
1278 BKE_scene_update_tagged(bmain->eval_ctx, bmain, CTX_data_scene(C));
1281 /* failed to load file, stop processing arguments */
1285 /* WM_file_read() runs normally but since we're in background mode do here */
1287 /* run any texts that were loaded in and flagged as modules */
1288 BPY_python_reset(C);
1291 /* happens for the UI on file reading too (huh? (ton))*/
1292 // XXX BKE_reset_undo();
1293 // BKE_write_undo("original"); /* save current state */
1296 /* we are not running in background mode here, but start blender in UI mode with
1297 * a file - this should do everything a 'load file' does */
1299 BKE_reports_init(&reports, RPT_PRINT);
1300 WM_file_autoexec_init(filename);
1301 WM_file_read(C, filename, &reports);
1302 BKE_reports_clear(&reports);
1310 static void setupArguments(bContext *C, bArgs *ba, SYS_SystemHandle *syshandle)
1312 static char output_doc[] = "<path>"
1313 "\n\tSet the render path and file name."
1314 "\n\tUse // at the start of the path to"
1315 "\n\t\trender relative to the blend file."
1316 "\n\tThe # characters are replaced by the frame number, and used to define zero padding."
1317 "\n\t\tani_##_test.png becomes ani_01_test.png"
1318 "\n\t\ttest-######.png becomes test-000001.png"
1319 "\n\t\tWhen the filename does not contain #, The suffix #### is added to the filename"
1320 "\n\tThe frame number will be added at the end of the filename."
1321 "\n\t\teg: blender -b foobar.blend -o //render_ -F PNG -x 1 -a"
1322 "\n\t\t//render_ becomes //render_####, writing frames as //render_0001.png//";
1324 static char format_doc[] = "<format>"
1325 "\n\tSet the render format, Valid options are..."
1326 "\n\t\tTGA IRIS JPEG MOVIE IRIZ RAWTGA"
1327 "\n\t\tAVIRAW AVIJPEG PNG BMP FRAMESERVER"
1328 "\n\t(formats that can be compiled into blender, not available on all systems)"
1329 "\n\t\tHDR TIFF EXR MULTILAYER MPEG AVICODEC QUICKTIME CINEON DPX DDS";
1331 static char playback_doc[] = "<options> <file(s)>"
1332 "\n\tPlayback <file(s)>, only operates this way when not running in background."
1333 "\n\t\t-p <sx> <sy>\tOpen with lower left corner at <sx>, <sy>"
1334 "\n\t\t-m\t\tRead from disk (Don't buffer)"
1335 "\n\t\t-f <fps> <fps-base>\t\tSpecify FPS to start with"
1336 "\n\t\t-j <frame>\tSet frame step to <frame>"
1337 "\n\t\t-s <frame>\tPlay from <frame>"
1338 "\n\t\t-e <frame>\tPlay until <frame>";
1340 static char game_doc[] = "Game Engine specific options"
1341 "\n\t-g fixedtime\t\tRun on 50 hertz without dropping frames"
1342 "\n\t-g vertexarrays\t\tUse Vertex Arrays for rendering (usually faster)"
1343 "\n\t-g nomipmap\t\tNo Texture Mipmapping"
1344 "\n\t-g linearmipmap\t\tLinear Texture Mipmapping instead of Nearest (default)";
1346 static char debug_doc[] = "\n\tTurn debugging on\n"
1347 "\n\t* Prints every operator call and their arguments"
1348 "\n\t* Disables mouse grab (to interact with a debugger in some cases)"
1349 "\n\t* Keeps python sys.stdin rather than setting it to None";
1351 //BLI_argsAdd(ba, pass, short_arg, long_arg, doc, cb, C);
1353 /* end argument processing after -- */
1354 BLI_argsAdd(ba, -1, "--", NULL, "\n\tEnds option processing, following arguments passed unchanged. Access via python's sys.argv", end_arguments, NULL);
1356 /* first pass: background mode, disable python and commands that exit after usage */
1357 BLI_argsAdd(ba, 1, "-h", "--help", "\n\tPrint this help text and exit", print_help, ba);
1359 BLI_argsAdd(ba, 1, "/?", NULL, "\n\tPrint this help text and exit (windows only)", print_help, ba);
1361 BLI_argsAdd(ba, 1, "-v", "--version", "\n\tPrint Blender version and exit", print_version, NULL);
1363 /* only to give help message */
1364 #ifndef WITH_PYTHON_SECURITY /* default */
1365 # define PY_ENABLE_AUTO ", (default)"
1366 # define PY_DISABLE_AUTO ""
1368 # define PY_ENABLE_AUTO ""
1369 # define PY_DISABLE_AUTO ", (compiled as non-standard default)"
1372 BLI_argsAdd(ba, 1, "-y", "--enable-autoexec", "\n\tEnable automatic python script execution" PY_ENABLE_AUTO, enable_python, NULL);
1373 BLI_argsAdd(ba, 1, "-Y", "--disable-autoexec", "\n\tDisable automatic python script execution (pydrivers & startup scripts)" PY_DISABLE_AUTO, disable_python, NULL);
1375 BLI_argsAdd(ba, 1, NULL, "--disable-crash-handler", "\n\tDisable the crash handler", disable_crash_handler, NULL);
1377 #undef PY_ENABLE_AUTO
1378 #undef PY_DISABLE_AUTO
1380 BLI_argsAdd(ba, 1, "-b", "--background", "<file>\n\tLoad <file> in background (often used for UI-less rendering)", background_mode, NULL);
1382 BLI_argsAdd(ba, 1, "-a", NULL, playback_doc, playback_mode, NULL);
1384 BLI_argsAdd(ba, 1, "-d", "--debug", debug_doc, debug_mode, ba);
1387 BLI_argsAdd(ba, 1, NULL, "--debug-ffmpeg", "\n\tEnable debug messages from FFmpeg library", debug_mode_generic, (void *)G_DEBUG_FFMPEG);
1390 #ifdef WITH_FREESTYLE
1391 BLI_argsAdd(ba, 1, NULL, "--debug-freestyle", "\n\tEnable debug/profiling messages from Freestyle rendering", debug_mode_generic, (void *)G_DEBUG_FREESTYLE);
1394 BLI_argsAdd(ba, 1, NULL, "--debug-python", "\n\tEnable debug messages for python", debug_mode_generic, (void *)G_DEBUG_PYTHON);
1395 BLI_argsAdd(ba, 1, NULL, "--debug-events", "\n\tEnable debug messages for the event system", debug_mode_generic, (void *)G_DEBUG_EVENTS);
1396 BLI_argsAdd(ba, 1, NULL, "--debug-handlers", "\n\tEnable debug messages for event handling", debug_mode_generic, (void *)G_DEBUG_HANDLERS);
1397 BLI_argsAdd(ba, 1, NULL, "--debug-wm", "\n\tEnable debug messages for the window manager", debug_mode_generic, (void *)G_DEBUG_WM);
1398 BLI_argsAdd(ba, 1, NULL, "--debug-all", "\n\tEnable all debug messages (excludes libmv)", debug_mode_generic, (void *)G_DEBUG_ALL);
1400 BLI_argsAdd(ba, 1, NULL, "--debug-fpe", "\n\tEnable floating point exceptions", set_fpe, NULL);
1403 BLI_argsAdd(ba, 1, NULL, "--debug-libmv", "\n\tEnable debug messages from libmv library", debug_mode_libmv, NULL);
1405 BLI_argsAdd(ba, 1, NULL, "--debug-memory", "\n\tEnable fully guarded memory allocation and debugging", debug_mode_memory, NULL);
1407 BLI_argsAdd(ba, 1, NULL, "--debug-value", "<value>\n\tSet debug value of <value> on startup\n", set_debug_value, NULL);
1408 BLI_argsAdd(ba, 1, NULL, "--debug-jobs", "\n\tEnable time profiling for background jobs.", debug_mode_generic, (void *)G_DEBUG_JOBS);
1410 BLI_argsAdd(ba, 1, NULL, "--verbose", "<verbose>\n\tSet logging verbosity level.", set_verbosity, NULL);
1412 BLI_argsAdd(ba, 1, NULL, "--factory-startup", "\n\tSkip reading the "STRINGIFY (BLENDER_STARTUP_FILE)" in the users home directory", set_factory_startup, NULL);
1414 /* TODO, add user env vars? */
1415 BLI_argsAdd(ba, 1, NULL, "--env-system-datafiles", "\n\tSet the "STRINGIFY_ARG (BLENDER_SYSTEM_DATAFILES)" environment variable", set_env, NULL);
1416 BLI_argsAdd(ba, 1, NULL, "--env-system-scripts", "\n\tSet the "STRINGIFY_ARG (BLENDER_SYSTEM_SCRIPTS)" environment variable", set_env, NULL);
1417 BLI_argsAdd(ba, 1, NULL, "--env-system-python", "\n\tSet the "STRINGIFY_ARG (BLENDER_SYSTEM_PYTHON)" environment variable", set_env, NULL);
1419 /* second pass: custom window stuff */
1420 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);
1421 BLI_argsAdd(ba, 2, "-w", "--window-border", "\n\tForce opening with borders (default)", with_borders, NULL);
1422 BLI_argsAdd(ba, 2, "-W", "--window-borderless", "\n\tForce opening without borders", without_borders, NULL);
1423 BLI_argsAdd(ba, 2, "-con", "--start-console", "\n\tStart with the console window open (ignored if -b is set), (Windows only)", start_with_console, NULL);
1424 BLI_argsAdd(ba, 2, "-R", NULL, "\n\tRegister .blend extension, then exit (Windows only)", register_extension, NULL);
1425 BLI_argsAdd(ba, 2, "-r", NULL, "\n\tSilently register .blend extension, then exit (Windows only)", register_extension, ba);
1426 BLI_argsAdd(ba, 2, NULL, "--no-native-pixels", "\n\tDo not use native pixel size, for high resolution displays (MacBook 'Retina')", native_pixels, ba);
1428 /* third pass: disabling things and forcing settings */
1429 BLI_argsAddCase(ba, 3, "-nojoystick", 1, NULL, 0, "\n\tDisable joystick support", no_joystick, syshandle);
1430 BLI_argsAddCase(ba, 3, "-noglsl", 1, NULL, 0, "\n\tDisable GLSL shading", no_glsl, NULL);
1431 BLI_argsAddCase(ba, 3, "-noaudio", 1, NULL, 0, "\n\tForce sound system to None", no_audio, NULL);
1432 BLI_argsAddCase(ba, 3, "-setaudio", 1, NULL, 0, "\n\tForce sound system to a specific device\n\tNULL SDL OPENAL JACK", set_audio, NULL);
1434 /* fourth pass: processing arguments */
1435 BLI_argsAdd(ba, 4, "-g", NULL, game_doc, set_ge_parameters, syshandle);
1436 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);
1437 BLI_argsAdd(ba, 4, "-a", "--render-anim", "\n\tRender frames from start to end (inclusive)", render_animation, C);
1438 BLI_argsAdd(ba, 4, "-S", "--scene", "<name>\n\tSet the active scene <name> for rendering", set_scene, C);
1439 BLI_argsAdd(ba, 4, "-s", "--frame-start", "<frame>\n\tSet start to frame <frame> (use before the -a argument)", set_start_frame, C);
1440 BLI_argsAdd(ba, 4, "-e", "--frame-end", "<frame>\n\tSet end to frame <frame> (use before the -a argument)", set_end_frame, C);
1441 BLI_argsAdd(ba, 4, "-j", "--frame-jump", "<frames>\n\tSet number of frames to step forward after each rendered frame", set_skip_frame, C);
1442 BLI_argsAdd(ba, 4, "-P", "--python", "<filename>\n\tRun the given Python script file", run_python_file, C);
1443 BLI_argsAdd(ba, 4, NULL, "--python-text", "<name>\n\tRun the given Python script text block", run_python_text, C);
1444 BLI_argsAdd(ba, 4, NULL, "--python-console", "\n\tRun blender with an interactive console", run_python_console, C);
1445 BLI_argsAdd(ba, 4, NULL, "--addons", "\n\tComma separated list of addons (no spaces)", set_addons, C);
1447 BLI_argsAdd(ba, 4, "-o", "--render-output", output_doc, set_output, C);
1448 BLI_argsAdd(ba, 4, "-E", "--engine", "<engine>\n\tSpecify the render engine\n\tuse -E help to list available engines", set_engine, C);
1450 BLI_argsAdd(ba, 4, "-F", "--render-format", format_doc, set_image_type, C);
1451 BLI_argsAdd(ba, 4, "-t", "--threads", "<threads>\n\tUse amount of <threads> for rendering and other operations\n\t[1-" STRINGIFY(BLENDER_MAX_THREADS) "], 0 for systems processor count.", set_threads, NULL);
1452 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);
1455 #endif /* WITH_PYTHON_MODULE */
1457 #ifdef WITH_PYTHON_MODULE
1458 /* allow python module to call main */
1459 # define main main_python_enter
1460 static void *evil_C = NULL;
1463 /* environ is not available in mac shared libraries */
1464 # include <crt_externs.h>
1465 char **environ = NULL;
1471 int main(int argc, const char **UNUSED(argv_c)) /* Do not mess with const */
1473 int main(int argc, const char **argv)
1477 SYS_SystemHandle syshandle;
1479 #ifndef WITH_PYTHON_MODULE
1483 #ifdef WIN32 /* Win32 Unicode Args */
1484 /* NOTE: cannot use guardedalloc malloc here, as it's not yet initialized
1485 * (it depends on the args passed in, which is what we're getting here!)
1487 wchar_t **argv_16 = CommandLineToArgvW(GetCommandLineW(), &argc);
1488 char **argv = malloc(argc * sizeof(char *));
1491 for (argci = 0; argci < argc; argci++) {
1492 argv[argci] = alloc_utf_8_from_16(argv_16[argci], 0);
1498 /* NOTE: Special exception for guarded allocator type switch:
1499 * we need to perform switch from lock-free to fully
1500 * guarded allocator before any allocation happened.
1504 for (i = 0; i < argc; i++) {
1505 if (STREQ(argv[i], "--debug") || STREQ(argv[i], "-d") ||
1506 STREQ(argv[i], "--debug-memory"))
1508 printf("Switching to fully guarded memory allocator.\n");
1509 MEM_use_guarded_allocator();
1512 else if (STREQ(argv[i], "--")) {
1520 time_t temp_time = build_commit_timestamp;
1521 struct tm *tm = gmtime(&temp_time);
1523 strftime(build_commit_date, sizeof(build_commit_date), "%Y-%m-%d", tm);
1524 strftime(build_commit_time, sizeof(build_commit_time), "%H:%M", tm);
1527 const char *unknown = "date-unknown";
1528 BLI_strncpy(build_commit_date, unknown, sizeof(build_commit_date));
1529 BLI_strncpy(build_commit_time, unknown, sizeof(build_commit_time));
1536 #ifdef WITH_PYTHON_MODULE
1538 environ = *_NSGetEnviron();
1547 #ifdef WITH_BINRELOC
1552 libmv_initLogging(argv[0]);
1556 #if defined(__APPLE__) && !defined(WITH_PYTHON_MODULE)
1557 /* patch to ignore argument finder gives us (pid?) */
1558 if (argc == 2 && strncmp(argv[1], "-psn_", 5) == 0) {
1559 extern int GHOST_HACK_getFirstFile(char buf[]);
1560 static char firstfilebuf[512];
1564 if (GHOST_HACK_getFirstFile(firstfilebuf)) {
1566 argv[1] = firstfilebuf;
1576 /* initialize path to executable */
1577 BLI_init_program_path(argv[0]);
1579 BLI_threadapi_init();
1581 initglobals(); /* blender.c */
1585 BKE_modifier_init();
1588 BKE_brush_system_init();
1590 BLI_callback_global_init();
1592 #ifdef WITH_GAMEENGINE
1593 syshandle = SYS_GetSystem();
1598 /* first test for background */
1599 #ifndef WITH_PYTHON_MODULE
1600 ba = BLI_argsInit(argc, (const char **)argv); /* skip binary path */
1601 setupArguments(C, ba, &syshandle);
1603 BLI_argsParse(ba, 1, NULL, NULL);
1605 if (use_crash_handler) {
1606 /* after parsing args */
1607 signal(SIGSEGV, blender_crash_handler);
1610 G.factory_startup = true; /* using preferences or user startup makes no sense for py-as-module */
1618 /* after level 1 args, this is so playanim skips RNA init */
1623 /* end second init */
1626 #if defined(WITH_PYTHON_MODULE) || defined(WITH_HEADLESS)
1627 G.background = true; /* python module mode ALWAYS runs in background mode (for now) */
1629 /* for all platforms, even windos has it! */
1631 signal(SIGINT, blender_esc); /* ctrl c out bg render */
1635 /* background render uses this font too */
1636 BKE_vfont_builtin_register(datatoc_bfont_pfb, datatoc_bfont_pfb_size);
1638 /* Initialize ffmpeg if built in, also needed for bg mode if videos are
1639 * rendered via ffmpeg */
1642 init_def_material();
1644 if (G.background == 0) {
1645 #ifndef WITH_PYTHON_MODULE
1646 BLI_argsParse(ba, 2, NULL, NULL);
1647 BLI_argsParse(ba, 3, NULL, NULL);
1649 WM_init(C, argc, (const char **)argv);
1651 /* this is properly initialized with user defs, but this is default */
1652 /* call after loading the startup.blend so we can read U.tempdir */
1653 BLI_init_temporary_dir(U.tempdir);
1656 #ifndef WITH_PYTHON_MODULE
1657 BLI_argsParse(ba, 3, NULL, NULL);
1660 WM_init(C, argc, (const char **)argv);
1662 /* don't use user preferences temp dir */
1663 BLI_init_temporary_dir(NULL);
1667 * NOTE: the U.pythondir string is NULL until WM_init() is executed,
1668 * so we provide the BPY_ function below to append the user defined
1669 * python-dir to Python's sys.path at this point. Simply putting
1670 * WM_init() before #BPY_python_start() crashes Blender at startup.
1673 /* TODO - U.pythondir */
1675 printf("\n* WARNING * - Blender compiled without Python!\nthis is not intended for typical usage\n\n");
1678 CTX_py_init_set(C, 1);
1681 #ifdef WITH_FREESTYLE
1682 /* initialize Freestyle */
1687 /* OK we are ready for it */
1688 #ifndef WITH_PYTHON_MODULE
1689 BLI_argsParse(ba, 4, load_file, C);
1691 if (G.background == 0) {
1693 if (U.uiflag2 & USER_KEEP_SESSION)
1694 WM_recover_last_session(C, NULL);
1699 #ifndef WITH_PYTHON_MODULE
1705 free(argv[--argci]);
1711 #ifdef WITH_PYTHON_MODULE
1712 return 0; /* keep blender in background mode running */
1716 /* actually incorrect, but works for now (ton) */
1720 if (G.fileflags & G_FILE_AUTOPLAY) {
1721 if (G.f & G_SCRIPT_AUTOEXEC) {
1722 if (WM_init_game(C)) {
1727 if (!(G.f & G_SCRIPT_AUTOEXEC_FAIL_QUIET)) {
1728 G.f |= G_SCRIPT_AUTOEXEC_FAIL;
1729 BLI_snprintf(G.autoexec_fail, sizeof(G.autoexec_fail), "Game AutoStart");
1734 if (!G.file_loaded) {
1742 } /* end of int main(argc, argv) */
1744 #ifdef WITH_PYTHON_MODULE
1745 void main_python_exit(void)
1747 WM_exit((bContext *)evil_C);
1752 static void mem_error_cb(const char *errorStr)
1754 fputs(errorStr, stderr);
1758 static void setCallbacks(void)
1760 /* Error output from the alloc routines: */
1761 MEM_set_error_callback(mem_error_cb);