0
$\begingroup$

How can I use the Python API to run a Python script, exactly as if it were either opened with .use_module=True or run with or bpy.ops.text.run_script()?

I don't think the the existing answers are about the same question, because I think the use of exec() will necessarily pollute the namespace/globals/locals it's given, likely clobbering existing names. It also seems to have trouble with imports.

Things I've tried:

  • exec(compile(), {}, {}): Doesn't handle imports of external modules correctly.

  • Text.as_module() (followed by .register()): Doesn't set __file__ correctly.

  • ops.script.reload(): Doesn't seem to actually run user scripts.

$\endgroup$
2
  • $\begingroup$ Helo, are you running scripts from the command line ? $\endgroup$
    – Gorgious
    Jun 7 '21 at 6:23
  • $\begingroup$ @Gorgious I'm actually running them from another script, hence why clobbering globals() is unacceptable. For now I'm just switching the context area type to use ops.text.run_script(), but that's a bit dirty, so hopefully a better solution is possible. $\endgroup$
    – Will Chen
    Jun 7 '21 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.