Blender V4.3
BPY_extern_run.hh File Reference
#include "BLI_sys_types.h"
#include "BLI_compiler_attrs.h"

Go to the source code of this file.

Classes

struct  BPy_RunErrInfo
 

Functions

Run File/Text as a Script
Note
BPY_run_filepath and BPY_run_filepath have almost identical behavior one operates on a file-path, the other on a blender text-block.
bool BPY_run_filepath (bContext *C, const char *filepath, ReportList *reports) ATTR_NONNULL(1
 
bool bool BPY_run_text (bContext *C, Text *text, ReportList *reports, bool do_jump) ATTR_NONNULL(1
 
Run a String as a Script
  • Use 'eval' for simple single-line expressions.
  • Use 'exec' for full multi-line scripts.
bool BPY_run_string_exec (bContext *C, const char *imports[], const char *expr)
 
bool BPY_run_string_eval (bContext *C, const char *imports[], const char *expr)
 
Run a String as a Script & Return the Result

Convenience functions for executing a script and returning the result as an expected type.

bool BPY_run_string_as_number (bContext *C, const char *imports[], const char *expr, BPy_RunErrInfo *err_info, double *r_value) ATTR_NONNULL(1
 
bool bool BPY_run_string_as_intptr (bContext *C, const char *imports[], const char *expr, BPy_RunErrInfo *err_info, intptr_t *r_value) ATTR_NONNULL(1
 
bool bool bool BPY_run_string_as_string_and_len (bContext *C, const char *imports[], const char *expr, BPy_RunErrInfo *err_info, char **r_value, size_t *r_value_len) ATTR_NONNULL(1
 
bool bool bool bool BPY_run_string_as_string (bContext *C, const char *imports[], const char *expr, BPy_RunErrInfo *err_info, char **r_value) ATTR_NONNULL(1
 
bool bool bool bool bool BPY_run_string_as_string_and_len_or_none (bContext *C, const char *imports[], const char *expr, BPy_RunErrInfo *err_info, char **r_value, size_t *r_value_len) ATTR_NONNULL(1
 
bool bool bool bool bool bool BPY_run_string_as_string_or_none (bContext *C, const char *imports[], const char *expr, BPy_RunErrInfo *err_info, char **r_value) ATTR_NONNULL(1
 

Detailed Description

Common Arguments

  • C the bContext (never NULL).
  • imports: This is simply supported for convenience since imports can make constructing strings more cumbersome as otherwise small expressions become multi-line code-blocks. Optional (ignored when NULL), otherwise this is a NULL terminated array of module names.

    Failure to import any modules prevents any further execution.

  • err_info BPy_RunErrInfo is passed to some functions so errors can be forwarded to the UI. Option (when NULL errors are printed to the stdout and cleared). However this should be used in any case the error would be useful to show to the user.

Definition in file BPY_extern_run.hh.

Function Documentation

◆ BPY_run_filepath()

bool BPY_run_filepath ( bContext * C,
const char * filepath,
ReportList * reports )

Execute filepath as a Python script.

Wrapper for PyRun_File (similar to calling python with a script argument). Used for the --python command line argument.

Parameters
CThe context (never NULL).
filepathThe file path to execute.
reportsFailure to execute the script will report the exception here (may be NULL).
Returns
true on success, otherwise false with an error reported to reports.
Note
Python scripts could consider bpy.utils.execfile, which has the advantage of returning the object as a module for data access & caching pyc file for faster re-execution.

Referenced by arg_handle_python_file_run(), Freestyle::PythonInterpreter::interpretFile(), and run_pyfile_exec().

◆ BPY_run_string_as_intptr()

bool bool BPY_run_string_as_intptr ( bContext * C,
const char * imports[],
const char * expr,
BPy_RunErrInfo * err_info,
intptr_t * r_value )

Evaluate expr as an integer or pointer.

Note
Support both int and pointers.
Parameters
CSee Common Arguments.
importsSee Common Arguments.
exprThe expression to evaluate.
err_infoSee Common Arguments.
r_valueThe resulting value.
Returns
Success.

Referenced by ui_tooltip_data_from_tool().

◆ BPY_run_string_as_number()

bool BPY_run_string_as_number ( bContext * C,
const char * imports[],
const char * expr,
BPy_RunErrInfo * err_info,
double * r_value )

Evaluate expr as a number (double).

Parameters
CSee Common Arguments.
importsSee Common Arguments.
exprThe expression to evaluate.
err_infoSee Common Arguments.
r_valueThe resulting value.
Returns
Success.

Referenced by ui_number_from_string(), and user_string_to_number().

◆ BPY_run_string_as_string()

bool bool bool bool BPY_run_string_as_string ( bContext * C,
const char * imports[],
const char * expr,
BPy_RunErrInfo * err_info,
char ** r_value )

◆ BPY_run_string_as_string_and_len()

bool bool bool BPY_run_string_as_string_and_len ( bContext * C,
const char * imports[],
const char * expr,
BPy_RunErrInfo * err_info,
char ** r_value,
size_t * r_value_len )

Evaluate expr as a string.

Parameters
CSee Common Arguments.
importsSee Common Arguments.
exprThe expression to evaluate.
err_infoSee Common Arguments.
r_valueThe resulting value.
Returns
Success.

Referenced by ui_tooltip_data_from_tool().

◆ BPY_run_string_as_string_and_len_or_none()

bool bool bool bool bool BPY_run_string_as_string_and_len_or_none ( bContext * C,
const char * imports[],
const char * expr,
BPy_RunErrInfo * err_info,
char ** r_value,
size_t * r_value_len )

Evaluate expr as a string or None. Where a success return value with *r_value == nullptr indicates a value of None.

Parameters
CSee Common Arguments.
importsSee Common Arguments.
exprThe expression to evaluate.
err_infoSee Common Arguments.
r_valueThe resulting value.
Returns
Success.

◆ BPY_run_string_as_string_or_none()

bool bool bool bool bool bool BPY_run_string_as_string_or_none ( bContext * C,
const char * imports[],
const char * expr,
BPy_RunErrInfo * err_info,
char ** r_value )

◆ BPY_run_string_eval()

bool BPY_run_string_eval ( bContext * C,
const char * imports[],
const char * expr )

Run an expression, matches: exec(compile(..., "eval")).

Parameters
COptional context (may be null), used for bpy.context and reporting errors to CTX_wm_reports(C).

Definition at line 293 of file bpy_interface_run.cc.

References bpy_run_string_impl().

Referenced by arg_handle_python_console_run(), BPY_python_start(), Freestyle::PythonInterpreter::interpretString(), script_reload_exec(), WM_exit_ex(), wm_file_read_post(), wm_homefile_read_ex(), wm_init_scripts_extensions_once(), and WM_keyconfig_reload().

◆ BPY_run_string_exec()

bool BPY_run_string_exec ( bContext * C,
const char * imports[],
const char * expr )

Run an entire script, matches: exec(compile(..., "exec"))

Parameters
COptional context (may be null), used for bpy.context and reporting errors to CTX_wm_reports(C).

Definition at line 298 of file bpy_interface_run.cc.

References bpy_run_string_impl().

Referenced by arg_handle_addons_set(), arg_handle_python_expr_run(), script_reload_exec(), and wm_file_read_post().

◆ BPY_run_text()

bool bool BPY_run_text ( bContext * C,
Text * text,
ReportList * reports,
bool do_jump )

Execute a Blender text block as a Python script.

Wrapper for Py_CompileStringObject & PyEval_EvalCode. Used for the --python-text command line argument.

Parameters
CThe context (never NULL).
textThe text-block to execute.
reportsFailure to execute the script will report the exception here (may be NULL).
do_jumpWhen true, any error moves the cursor to the location of that error. Useful for executing scripts interactively from the text editor.
Returns
true on success, otherwise false with an error reported to reports.
Note
The __file__ is constructed by joining the blend file-path to the name of the text. This is done so error messages give useful output however there are rare cases causes problems with introspection tools which attempt to load __file__.

Referenced by arg_handle_python_text_run(), BPY_modules_load_user(), Freestyle::PythonInterpreter::interpretFile(), Freestyle::PythonInterpreter::interpretText(), and text_run_script().