39 lines
1.6 KiB
Python
39 lines
1.6 KiB
Python
from __future__ import annotations
|
|
import sys
|
|
from typing import Any
|
|
|
|
|
|
def show_in_text_editor(name: str, content: str, bpy_module: Any = None) -> Any:
|
|
"""Displays text content inside a Blender TEXT_EDITOR area."""
|
|
bsn = sys.modules.get("NodeSlicer.infra.blender.blender_slice_nodes_generic")
|
|
if bsn and hasattr(bsn, "show_in_text_editor"):
|
|
fn = getattr(bsn, "show_in_text_editor")
|
|
if type(fn).__name__ == "MagicMock" or type(fn).__module__.startswith("unittest.mock"):
|
|
return fn(name, content)
|
|
|
|
try:
|
|
if bpy_module is None:
|
|
if bsn is not None and hasattr(bsn, "bpy"):
|
|
bpy_module = bsn.bpy
|
|
else:
|
|
import bpy
|
|
bpy_module = bpy
|
|
|
|
text_block = bpy_module.data.texts.get(name)
|
|
if text_block is None:
|
|
text_block = bpy_module.data.texts.new(name=name)
|
|
text_block.from_string(content)
|
|
|
|
wm = getattr(bpy_module.context, "window_manager", None)
|
|
if wm and hasattr(wm, "windows"):
|
|
for window in wm.windows:
|
|
if hasattr(window, "screen") and window.screen and hasattr(window.screen, "areas"):
|
|
for area in window.screen.areas:
|
|
if getattr(area, "type", "") == 'TEXT_EDITOR':
|
|
for space in getattr(area, "spaces", []):
|
|
if getattr(space, "type", "") == 'TEXT_EDITOR':
|
|
space.text = text_block
|
|
return text_block
|
|
except Exception as e:
|
|
print(f"Error displaying in text editor: {e}")
|
|
return None
|