2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #ifndef WITH_PYTHON_MODULE
27 #include "MEM_guardedalloc.h"
32 # include "BLI_winstuff.h"
36 #include "BLI_threads.h"
37 #include "BLI_utildefines.h"
38 #include "BLI_listbase.h"
39 #include "BLI_string.h"
40 #include "BLI_string_utf8.h"
41 #include "BLI_path_util.h"
42 #include "BLI_fileops.h"
43 #include "BLI_mempool.h"
44 #include "BLI_system.h"
46 #include "BLO_readfile.h" /* only for BLO_has_bfile_extension */
48 #include "BKE_blender_version.h"
49 #include "BKE_context.h"
51 #include "BKE_global.h"
52 #include "BKE_image.h"
53 #include "BKE_library.h"
54 #include "BKE_library_override.h"
56 #include "BKE_report.h"
57 #include "BKE_scene.h"
58 #include "BKE_sound.h"
61 #include "IMB_imbuf.h"
65 #include "BPY_extern.h"
68 #include "RE_engine.h"
69 #include "RE_pipeline.h"
71 #include "ED_datafiles.h"
78 # include "libmv-capi.h"
81 #ifdef WITH_CYCLES_LOGGING
85 #include "DEG_depsgraph.h"
86 #include "DEG_depsgraph_build.h"
87 #include "DEG_depsgraph_debug.h"
89 #include "creator_intern.h" /* own include */
92 /* -------------------------------------------------------------------- */
93 /** \name Utility String Parsing
96 static bool parse_int_relative(
97 const char *str, const char *str_end_test, int pos, int neg,
98 int *r_value, const char **r_err_msg)
100 char *str_end = NULL;
107 value = pos + strtol(str + 1, &str_end, 10);
110 value = (neg - strtol(str + 1, &str_end, 10)) + 1;
113 value = strtol(str, &str_end, 10);
118 if (*str_end != '\0' && (str_end != str_end_test)) {
119 static const char *msg = "not a number";
123 else if ((errno == ERANGE) || ((value < INT_MIN || value > INT_MAX))) {
124 static const char *msg = "exceeds range";
129 *r_value = (int)value;
134 static const char *parse_int_range_sep_search(const char *str, const char *str_end_test)
136 const char *str_end_range = NULL;
138 str_end_range = memchr(str, '.', (str_end_test - str) - 1);
139 if (str_end_range && (str_end_range[1] != '.')) {
140 str_end_range = NULL;
144 str_end_range = strstr(str, "..");
145 if (str_end_range && (str_end_range[2] == '\0')) {
146 str_end_range = NULL;
149 return str_end_range;
153 * Parse a number as a range, eg: `1..4`.
155 * The \a str_end_range argument is a result of #parse_int_range_sep_search.
157 static bool parse_int_range_relative(
158 const char *str, const char *str_end_range, const char *str_end_test, int pos, int neg,
159 int r_value_range[2], const char **r_err_msg)
161 if (parse_int_relative(str, str_end_range, pos, neg, &r_value_range[0], r_err_msg) &&
162 parse_int_relative(str_end_range + 2, str_end_test, pos, neg, &r_value_range[1], r_err_msg))
171 static bool parse_int_relative_clamp(
172 const char *str, const char *str_end_test, int pos, int neg, int min, int max,
173 int *r_value, const char **r_err_msg)
175 if (parse_int_relative(str, str_end_test, pos, neg, r_value, r_err_msg)) {
176 CLAMP(*r_value, min, max);
184 static bool parse_int_range_relative_clamp(
185 const char *str, const char *str_end_range, const char *str_end_test, int pos, int neg, int min, int max,
186 int r_value_range[2], const char **r_err_msg)
188 if (parse_int_range_relative(str, str_end_range, str_end_test, pos, neg, r_value_range, r_err_msg)) {
189 CLAMP(r_value_range[0], min, max);
190 CLAMP(r_value_range[1], min, max);
199 * No clamping, fails with any number outside the range.
201 static bool parse_int_strict_range(
202 const char *str, const char *str_end_test, const int min, const int max,
203 int *r_value, const char **r_err_msg)
205 char *str_end = NULL;
209 value = strtol(str, &str_end, 10);
211 if (*str_end != '\0' && (str_end != str_end_test)) {
212 static const char *msg = "not a number";
216 else if ((errno == ERANGE) || ((value < min || value > max))) {
217 static const char *msg = "exceeds range";
222 *r_value = (int)value;
227 static bool parse_int(
228 const char *str, const char *str_end_test,
229 int *r_value, const char **r_err_msg)
231 return parse_int_strict_range(str, str_end_test, INT_MIN, INT_MAX, r_value, r_err_msg);
234 static bool parse_int_clamp(
235 const char *str, const char *str_end_test, int min, int max,
236 int *r_value, const char **r_err_msg)
238 if (parse_int(str, str_end_test, r_value, r_err_msg)) {
239 CLAMP(*r_value, min, max);
249 * Version of #parse_int_relative_clamp
250 * that parses a comma separated list of numbers.
252 static int *parse_int_relative_clamp_n(
253 const char *str, int pos, int neg, int min, int max,
254 int *r_value_len, const char **r_err_msg)
256 const char sep = ',';
258 for (int i = 0; str[i]; i++) {
264 int *values = MEM_mallocN(sizeof(*values) * len, __func__);
267 const char *str_end = strchr(str, sep);
268 if ((*str == sep) || (*str == '\0')) {
269 static const char *msg = "incorrect comma use";
274 else if (parse_int_relative_clamp(str, str_end, pos, neg, min, max, &values[i], r_err_msg)) {
278 goto fail; /* error message already set */
281 if (str_end) { /* next */
284 else { /* finished */
300 * Version of #parse_int_relative_clamp & #parse_int_range_relative_clamp
301 * that parses a comma separated list of numbers.
303 * \note single values are evaluated as a range with matching start/end.
305 static int (*parse_int_range_relative_clamp_n(
306 const char *str, int pos, int neg, int min, int max,
307 int *r_value_len, const char **r_err_msg))[2]
309 const char sep = ',';
311 for (int i = 0; str[i]; i++) {
317 int (*values)[2] = MEM_mallocN(sizeof(*values) * len, __func__);
320 const char *str_end_range;
321 const char *str_end = strchr(str, sep);
322 if ((*str == sep) || (*str == '\0')) {
323 static const char *msg = "incorrect comma use";
327 else if ((str_end_range = parse_int_range_sep_search(str, str_end)) ?
328 parse_int_range_relative_clamp(str, str_end_range, str_end, pos, neg, min, max, values[i], r_err_msg) :
329 parse_int_relative_clamp(str, str_end, pos, neg, min, max, &values[i][0], r_err_msg))
331 if (str_end_range == NULL) {
332 values[i][1] = values[i][0];
337 goto fail; /* error message already set */
340 if (str_end) { /* next */
343 else { /* finished */
359 /* -------------------------------------------------------------------- */
363 /** \name Utilities Python Context Macro (#BPY_CTX_SETUP)
365 struct BlendePyContextStore {
372 static void arg_py_context_backup(
373 bContext *C, struct BlendePyContextStore *c_py,
374 const char *script_id)
376 c_py->wm = CTX_wm_manager(C);
377 c_py->scene = CTX_data_scene(C);
378 c_py->has_win = !BLI_listbase_is_empty(&c_py->wm->windows);
380 c_py->win = CTX_wm_window(C);
381 CTX_wm_window_set(C, c_py->wm->windows.first);
385 fprintf(stderr, "Python script \"%s\" "
386 "running with missing context data.\n", script_id);
390 static void arg_py_context_restore(
391 bContext *C, struct BlendePyContextStore *c_py)
393 /* script may load a file, check old data is valid before using */
395 if ((c_py->win == NULL) ||
396 ((BLI_findindex(&G_MAIN->wm, c_py->wm) != -1) &&
397 (BLI_findindex(&c_py->wm->windows, c_py->win) != -1)))
399 CTX_wm_window_set(C, c_py->win);
403 if ((c_py->scene == NULL) ||
404 BLI_findindex(&G_MAIN->scenes, c_py->scene) != -1)
406 CTX_data_scene_set(C, c_py->scene);
410 /* macro for context setup/reset */
411 #define BPY_CTX_SETUP(_cmd) \
413 struct BlendePyContextStore py_c; \
414 arg_py_context_backup(C, &py_c, argv[1]); \
416 arg_py_context_restore(C, &py_c); \
419 #endif /* WITH_PYTHON */
423 /* -------------------------------------------------------------------- */
424 /** \name Handle Argument Callbacks
426 * \note Doc strings here are used in differently:
428 * - The `--help` message.
429 * - The man page (for Unix systems),
430 * see: `doc/manpage/blender.1.py`
431 * - Parsed and extracted for the manual,
432 * which converts our ad-hoc formatting to reStructuredText.
433 * see: https://docs.blender.org/manual/en/dev/advanced/command_line.html
437 static void print_version_full(void)
439 printf(BLEND_VERSION_STRING_FMT);
441 printf("\tbuild date: %s\n", build_date);
442 printf("\tbuild time: %s\n", build_time);
443 printf("\tbuild commit date: %s\n", build_commit_date);
444 printf("\tbuild commit time: %s\n", build_commit_time);
445 printf("\tbuild hash: %s\n", build_hash);
446 printf("\tbuild platform: %s\n", build_platform);
447 printf("\tbuild type: %s\n", build_type);
448 printf("\tbuild c flags: %s\n", build_cflags);
449 printf("\tbuild c++ flags: %s\n", build_cxxflags);
450 printf("\tbuild link flags: %s\n", build_linkflags);
451 printf("\tbuild system: %s\n", build_system);
455 static void print_version_short(void)
458 /* NOTE: We include built time since sometimes we need to tell broken from
459 * working built of the same hash. */
460 printf(BLEND_VERSION_FMT " (hash %s built %s %s)\n",
461 BLEND_VERSION_ARG, build_hash, build_date, build_time);
463 printf(BLEND_VERSION_STRING_FMT);
467 static const char arg_handle_print_version_doc[] =
468 "\n\tPrint Blender version and exit."
470 static int arg_handle_print_version(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
472 print_version_full();
477 static const char arg_handle_print_help_doc[] =
478 "\n\tPrint this help text and exit."
480 static const char arg_handle_print_help_doc_win32[] =
481 "\n\tPrint this help text and exit (windows only)."
483 static int arg_handle_print_help(int UNUSED(argc), const char **UNUSED(argv), void *data)
485 bArgs *ba = (bArgs *)data;
487 printf(BLEND_VERSION_STRING_FMT);
488 printf("Usage: blender [args ...] [file] [args ...]\n\n");
490 printf("Render Options:\n");
491 BLI_argsPrintArgDoc(ba, "--background");
492 BLI_argsPrintArgDoc(ba, "--render-anim");
493 BLI_argsPrintArgDoc(ba, "--scene");
494 BLI_argsPrintArgDoc(ba, "--render-frame");
495 BLI_argsPrintArgDoc(ba, "--frame-start");
496 BLI_argsPrintArgDoc(ba, "--frame-end");
497 BLI_argsPrintArgDoc(ba, "--frame-jump");
498 BLI_argsPrintArgDoc(ba, "--render-output");
499 BLI_argsPrintArgDoc(ba, "--engine");
500 BLI_argsPrintArgDoc(ba, "--threads");
503 printf("Format Options:\n");
504 BLI_argsPrintArgDoc(ba, "--render-format");
505 BLI_argsPrintArgDoc(ba, "--use-extension");
508 printf("Animation Playback Options:\n");
509 BLI_argsPrintArgDoc(ba, "-a");
512 printf("Window Options:\n");
513 BLI_argsPrintArgDoc(ba, "--window-border");
514 BLI_argsPrintArgDoc(ba, "--window-fullscreen");
515 BLI_argsPrintArgDoc(ba, "--window-geometry");
516 BLI_argsPrintArgDoc(ba, "--window-maximized");
517 BLI_argsPrintArgDoc(ba, "--start-console");
518 BLI_argsPrintArgDoc(ba, "--no-native-pixels");
519 BLI_argsPrintArgDoc(ba, "--no-window-focus");
522 printf("Python Options:\n");
523 BLI_argsPrintArgDoc(ba, "--enable-autoexec");
524 BLI_argsPrintArgDoc(ba, "--disable-autoexec");
528 BLI_argsPrintArgDoc(ba, "--python");
529 BLI_argsPrintArgDoc(ba, "--python-text");
530 BLI_argsPrintArgDoc(ba, "--python-expr");
531 BLI_argsPrintArgDoc(ba, "--python-console");
532 BLI_argsPrintArgDoc(ba, "--python-exit-code");
533 BLI_argsPrintArgDoc(ba, "--addons");
536 printf("Logging Options:\n");
537 BLI_argsPrintArgDoc(ba, "--log");
538 BLI_argsPrintArgDoc(ba, "--log-level");
539 BLI_argsPrintArgDoc(ba, "--log-show-basename");
540 BLI_argsPrintArgDoc(ba, "--log-show-backtrace");
541 BLI_argsPrintArgDoc(ba, "--log-show-timestamp");
542 BLI_argsPrintArgDoc(ba, "--log-file");
545 printf("Debug Options:\n");
546 BLI_argsPrintArgDoc(ba, "--debug");
547 BLI_argsPrintArgDoc(ba, "--debug-value");
550 BLI_argsPrintArgDoc(ba, "--debug-events");
552 BLI_argsPrintArgDoc(ba, "--debug-ffmpeg");
554 BLI_argsPrintArgDoc(ba, "--debug-handlers");
556 BLI_argsPrintArgDoc(ba, "--debug-libmv");
558 #ifdef WITH_CYCLES_LOGGING
559 BLI_argsPrintArgDoc(ba, "--debug-cycles");
561 BLI_argsPrintArgDoc(ba, "--debug-memory");
562 BLI_argsPrintArgDoc(ba, "--debug-jobs");
563 BLI_argsPrintArgDoc(ba, "--debug-python");
564 BLI_argsPrintArgDoc(ba, "--debug-depsgraph");
565 BLI_argsPrintArgDoc(ba, "--debug-depsgraph-eval");
566 BLI_argsPrintArgDoc(ba, "--debug-depsgraph-build");
567 BLI_argsPrintArgDoc(ba, "--debug-depsgraph-tag");
568 BLI_argsPrintArgDoc(ba, "--debug-depsgraph-no-threads");
569 BLI_argsPrintArgDoc(ba, "--debug-depsgraph-time");
570 BLI_argsPrintArgDoc(ba, "--debug-depsgraph-pretty");
571 BLI_argsPrintArgDoc(ba, "--debug-gpu");
572 BLI_argsPrintArgDoc(ba, "--debug-gpumem");
573 BLI_argsPrintArgDoc(ba, "--debug-gpu-shaders");
574 BLI_argsPrintArgDoc(ba, "--debug-gpu-force-workarounds");
575 BLI_argsPrintArgDoc(ba, "--debug-wm");
576 BLI_argsPrintArgDoc(ba, "--debug-all");
577 BLI_argsPrintArgDoc(ba, "--debug-io");
580 BLI_argsPrintArgDoc(ba, "--debug-fpe");
581 BLI_argsPrintArgDoc(ba, "--disable-crash-handler");
582 BLI_argsPrintArgDoc(ba, "--disable-abort-handler");
585 printf("Misc Options:\n");
586 BLI_argsPrintArgDoc(ba, "--app-template");
587 BLI_argsPrintArgDoc(ba, "--factory-startup");
588 BLI_argsPrintArgDoc(ba, "--enable-static-override");
589 BLI_argsPrintArgDoc(ba, "--enable-event-simulate");
591 BLI_argsPrintArgDoc(ba, "--env-system-datafiles");
592 BLI_argsPrintArgDoc(ba, "--env-system-scripts");
593 BLI_argsPrintArgDoc(ba, "--env-system-python");
595 BLI_argsPrintArgDoc(ba, "-noaudio");
596 BLI_argsPrintArgDoc(ba, "-setaudio");
600 BLI_argsPrintArgDoc(ba, "--help");
602 /* WIN32 only (ignored for non-win32) */
603 BLI_argsPrintArgDoc(ba, "-R");
604 BLI_argsPrintArgDoc(ba, "-r");
606 BLI_argsPrintArgDoc(ba, "--version");
608 BLI_argsPrintArgDoc(ba, "--");
611 //printf("Experimental Features:\n");
613 /* Other options _must_ be last (anything not handled will show here) */
615 printf("Other Options:\n");
616 BLI_argsPrintOtherDoc(ba);
619 printf("Argument Parsing:\n");
620 printf("\tArguments must be separated by white space, eg:\n");
621 printf("\t# blender -ba test.blend\n");
622 printf("\t...will ignore the 'a'.\n");
623 printf("\t# blender -b test.blend -f8\n");
624 printf("\t...will ignore '8' because there is no space between the '-f' and the frame value.\n\n");
626 printf("Argument Order:\n");
627 printf("\tArguments are executed in the order they are given. eg:\n");
628 printf("\t# blender --background test.blend --render-frame 1 --render-output '/tmp'\n");
629 printf("\t...will not render to '/tmp' because '--render-frame 1' renders before the output path is set.\n");
630 printf("\t# blender --background --render-output /tmp test.blend --render-frame 1\n");
631 printf("\t...will not render to '/tmp' because loading the blend-file overwrites the render output that was set.\n");
632 printf("\t# blender --background test.blend --render-output /tmp --render-frame 1\n");
633 printf("\t...works as expected.\n\n");
635 printf("Environment Variables:\n");
636 printf(" $BLENDER_USER_CONFIG Directory for user configuration files.\n");
637 printf(" $BLENDER_USER_SCRIPTS Directory for user scripts.\n");
638 printf(" $BLENDER_SYSTEM_SCRIPTS Directory for system wide scripts.\n");
639 printf(" $BLENDER_USER_DATAFILES Directory for user data files (icons, translations, ..).\n");
640 printf(" $BLENDER_SYSTEM_DATAFILES Directory for system wide data files.\n");
641 printf(" $BLENDER_SYSTEM_PYTHON Directory for system Python libraries.\n");
643 printf(" $TEMP Store temporary files here.\n");
645 printf(" $TMP or $TMPDIR Store temporary files here.\n");
648 printf(" $SDL_AUDIODRIVER LibSDL audio driver - alsa, esd, dma.\n");
650 printf(" $PYTHONHOME Path to the Python directory, eg. /usr/lib/python.\n\n");
657 static const char arg_handle_arguments_end_doc[] =
658 "\n\tEnd option processing, following arguments passed unchanged. Access via Python's 'sys.argv'."
660 static int arg_handle_arguments_end(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
665 /* only to give help message */
666 #ifndef WITH_PYTHON_SECURITY /* default */
667 # define PY_ENABLE_AUTO ", (default)"
668 # define PY_DISABLE_AUTO ""
670 # define PY_ENABLE_AUTO ""
671 # define PY_DISABLE_AUTO ", (compiled as non-standard default)"
674 static const char arg_handle_python_set_doc_enable[] =
675 "\n\tEnable automatic Python script execution" PY_ENABLE_AUTO "."
677 static const char arg_handle_python_set_doc_disable[] =
678 "\n\tDisable automatic Python script execution (pydrivers & startup scripts)" PY_DISABLE_AUTO "."
680 #undef PY_ENABLE_AUTO
681 #undef PY_DISABLE_AUTO
683 static int arg_handle_python_set(int UNUSED(argc), const char **UNUSED(argv), void *data)
686 G.f |= G_FLAG_SCRIPT_AUTOEXEC;
689 G.f &= ~G_FLAG_SCRIPT_AUTOEXEC;
691 G.f |= G_FLAG_SCRIPT_OVERRIDE_PREF;
695 static const char arg_handle_crash_handler_disable_doc[] =
696 "\n\tDisable the crash handler."
698 static int arg_handle_crash_handler_disable(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
700 app_state.signal.use_crash_handler = false;
704 static const char arg_handle_abort_handler_disable_doc[] =
705 "\n\tDisable the abort handler."
707 static int arg_handle_abort_handler_disable(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
709 app_state.signal.use_abort_handler = false;
713 static const char arg_handle_background_mode_set_doc[] =
714 "\n\tRun in background (often used for UI-less rendering)."
716 static int arg_handle_background_mode_set(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
718 print_version_short();
723 static const char arg_handle_log_level_set_doc[] =
726 "\tSet the logging verbosity level (higher for more details) defaults to 1, use -1 to log all levels."
728 static int arg_handle_log_level_set(int argc, const char **argv, void *UNUSED(data))
730 const char *arg_id = "--log-level";
732 const char *err_msg = NULL;
733 if (!parse_int_clamp(argv[1], NULL, -1, INT_MAX, &G.log.level, &err_msg)) {
734 printf("\nError: %s '%s %s'.\n", err_msg, arg_id, argv[1]);
737 if (G.log.level == -1) {
738 G.log.level = INT_MAX;
740 CLG_level_set(G.log.level);
745 printf("\nError: '%s' no args given.\n", arg_id);
750 static const char arg_handle_log_show_basename_set_doc[] =
751 "\n\tOnly show file name in output (not the leading path)."
753 static int arg_handle_log_show_basename_set(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
755 CLG_output_use_basename_set(true);
759 static const char arg_handle_log_show_backtrace_set_doc[] =
760 "\n\tShow a back trace for each log message (debug builds only)."
762 static int arg_handle_log_show_backtrace_set(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
764 /* Ensure types don't become incompatible. */
765 void (*fn)(FILE *fp) = BLI_system_backtrace;
766 CLG_backtrace_fn_set((void (*)(void *))fn);
770 static const char arg_handle_log_show_timestamp_set_doc[] =
771 "\n\tShow a timestamp for each log message in seconds since start."
773 static int arg_handle_log_show_timestamp_set(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
775 CLG_output_use_timestamp_set(true);
779 static const char arg_handle_log_file_set_doc[] =
782 "\tSet a file to output the log to."
784 static int arg_handle_log_file_set(int argc, const char **argv, void *UNUSED(data))
786 const char *arg_id = "--log-file";
789 FILE *fp = BLI_fopen(argv[1], "w");
791 const char *err_msg = errno ? strerror(errno) : "unknown";
792 printf("\nError: %s '%s %s'.\n", err_msg, arg_id, argv[1]);
795 if (UNLIKELY(G.log.file != NULL)) {
799 CLG_output_set(G.log.file);
804 printf("\nError: '%s' no args given.\n", arg_id);
809 static const char arg_handle_log_set_doc[] =
811 "\tEnable logging categories, taking a single comma separated argument.\n"
812 "\tMultiple categories can be matched using a '.*' suffix,\n"
813 "\tso '--log \"wm.*\"' logs every kind of window-manager message.\n"
814 "\tUse \"^\" prefix to ignore, so '--log \"*,^wm.operator.*\"' logs all except for 'wm.operators.*'\n"
815 "\tUse \"*\" to log everything."
817 static int arg_handle_log_set(int argc, const char **argv, void *UNUSED(data))
819 const char *arg_id = "--log";
821 const char *str_step = argv[1];
823 const char *str_step_end = strchr(str_step, ',');
824 int str_step_len = str_step_end ? (str_step_end - str_step) : strlen(str_step);
826 if (str_step[0] == '^') {
827 CLG_type_filter_exclude(str_step + 1, str_step_len - 1);
830 CLG_type_filter_include(str_step, str_step_len);
834 /* typically only be one, but don't fail on multiple.*/
835 while (*str_step_end == ',') {
838 str_step = str_step_end;
847 printf("\nError: '%s' no args given.\n", arg_id);
852 static const char arg_handle_debug_mode_set_doc[] =
854 "\tTurn debugging on.\n"
856 "\t* Enables memory error detection\n"
857 "\t* Disables mouse grab (to interact with a debugger in some cases)\n"
858 "\t* Keeps Python's 'sys.stdin' rather than setting it to None"
860 static int arg_handle_debug_mode_set(int UNUSED(argc), const char **UNUSED(argv), void *data)
862 G.debug |= G_DEBUG; /* std output printf's */
863 printf(BLEND_VERSION_STRING_FMT);
864 MEM_set_memory_debug();
866 BLI_mempool_set_memory_debug();
869 #ifdef WITH_BUILDINFO
870 printf("Build: %s %s %s %s\n", build_date, build_time, build_platform, build_type);
878 static const char arg_handle_debug_mode_generic_set_doc_ffmpeg[] =
879 "\n\tEnable debug messages from FFmpeg library.";
881 #ifdef WITH_FREESTYLE
882 static const char arg_handle_debug_mode_generic_set_doc_freestyle[] =
883 "\n\tEnable debug messages for FreeStyle.";
885 static const char arg_handle_debug_mode_generic_set_doc_python[] =
886 "\n\tEnable debug messages for Python.";
887 static const char arg_handle_debug_mode_generic_set_doc_events[] =
888 "\n\tEnable debug messages for the event system.";
889 static const char arg_handle_debug_mode_generic_set_doc_handlers[] =
890 "\n\tEnable debug messages for event handling.";
891 static const char arg_handle_debug_mode_generic_set_doc_wm[] =
892 "\n\tEnable debug messages for the window manager, shows all operators in search, shows keymap errors.";
893 static const char arg_handle_debug_mode_generic_set_doc_jobs[] =
894 "\n\tEnable time profiling for background jobs.";
895 static const char arg_handle_debug_mode_generic_set_doc_gpu[] =
896 "\n\tEnable gpu debug context and information for OpenGL 4.3+.";
897 static const char arg_handle_debug_mode_generic_set_doc_depsgraph[] =
898 "\n\tEnable all debug messages from dependency graph.";
899 static const char arg_handle_debug_mode_generic_set_doc_depsgraph_build[] =
900 "\n\tEnable debug messages from dependency graph related on graph construction.";
901 static const char arg_handle_debug_mode_generic_set_doc_depsgraph_tag[] =
902 "\n\tEnable debug messages from dependency graph related on tagging.";
903 static const char arg_handle_debug_mode_generic_set_doc_depsgraph_time[] =
904 "\n\tEnable debug messages from dependency graph related on timing.";
905 static const char arg_handle_debug_mode_generic_set_doc_depsgraph_eval[] =
906 "\n\tEnable debug messages from dependency graph related on evaluation.";
907 static const char arg_handle_debug_mode_generic_set_doc_depsgraph_no_threads[] =
908 "\n\tSwitch dependency graph to a single threaded evaluation.";
909 static const char arg_handle_debug_mode_generic_set_doc_depsgraph_pretty[] =
910 "\n\tEnable colors for dependency graph debug messages.";
911 static const char arg_handle_debug_mode_generic_set_doc_gpumem[] =
912 "\n\tEnable GPU memory stats in status bar.";
914 static int arg_handle_debug_mode_generic_set(int UNUSED(argc), const char **UNUSED(argv), void *data)
916 G.debug |= POINTER_AS_INT(data);
920 static const char arg_handle_debug_mode_io_doc[] =
921 "\n\tEnable debug messages for I/O (collada, ...).";
922 static int arg_handle_debug_mode_io(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
924 G.debug |= G_DEBUG_IO;
928 static const char arg_handle_debug_mode_all_doc[] =
929 "\n\tEnable all debug messages.";
930 static int arg_handle_debug_mode_all(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
932 G.debug |= G_DEBUG_ALL;
934 libmv_startDebugLogging();
936 #ifdef WITH_CYCLES_LOGGING
937 CCL_start_debug_logging();
943 static const char arg_handle_debug_mode_libmv_doc[] =
944 "\n\tEnable debug messages from libmv library."
946 static int arg_handle_debug_mode_libmv(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
948 libmv_startDebugLogging();
954 #ifdef WITH_CYCLES_LOGGING
955 static const char arg_handle_debug_mode_cycles_doc[] =
956 "\n\tEnable debug messages from Cycles."
958 static int arg_handle_debug_mode_cycles(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
960 CCL_start_debug_logging();
965 static const char arg_handle_debug_mode_memory_set_doc[] =
966 "\n\tEnable fully guarded memory allocation and debugging."
968 static int arg_handle_debug_mode_memory_set(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
970 MEM_set_memory_debug();
974 static const char arg_handle_debug_value_set_doc[] =
976 "\tSet debug value of <value> on startup."
978 static int arg_handle_debug_value_set(int argc, const char **argv, void *UNUSED(data))
980 const char *arg_id = "--debug-value";
982 const char *err_msg = NULL;
984 if (!parse_int(argv[1], NULL, &value, &err_msg)) {
985 printf("\nError: %s '%s %s'.\n", err_msg, arg_id, argv[1]);
989 G.debug_value = value;
994 printf("\nError: you must specify debug value to set.\n");
999 static const char arg_handle_debug_fpe_set_doc[] =
1000 "\n\tEnable floating point exceptions."
1002 static int arg_handle_debug_fpe_set(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
1004 main_signal_setup_fpe();
1008 static const char arg_handle_app_template_doc[] =
1009 "\n\tSet the application template, use 'default' for none."
1011 static int arg_handle_app_template(int argc, const char **argv, void *UNUSED(data))
1014 const char *app_template = STREQ(argv[1], "default") ? "" : argv[1];
1015 WM_init_state_app_template_set(app_template);
1019 printf("\nError: App template must follow '--app-template'.\n");
1024 static const char arg_handle_factory_startup_set_doc[] =
1025 "\n\tSkip reading the " STRINGIFY(BLENDER_STARTUP_FILE) " in the users home directory."
1027 static int arg_handle_factory_startup_set(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
1029 G.factory_startup = 1;
1033 static const char arg_handle_enable_static_override_doc[] =
1034 "\n\tEnable Static Override features in the UI."
1036 static int arg_handle_enable_static_override(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
1038 BKE_override_static_enable(true);
1042 static const char arg_handle_enable_event_simulate_doc[] =
1043 "\n\tEnable event simulation testing feature 'bpy.types.Window.event_simulate'."
1045 static int arg_handle_enable_event_simulate(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
1047 G.f |= G_FLAG_EVENT_SIMULATE;
1051 static const char arg_handle_env_system_set_doc_datafiles[] =
1052 "\n\tSet the "STRINGIFY_ARG (BLENDER_SYSTEM_DATAFILES)" environment variable.";
1053 static const char arg_handle_env_system_set_doc_scripts[] =
1054 "\n\tSet the "STRINGIFY_ARG (BLENDER_SYSTEM_SCRIPTS)" environment variable.";
1055 static const char arg_handle_env_system_set_doc_python[] =
1056 "\n\tSet the "STRINGIFY_ARG (BLENDER_SYSTEM_PYTHON)" environment variable.";
1058 static int arg_handle_env_system_set(int argc, const char **argv, void *UNUSED(data))
1060 /* "--env-system-scripts" --> "BLENDER_SYSTEM_SCRIPTS" */
1062 char env[64] = "BLENDER";
1063 char *ch_dst = env + 7; /* skip BLENDER */
1064 const char *ch_src = argv[0] + 5; /* skip --env */
1067 printf("%s requires one argument\n", argv[0]);
1071 for (; *ch_src; ch_src++, ch_dst++) {
1072 *ch_dst = (*ch_src == '-') ? '_' : (*ch_src) - 32; /* toupper() */
1076 BLI_setenv(env, argv[1]);
1080 static const char arg_handle_playback_mode_doc[] =
1081 "<options> <file(s)>\n"
1082 "\tPlayback <file(s)>, only operates this way when not running in background.\n\n"
1084 "\t\tOpen with lower left corner at <sx>, <sy>.\n"
1086 "\t\tRead from disk (Do not buffer).\n"
1087 "\t-f <fps> <fps-base>\n"
1088 "\t\tSpecify FPS to start with.\n"
1090 "\t\tSet frame step to <frame>.\n"
1092 "\t\tPlay from <frame>.\n"
1094 "\t\tPlay until <frame>."
1096 static int arg_handle_playback_mode(int argc, const char **argv, void *UNUSED(data))
1098 /* not if -b was given first */
1099 if (G.background == 0) {
1101 /* Setup FFmpeg with current debug flags. */
1105 WM_main_playanim(argc, argv); /* not the same argc and argv as before */
1106 exit(0); /* 2.4x didn't do this */
1112 static const char arg_handle_window_geometry_doc[] =
1113 "<sx> <sy> <w> <h>\n"
1114 "\tOpen with lower left corner at <sx>, <sy> and width and height as <w>, <h>."
1116 static int arg_handle_window_geometry(int argc, const char **argv, void *UNUSED(data))
1118 const char *arg_id = "-p / --window-geometry";
1122 fprintf(stderr, "Error: requires four arguments '%s'\n", arg_id);
1126 for (i = 0; i < 4; i++) {
1127 const char *err_msg = NULL;
1128 if (!parse_int(argv[i + 1], NULL, ¶ms[i], &err_msg)) {
1129 printf("\nError: %s '%s %s'.\n", err_msg, arg_id, argv[1]);
1134 WM_init_state_size_set(UNPACK4(params));
1139 static const char arg_handle_native_pixels_set_doc[] =
1140 "\n\tDo not use native pixel size, for high resolution displays (MacBook 'Retina')."
1142 static int arg_handle_native_pixels_set(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
1144 WM_init_native_pixels(false);
1148 static const char arg_handle_with_borders_doc[] =
1149 "\n\tForce opening with borders."
1151 static int arg_handle_with_borders(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
1153 WM_init_state_normal_set();
1157 static const char arg_handle_without_borders_doc[] =
1158 "\n\tForce opening in fullscreen mode."
1160 static int arg_handle_without_borders(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
1162 WM_init_state_fullscreen_set();
1166 static const char arg_handle_window_maximized_doc[] =
1167 "\n\tForce opening maximized."
1169 static int arg_handle_window_maximized(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
1171 WM_init_state_maximized_set();
1175 static const char arg_handle_no_window_focus_doc[] =
1176 "\n\tOpen behind other windows and without taking focus."
1178 static int arg_handle_no_window_focus(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
1180 WM_init_window_focus_set(false);
1184 static const char arg_handle_start_with_console_doc[] =
1185 "\n\tStart with the console window open (ignored if -b is set), (Windows only)."
1187 static int arg_handle_start_with_console(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
1189 WM_init_state_start_with_console_set(true);
1193 static const char arg_handle_register_extension_doc[] =
1194 "\n\tRegister blend-file extension, then exit (Windows only)."
1196 static const char arg_handle_register_extension_doc_silent[] =
1197 "\n\tSilently register blend-file extension, then exit (Windows only)."
1199 static int arg_handle_register_extension(int UNUSED(argc), const char **UNUSED(argv), void *data)
1204 RegisterBlendExtension();
1206 (void)data; /* unused */
1211 static const char arg_handle_audio_disable_doc[] =
1212 "\n\tForce sound system to None."
1214 static int arg_handle_audio_disable(int UNUSED(argc), const char **UNUSED(argv), void *UNUSED(data))
1216 BKE_sound_force_device("Null");
1220 static const char arg_handle_audio_set_doc[] =
1221 "\n\tForce sound system to a specific device.\n\t'NULL' 'SDL' 'OPENAL' 'JACK'."
1223 static int arg_handle_audio_set(int argc, const char **argv, void *UNUSED(data))
1226 fprintf(stderr, "-setaudio require one argument\n");
1230 BKE_sound_force_device(argv[1]);
1234 static const char arg_handle_output_set_doc[] =
1236 "\tSet the render path and file name.\n"
1237 "\tUse '//' at the start of the path to render relative to the blend-file.\n"
1239 "\tThe '#' characters are replaced by the frame number, and used to define zero padding.\n"
1241 "\t* 'ani_##_test.png' becomes 'ani_01_test.png'\n"
1242 "\t* 'test-######.png' becomes 'test-000001.png'\n"
1244 "\tWhen the filename does not contain '#', The suffix '####' is added to the filename.\n"
1246 "\tThe frame number will be added at the end of the filename, eg:\n"
1247 "\t# blender -b foobar.blend -o //render_ -F PNG -x 1 -a\n"
1248 "\t'//render_' becomes '//render_####', writing frames as '//render_0001.png'"
1250 static int arg_handle_output_set(int argc, const char **argv, void *data)
1254 Scene *scene = CTX_data_scene(C);
1256 BLI_strncpy(scene->r.pic, argv[1], sizeof(scene->r.pic));
1259 printf("\nError: no blend loaded. cannot use '-o / --render-output'.\n");
1264 printf("\nError: you must specify a path after '-o / --render-output'.\n");
1269 static const char arg_handle_engine_set_doc[] =
1271 "\tSpecify the render engine.\n\tUse -E help to list available engines."
1273 static int arg_handle_engine_set(int argc, const char **argv, void *data)
1277 if (STREQ(argv[1], "help")) {
1278 RenderEngineType *type = NULL;
1279 printf("Blender Engine Listing:\n");
1280 for (type = R_engines.first; type; type = type->next) {
1281 printf("\t%s\n", type->idname);
1286 Scene *scene = CTX_data_scene(C);
1288 if (BLI_findstring(&R_engines, argv[1], offsetof(RenderEngineType, idname))) {
1289 BLI_strncpy_utf8(scene->r.engine, argv[1], sizeof(scene->r.engine));
1292 printf("\nError: engine not found '%s'\n", argv[1]);
1297 printf("\nError: no blend loaded. "
1298 "order the arguments so '-E / --engine ' is after a blend is loaded.\n");
1305 printf("\nEngine not specified, give 'help' for a list of available engines.\n");
1310 static const char arg_handle_image_type_set_doc[] =
1312 "\tSet the render format.\n"
1313 "\tValid options are 'TGA' 'RAWTGA' 'JPEG' 'IRIS' 'IRIZ' 'AVIRAW' 'AVIJPEG' 'PNG' 'BMP'\n"
1315 "\tFormats that can be compiled into Blender, not available on all systems: 'HDR' 'TIFF' 'OPEN_EXR'\n"
1316 "\t'OPEN_EXR_MULTILAYER' 'MPEG' 'CINEON' 'DPX' 'DDS' 'JP2'"
1318 static int arg_handle_image_type_set(int argc, const char **argv, void *data)
1322 const char *imtype = argv[1];
1323 Scene *scene = CTX_data_scene(C);
1325 const char imtype_new = BKE_imtype_from_arg(imtype);
1327 if (imtype_new == R_IMF_IMTYPE_INVALID) {
1328 printf("\nError: Format from '-F / --render-format' not known or not compiled in this release.\n");
1331 scene->r.im_format.imtype = imtype_new;
1335 printf("\nError: no blend loaded. "
1336 "order the arguments so '-F / --render-format' is after the blend is loaded.\n");
1341 printf("\nError: you must specify a format after '-F / --render-foramt'.\n");
1346 static const char arg_handle_threads_set_doc[] =
1348 "\tUse amount of <threads> for rendering and other operations\n"
1349 "\t[1-" STRINGIFY(BLENDER_MAX_THREADS) "], 0 for systems processor count."
1351 static int arg_handle_threads_set(int argc, const char **argv, void *UNUSED(data))
1353 const char *arg_id = "-t / --threads";
1354 const int min = 0, max = BLENDER_MAX_THREADS;
1356 const char *err_msg = NULL;
1358 if (!parse_int_strict_range(argv[1], NULL, min, max, &threads, &err_msg)) {
1359 printf("\nError: %s '%s %s', expected number in [%d..%d].\n", err_msg, arg_id, argv[1], min, max);
1363 BLI_system_num_threads_override_set(threads);
1367 printf("\nError: you must specify a number of threads in [%d..%d] '%s'.\n", min, max, arg_id);
1372 static const char arg_handle_verbosity_set_doc[] =
1374 "\tSet logging verbosity level."
1376 static int arg_handle_verbosity_set(int argc, const char **argv, void *UNUSED(data))
1378 const char *arg_id = "--verbose";
1380 const char *err_msg = NULL;
1382 if (!parse_int(argv[1], NULL, &level, &err_msg)) {
1383 printf("\nError: %s '%s %s'.\n", err_msg, arg_id, argv[1]);
1387 libmv_setLoggingVerbosity(level);
1388 #elif defined(WITH_CYCLES_LOGGING)
1389 CCL_logging_verbosity_set(level);
1397 printf("\nError: you must specify a verbosity level.\n");
1402 static const char arg_handle_extension_set_doc[] =
1404 "\tSet option to add the file extension to the end of the file."
1406 static int arg_handle_extension_set(int argc, const char **argv, void *data)
1410 Scene *scene = CTX_data_scene(C);
1412 if (argv[1][0] == '0') {
1413 scene->r.scemode &= ~R_EXTENSION;
1415 else if (argv[1][0] == '1') {
1416 scene->r.scemode |= R_EXTENSION;
1419 printf("\nError: Use '-x 1 / -x 0' To set the extension option or '--use-extension'\n");
1423 printf("\nError: no blend loaded. "
1424 "order the arguments so '-o ' is after '-x '.\n");
1429 printf("\nError: you must specify a path after '- '.\n");
1434 static const char arg_handle_render_frame_doc[] =
1436 "\tRender frame <frame> and save it.\n"
1438 "\t* +<frame> start frame relative, -<frame> end frame relative.\n"
1439 "\t* A comma separated list of frames can also be used (no spaces).\n"
1440 "\t* A range of frames can be expressed using '..' separator between the first and last frames (inclusive).\n"
1442 static int arg_handle_render_frame(int argc, const char **argv, void *data)
1444 const char *arg_id = "-f / --render-frame";
1446 Scene *scene = CTX_data_scene(C);
1448 Main *bmain = CTX_data_main(C);
1451 const char *err_msg = NULL;
1455 int (*frame_range_arr)[2], frames_range_len;
1456 if ((frame_range_arr = parse_int_range_relative_clamp_n(
1457 argv[1], scene->r.sfra, scene->r.efra, MINAFRAME, MAXFRAME,
1458 &frames_range_len, &err_msg)) == NULL)
1460 printf("\nError: %s '%s %s'.\n", err_msg, arg_id, argv[1]);
1464 re = RE_NewSceneRender(scene);
1465 BLI_threaded_malloc_begin();
1466 BKE_reports_init(&reports, RPT_STORE);
1467 RE_SetReports(re, &reports);
1468 for (int i = 0; i < frames_range_len; i++) {
1469 /* We could pass in frame ranges,
1470 * but prefer having exact behavior as passing in multiple frames */
1471 if ((frame_range_arr[i][0] <= frame_range_arr[i][1]) == 0) {
1472 printf("\nWarning: negative range ignored '%s %s'.\n", arg_id, argv[1]);
1475 for (int frame = frame_range_arr[i][0]; frame <= frame_range_arr[i][1]; frame++) {
1476 RE_BlenderAnim(re, bmain, scene, NULL, NULL, frame, frame, scene->r.frame_step);
1479 RE_SetReports(re, NULL);
1480 BKE_reports_clear(&reports);
1481 BLI_threaded_malloc_end();
1482 MEM_freeN(frame_range_arr);
1486 printf("\nError: frame number must follow '%s'.\n", arg_id);
1491 printf("\nError: no blend loaded. cannot use '%s'.\n", arg_id);
1496 static const char arg_handle_render_animation_doc[] =
1497 "\n\tRender frames from start to end (inclusive)."
1499 static int arg_handle_render_animation(int UNUSED(argc), const char **UNUSED(argv), void *data)
1502 Scene *scene = CTX_data_scene(C);
1504 Main *bmain = CTX_data_main(C);
1505 Render *re = RE_NewSceneRender(scene);
1507 BLI_threaded_malloc_begin();
1508 BKE_reports_init(&reports, RPT_STORE);
1509 RE_SetReports(re, &reports);
1510 RE_BlenderAnim(re, bmain, scene, NULL, NULL, scene->r.sfra, scene->r.efra, scene->r.frame_step);
1511 RE_SetReports(re, NULL);
1512 BKE_reports_clear(&reports);
1513 BLI_threaded_malloc_end();
1516 printf("\nError: no blend loaded. cannot use '-a'.\n");
1521 static const char arg_handle_scene_set_doc[] =
1523 "\tSet the active scene <name> for rendering."
1525 static int arg_handle_scene_set(int argc, const char **argv, void *data)
1529 Scene *scene = BKE_scene_set_name(CTX_data_main(C), argv[1]);
1531 CTX_data_scene_set(C, scene);
1533 /* Set the scene of the first window, see: T55991,
1534 * otherwise scrips that run later won't get this scene back from the context. */
1535 wmWindow *win = CTX_wm_window(C);
1537 win = CTX_wm_manager(C)->windows.first;
1540 WM_window_set_active_scene(CTX_data_main(C), C, win, scene);
1546 printf("\nError: Scene name must follow '-S / --scene'.\n");
1551 static const char arg_handle_frame_start_set_doc[] =
1553 "\tSet start to frame <frame>, supports +/- for relative frames too."
1555 static int arg_handle_frame_start_set(int argc, const char **argv, void *data)
1557 const char *arg_id = "-s / --frame-start";
1559 Scene *scene = CTX_data_scene(C);
1562 const char *err_msg = NULL;
1563 if (!parse_int_relative_clamp(
1564 argv[1], NULL, scene->r.sfra, scene->r.sfra - 1, MINAFRAME, MAXFRAME,
1565 &scene->r.sfra, &err_msg))
1567 printf("\nError: %s '%s %s'.\n", err_msg, arg_id, argv[1]);
1572 printf("\nError: frame number must follow '%s'.\n", arg_id);
1577 printf("\nError: no blend loaded. cannot use '%s'.\n", arg_id);
1582 static const char arg_handle_frame_end_set_doc[] =
1584 "\tSet end to frame <frame>, supports +/- for relative frames too."
1586 static int arg_handle_frame_end_set(int argc, const char **argv, void *data)
1588 const char *arg_id = "-e / --frame-end";
1590 Scene *scene = CTX_data_scene(C);
1593 const char *err_msg = NULL;
1594 if (!parse_int_relative_clamp(
1595 argv[1], NULL, scene->r.efra, scene->r.efra - 1, MINAFRAME, MAXFRAME,
1596 &scene->r.efra, &err_msg))
1598 printf("\nError: %s '%s %s'.\n", err_msg, arg_id, argv[1]);
1603 printf("\nError: frame number must follow '%s'.\n", arg_id);
1608 printf("\nError: no blend loaded. cannot use '%s'.\n", arg_id);
1613 static const char arg_handle_frame_skip_set_doc[] =
1615 "\tSet number of frames to step forward after each rendered frame."
1617 static int arg_handle_frame_skip_set(int argc, const char **argv, void *data)
1619 const char *arg_id = "-j / --frame-jump";
1621 Scene *scene = CTX_data_scene(C);
1624 const char *err_msg = NULL;
1625 if (!parse_int_clamp(argv[1], NULL, 1, MAXFRAME, &scene->r.frame_step, &err_msg)) {
1626 printf("\nError: %s '%s %s'.\n", err_msg, arg_id, argv[1]);
1631 printf("\nError: number of frames to step must follow '%s'.\n", arg_id);
1636 printf("\nError: no blend loaded. cannot use '%s'.\n", arg_id);
1641 static const char arg_handle_python_file_run_doc[] =
1643 "\tRun the given Python script file."
1645 static int arg_handle_python_file_run(int argc, const char **argv, void *data)
1650 /* workaround for scripts not getting a bpy.context.scene, causes internal errors elsewhere */
1652 /* Make the path absolute because its needed for relative linked blends to be found */
1653 char filename[FILE_MAX];
1654 BLI_strncpy(filename, argv[1], sizeof(filename));
1655 BLI_path_cwd(filename, sizeof(filename));
1658 BPY_CTX_SETUP(ok = BPY_execute_filepath(C, filename, NULL));
1659 if (!ok && app_state.exit_code_on_error.python) {
1660 printf("\nError: script failed, file: '%s', exiting.\n", argv[1]);
1662 exit(app_state.exit_code_on_error.python);
1667 printf("\nError: you must specify a filepath after '%s'.\n", argv[0]);
1671 UNUSED_VARS(argc, argv, data);
1672 printf("This Blender was built without Python support\n");
1674 #endif /* WITH_PYTHON */
1677 static const char arg_handle_python_text_run_doc[] =
1679 "\tRun the given Python script text block."
1681 static int arg_handle_python_text_run(int argc, const char **argv, void *data)
1686 /* workaround for scripts not getting a bpy.context.scene, causes internal errors elsewhere */
1688 Main *bmain = CTX_data_main(C);
1689 /* Make the path absolute because its needed for relative linked blends to be found */
1690 struct Text *text = (struct Text *)BKE_libblock_find_name(bmain, ID_TXT, argv[1]);
1694 BPY_CTX_SETUP(ok = BPY_execute_text(C, text, NULL, false));
1697 printf("\nError: text block not found %s.\n", argv[1]);
1701 if (!ok && app_state.exit_code_on_error.python) {
1702 printf("\nError: script failed, text: '%s', exiting.\n", argv[1]);
1704 exit(app_state.exit_code_on_error.python);
1710 printf("\nError: you must specify a text block after '%s'.\n", argv[0]);
1714 UNUSED_VARS(argc, argv, data);
1715 printf("This Blender was built without Python support\n");
1717 #endif /* WITH_PYTHON */
1720 static const char arg_handle_python_expr_run_doc[] =
1722 "\tRun the given expression as a Python script."
1724 static int arg_handle_python_expr_run(int argc, const char **argv, void *data)
1729 /* workaround for scripts not getting a bpy.context.scene, causes internal errors elsewhere */
1732 BPY_CTX_SETUP(ok = BPY_execute_string_ex(C, NULL, argv[1], false));
1733 if (!ok && app_state.exit_code_on_error.python) {
1734 printf("\nError: script failed, expr: '%s', exiting.\n", argv[1]);
1736 exit(app_state.exit_code_on_error.python);
1741 printf("\nError: you must specify a Python expression after '%s'.\n", argv[0]);
1745 UNUSED_VARS(argc, argv, data);
1746 printf("This Blender was built without Python support\n");
1748 #endif /* WITH_PYTHON */
1751 static const char arg_handle_python_console_run_doc[] =
1752 "\n\tRun Blender with an interactive console."
1754 static int arg_handle_python_console_run(int UNUSED(argc), const char **argv, void *data)
1759 BPY_CTX_SETUP(BPY_execute_string(C, (const char *[]){"code", NULL}, "code.interact()"));
1763 UNUSED_VARS(argv, data);
1764 printf("This Blender was built without python support\n");
1766 #endif /* WITH_PYTHON */
1769 static const char arg_handle_python_exit_code_set_doc[] =
1771 "\tSet the exit-code in [0..255] to exit if a Python exception is raised\n"
1772 "\t(only for scripts executed from the command line), zero disables."
1774 static int arg_handle_python_exit_code_set(int argc, const char **argv, void *UNUSED(data))
1776 const char *arg_id = "--python-exit-code";
1778 const char *err_msg = NULL;
1779 const int min = 0, max = 255;
1781 if (!parse_int_strict_range(argv[1], NULL, min, max, &exit_code, &err_msg)) {
1782 printf("\nError: %s '%s %s', expected number in [%d..%d].\n", err_msg, arg_id, argv[1], min, max);
1786 app_state.exit_code_on_error.python = (unsigned char)exit_code;
1790 printf("\nError: you must specify an exit code number '%s'.\n", arg_id);
1795 static const char arg_handle_addons_set_doc[] =
1797 "\tComma separated list of add-ons (no spaces)."
1799 static int arg_handle_addons_set(int argc, const char **argv, void *data)
1801 /* workaround for scripts not getting a bpy.context.scene, causes internal errors elsewhere */
1804 const char script_str[] =
1805 "from addon_utils import check, enable\n"
1806 "for m in '%s'.split(','):\n"
1807 " if check(m)[1] is False:\n"
1808 " enable(m, persistent=True)";
1809 const int slen = strlen(argv[1]) + (sizeof(script_str) - 2);
1810 char *str = malloc(slen);
1812 BLI_snprintf(str, slen, script_str, argv[1]);
1814 BLI_assert(strlen(str) + 1 == slen);
1815 BPY_CTX_SETUP(BPY_execute_string_ex(C, NULL, str, false));
1818 UNUSED_VARS(argv, data);
1819 #endif /* WITH_PYTHON */
1823 printf("\nError: you must specify a comma separated list after '--addons'.\n");
1828 static int arg_handle_load_file(int UNUSED(argc), const char **argv, void *data)
1834 /* Make the path absolute because its needed for relative linked blends to be found */
1835 char filename[FILE_MAX];
1837 /* note, we could skip these, but so far we always tried to load these files */
1838 if (argv[0][0] == '-') {
1839 fprintf(stderr, "unknown argument, loading as file: %s\n", argv[0]);
1842 BLI_strncpy(filename, argv[0], sizeof(filename));
1843 BLI_path_cwd(filename, sizeof(filename));
1846 BKE_reports_init(&reports, RPT_PRINT);
1847 WM_file_autoexec_init(filename);
1848 success = WM_file_read(C, filename, &reports);
1849 BKE_reports_clear(&reports);
1853 /* ensuer we use 'C->data.scene' for background render */
1854 CTX_wm_window_set(C, NULL);
1858 /* failed to load file, stop processing arguments if running in background mode */
1860 /* Set is_break if running in the background mode so
1861 * blender will return non-zero exit code which then
1862 * could be used in automated script to control how
1863 * good or bad things are.
1869 if (BLO_has_bfile_extension(filename)) {
1870 /* Just pretend a file was loaded, so the user can press Save and it'll
1871 * save at the filename from the CLI. */
1872 BLI_strncpy(G_MAIN->name, filename, FILE_MAX);
1873 G.relbase_valid = true;
1875 printf("... opened default scene instead; saving will write to: %s\n", filename);
1878 printf("Error: argument has no '.blend' file extension, not using as new file, exiting! %s\n", filename);
1890 void main_args_setup(bContext *C, bArgs *ba)
1893 #define CB(a) a##_doc, a
1894 #define CB_EX(a, b) a##_doc_##b, a
1896 //BLI_argsAdd(ba, pass, short_arg, long_arg, doc, cb, C);
1898 /* end argument processing after -- */
1899 BLI_argsAdd(ba, -1, "--", NULL, CB(arg_handle_arguments_end), NULL);
1901 /* first pass: background mode, disable python and commands that exit after usage */
1902 BLI_argsAdd(ba, 1, "-h", "--help", CB(arg_handle_print_help), ba);
1904 BLI_argsAdd(ba, 1, "/?", NULL, CB_EX(arg_handle_print_help, win32), ba);
1906 BLI_argsAdd(ba, 1, "-v", "--version", CB(arg_handle_print_version), NULL);
1908 BLI_argsAdd(ba, 1, "-y", "--enable-autoexec", CB_EX(arg_handle_python_set, enable), (void *)true);
1909 BLI_argsAdd(ba, 1, "-Y", "--disable-autoexec", CB_EX(arg_handle_python_set, disable), (void *)false);
1911 BLI_argsAdd(ba, 1, NULL, "--disable-crash-handler", CB(arg_handle_crash_handler_disable), NULL);
1912 BLI_argsAdd(ba, 1, NULL, "--disable-abort-handler", CB(arg_handle_abort_handler_disable), NULL);
1914 BLI_argsAdd(ba, 1, "-b", "--background", CB(arg_handle_background_mode_set), NULL);
1916 BLI_argsAdd(ba, 1, "-a", NULL, CB(arg_handle_playback_mode), NULL);
1918 BLI_argsAdd(ba, 1, NULL, "--log", CB(arg_handle_log_set), ba);
1919 BLI_argsAdd(ba, 1, NULL, "--log-level", CB(arg_handle_log_level_set), ba);
1920 BLI_argsAdd(ba, 1, NULL, "--log-show-basename", CB(arg_handle_log_show_basename_set), ba);
1921 BLI_argsAdd(ba, 1, NULL, "--log-show-backtrace", CB(arg_handle_log_show_backtrace_set), ba);
1922 BLI_argsAdd(ba, 1, NULL, "--log-show-timestamp", CB(arg_handle_log_show_timestamp_set), ba);
1923 BLI_argsAdd(ba, 1, NULL, "--log-file", CB(arg_handle_log_file_set), ba);
1925 BLI_argsAdd(ba, 1, "-d", "--debug", CB(arg_handle_debug_mode_set), ba);
1928 BLI_argsAdd(ba, 1, NULL, "--debug-ffmpeg",
1929 CB_EX(arg_handle_debug_mode_generic_set, ffmpeg), (void *)G_DEBUG_FFMPEG);
1932 #ifdef WITH_FREESTYLE
1933 BLI_argsAdd(ba, 1, NULL, "--debug-freestyle",
1934 CB_EX(arg_handle_debug_mode_generic_set, freestyle), (void *)G_DEBUG_FREESTYLE);
1937 BLI_argsAdd(ba, 1, NULL, "--debug-python",
1938 CB_EX(arg_handle_debug_mode_generic_set, python), (void *)G_DEBUG_PYTHON);
1939 BLI_argsAdd(ba, 1, NULL, "--debug-events",
1940 CB_EX(arg_handle_debug_mode_generic_set, events), (void *)G_DEBUG_EVENTS);
1941 BLI_argsAdd(ba, 1, NULL, "--debug-handlers",
1942 CB_EX(arg_handle_debug_mode_generic_set, handlers), (void *)G_DEBUG_HANDLERS);
1943 BLI_argsAdd(ba, 1, NULL, "--debug-wm",
1944 CB_EX(arg_handle_debug_mode_generic_set, wm), (void *)G_DEBUG_WM);
1945 BLI_argsAdd(ba, 1, NULL, "--debug-all", CB(arg_handle_debug_mode_all), NULL);
1947 BLI_argsAdd(ba, 1, NULL, "--debug-io", CB(arg_handle_debug_mode_io), NULL);
1949 BLI_argsAdd(ba, 1, NULL, "--debug-fpe",
1950 CB(arg_handle_debug_fpe_set), NULL);
1953 BLI_argsAdd(ba, 1, NULL, "--debug-libmv", CB(arg_handle_debug_mode_libmv), NULL);
1955 #ifdef WITH_CYCLES_LOGGING
1956 BLI_argsAdd(ba, 1, NULL, "--debug-cycles", CB(arg_handle_debug_mode_cycles), NULL);
1958 BLI_argsAdd(ba, 1, NULL, "--debug-memory", CB(arg_handle_debug_mode_memory_set), NULL);
1960 BLI_argsAdd(ba, 1, NULL, "--debug-value",
1961 CB(arg_handle_debug_value_set), NULL);
1962 BLI_argsAdd(ba, 1, NULL, "--debug-jobs",
1963 CB_EX(arg_handle_debug_mode_generic_set, jobs), (void *)G_DEBUG_JOBS);
1964 BLI_argsAdd(ba, 1, NULL, "--debug-gpu",
1965 CB_EX(arg_handle_debug_mode_generic_set, gpu), (void *)G_DEBUG_GPU);
1966 BLI_argsAdd(ba, 1, NULL, "--debug-depsgraph",
1967 CB_EX(arg_handle_debug_mode_generic_set, depsgraph), (void *)G_DEBUG_DEPSGRAPH);
1968 BLI_argsAdd(ba, 1, NULL, "--debug-depsgraph-build",
1969 CB_EX(arg_handle_debug_mode_generic_set, depsgraph_build), (void *)G_DEBUG_DEPSGRAPH_BUILD);
1970 BLI_argsAdd(ba, 1, NULL, "--debug-depsgraph-eval",
1971 CB_EX(arg_handle_debug_mode_generic_set, depsgraph_eval), (void *)G_DEBUG_DEPSGRAPH_EVAL);
1972 BLI_argsAdd(ba, 1, NULL, "--debug-depsgraph-tag",
1973 CB_EX(arg_handle_debug_mode_generic_set, depsgraph_tag), (void *)G_DEBUG_DEPSGRAPH_TAG);
1974 BLI_argsAdd(ba, 1, NULL, "--debug-depsgraph-time",
1975 CB_EX(arg_handle_debug_mode_generic_set, depsgraph_time), (void *)G_DEBUG_DEPSGRAPH_TIME);
1976 BLI_argsAdd(ba, 1, NULL, "--debug-depsgraph-no-threads",
1977 CB_EX(arg_handle_debug_mode_generic_set, depsgraph_no_threads), (void *)G_DEBUG_DEPSGRAPH_NO_THREADS);
1978 BLI_argsAdd(ba, 1, NULL, "--debug-depsgraph-pretty",
1979 CB_EX(arg_handle_debug_mode_generic_set, depsgraph_pretty), (void *)G_DEBUG_DEPSGRAPH_PRETTY);
1980 BLI_argsAdd(ba, 1, NULL, "--debug-gpumem",
1981 CB_EX(arg_handle_debug_mode_generic_set, gpumem), (void *)G_DEBUG_GPU_MEM);
1982 BLI_argsAdd(ba, 1, NULL, "--debug-gpu-shaders",
1983 CB_EX(arg_handle_debug_mode_generic_set, gpumem), (void *)G_DEBUG_GPU_SHADERS);
1984 BLI_argsAdd(ba, 1, NULL, "--debug-gpu-force-workarounds",
1985 CB_EX(arg_handle_debug_mode_generic_set, gpumem), (void *)G_DEBUG_GPU_FORCE_WORKAROUNDS);
1987 BLI_argsAdd(ba, 1, NULL, "--verbose", CB(arg_handle_verbosity_set), NULL);
1989 BLI_argsAdd(ba, 1, NULL, "--app-template", CB(arg_handle_app_template), NULL);
1990 BLI_argsAdd(ba, 1, NULL, "--factory-startup", CB(arg_handle_factory_startup_set), NULL);
1991 BLI_argsAdd(ba, 1, NULL, "--enable-static-override", CB(arg_handle_enable_static_override), NULL);
1992 BLI_argsAdd(ba, 1, NULL, "--enable-event-simulate", CB(arg_handle_enable_event_simulate), NULL);
1994 /* TODO, add user env vars? */
1995 BLI_argsAdd(ba, 1, NULL, "--env-system-datafiles", CB_EX(arg_handle_env_system_set, datafiles), NULL);
1996 BLI_argsAdd(ba, 1, NULL, "--env-system-scripts", CB_EX(arg_handle_env_system_set, scripts), NULL);
1997 BLI_argsAdd(ba, 1, NULL, "--env-system-python", CB_EX(arg_handle_env_system_set, python), NULL);
1999 /* second pass: custom window stuff */
2000 BLI_argsAdd(ba, 2, "-p", "--window-geometry", CB(arg_handle_window_geometry), NULL);
2001 BLI_argsAdd(ba, 2, "-w", "--window-border", CB(arg_handle_with_borders), NULL);
2002 BLI_argsAdd(ba, 2, "-W", "--window-fullscreen", CB(arg_handle_without_borders), NULL);
2003 BLI_argsAdd(ba, 2, "-M", "--window-maximized", CB(arg_handle_window_maximized), NULL);
2004 BLI_argsAdd(ba, 2, NULL, "--no-window-focus", CB(arg_handle_no_window_focus), NULL);
2005 BLI_argsAdd(ba, 2, "-con", "--start-console", CB(arg_handle_start_with_console), NULL);
2006 BLI_argsAdd(ba, 2, "-R", NULL, CB(arg_handle_register_extension), NULL);
2007 BLI_argsAdd(ba, 2, "-r", NULL, CB_EX(arg_handle_register_extension, silent), ba);
2008 BLI_argsAdd(ba, 2, NULL, "--no-native-pixels", CB(arg_handle_native_pixels_set), ba);
2010 /* third pass: disabling things and forcing settings */
2011 BLI_argsAddCase(ba, 3, "-noaudio", 1, NULL, 0, CB(arg_handle_audio_disable), NULL);
2012 BLI_argsAddCase(ba, 3, "-setaudio", 1, NULL, 0, CB(arg_handle_audio_set), NULL);
2014 /* fourth pass: processing arguments */
2015 BLI_argsAdd(ba, 4, "-f", "--render-frame", CB(arg_handle_render_frame), C);
2016 BLI_argsAdd(ba, 4, "-a", "--render-anim", CB(arg_handle_render_animation), C);
2017 BLI_argsAdd(ba, 4, "-S", "--scene", CB(arg_handle_scene_set), C);
2018 BLI_argsAdd(ba, 4, "-s", "--frame-start", CB(arg_handle_frame_start_set), C);
2019 BLI_argsAdd(ba, 4, "-e", "--frame-end", CB(arg_handle_frame_end_set), C);
2020 BLI_argsAdd(ba, 4, "-j", "--frame-jump", CB(arg_handle_frame_skip_set), C);
2021 BLI_argsAdd(ba, 4, "-P", "--python", CB(arg_handle_python_file_run), C);
2022 BLI_argsAdd(ba, 4, NULL, "--python-text", CB(arg_handle_python_text_run), C);
2023 BLI_argsAdd(ba, 4, NULL, "--python-expr", CB(arg_handle_python_expr_run), C);
2024 BLI_argsAdd(ba, 4, NULL, "--python-console", CB(arg_handle_python_console_run), C);
2025 BLI_argsAdd(ba, 4, NULL, "--python-exit-code", CB(arg_handle_python_exit_code_set), NULL);
2026 BLI_argsAdd(ba, 4, NULL, "--addons", CB(arg_handle_addons_set), C);
2028 BLI_argsAdd(ba, 4, "-o", "--render-output", CB(arg_handle_output_set), C);
2029 BLI_argsAdd(ba, 4, "-E", "--engine", CB(arg_handle_engine_set), C);
2031 BLI_argsAdd(ba, 4, "-F", "--render-format", CB(arg_handle_image_type_set), C);
2032 BLI_argsAdd(ba, 1, "-t", "--threads", CB(arg_handle_threads_set), NULL);
2033 BLI_argsAdd(ba, 4, "-x", "--use-extension", CB(arg_handle_extension_set), C);
2041 * Needs to be added separately.
2043 void main_args_setup_post(bContext *C, bArgs *ba)
2045 BLI_argsParse(ba, 4, arg_handle_load_file, C);
2050 #endif /* WITH_PYTHON_MODULE */