Fix T88899: `__file__` not set for `text.as_module()`

This commit is contained in:
Campbell Barton 2021-06-07 14:04:26 +10:00
parent 7ef2b760dc
commit 98876d46ef
Notes: blender-bot 2023-02-14 05:22:18 +01:00
Referenced by issue #88449, Blender LTS: Maintenance Task 2.93
Referenced by issue #88899, __file__ not set during `.as_module()`
1 changed files with 10 additions and 2 deletions

View File

@ -560,9 +560,17 @@ class Text(bpy_types.ID):
self.write(string)
def as_module(self):
from os.path import splitext
import bpy
from os.path import splitext, join
from types import ModuleType
mod = ModuleType(splitext(self.name)[0])
name = self.name
mod = ModuleType(splitext(name)[0])
# This is a fake file-path, set this since some scripts check `__file__`,
# error messages may include this as well.
# NOTE: the file path may be a blank string if the file hasn't been saved.
mod.__dict__.update({
"__file__": join(bpy.data.filepath, name),
})
# 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__)