From dc8dd24351462e73c5d0260564aad9cd56fd6c33 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 18 Apr 2019 14:45:23 +0200 Subject: PyAPI: remove support for importing text blocks as modules Allowing direct import of text blocks isn't especially useful, instead add `text.as_module()` script authors can do this explicitly if it's needed. Now the text "Register" option executes instead of loading as a module. This removes the need to keep track of the current Main, and C code to override Python's import & reload. --- release/scripts/modules/bpy_types.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'release/scripts/modules') diff --git a/release/scripts/modules/bpy_types.py b/release/scripts/modules/bpy_types.py index 32e8fe40c6a..fb4ccfaff7a 100644 --- a/release/scripts/modules/bpy_types.py +++ b/release/scripts/modules/bpy_types.py @@ -523,6 +523,15 @@ class Text(bpy_types.ID): self.clear() self.write(string) + def as_module(self): + from os.path import splitext + from types import ModuleType + mod = ModuleType(splitext(self.name)[0]) + # TODO: We could use Text.compiled (C struct member) + # if this is called often it will be much faster. + exec(self.as_string(), mod.__dict__) + return mod + class Sound(bpy_types.ID): __slots__ = () -- cgit v1.2.3