Skip to content

Commit

Permalink
refactor(external_bpy): Move clean temp dir to SetupUtility, don't ca…
Browse files Browse the repository at this point in the history
…ll setup in external mode

Adjust, so the part using bpy isn't imported
in CLI, where bpy might not be available.
  • Loading branch information
Griperis committed Dec 12, 2024
1 parent 7a162cf commit 78d0bf2
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 36 deletions.
5 changes: 4 additions & 1 deletion blenderproc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
# Also clean the python path as this might disturb the pip installs
if "PYTHONPATH" in os.environ:
del os.environ["PYTHONPATH"]
SetupUtility.setup([])

if not is_using_external_bpy_module():
SetupUtility.setup([])

from .api import loader
from .api import utility
from .api import sampler
Expand Down
7 changes: 3 additions & 4 deletions blenderproc/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# pylint: disable=wrong-import-position
from blenderproc.python.utility.SetupUtility import SetupUtility, is_using_external_bpy_module
from blenderproc.python.utility.InstallUtility import InstallUtility
from blenderproc.python.utility.Utility import Utility
# pylint: enable=wrong-import-position


Expand Down Expand Up @@ -179,7 +178,7 @@ def cli():
finally:
assert script_directory in sys.path
sys.path.remove(script_directory)
Utility.clean_temp_dir()
SetupUtility.clean_temp_dir()
else:
# Install blender, if not already done
custom_blender_path, blender_install_path = InstallUtility.determine_blender_install_path(args)
Expand Down Expand Up @@ -207,7 +206,7 @@ def cli():

# Listen for SIGTERM signal, so we can properly clean up and terminate the child process
def handle_sigterm(_signum, _frame):
Utility.clean_temp_dir()
SetupUtility.clean_temp_dir()
p.terminate()

signal.signal(signal.SIGTERM, handle_sigterm)
Expand All @@ -222,7 +221,7 @@ def handle_sigterm(_signum, _frame):
p.wait()

# Clean up
Utility.clean_temp_dir()
SetupUtility.clean_temp_dir()

sys.exit(p.returncode)
# Import the required entry point
Expand Down
6 changes: 3 additions & 3 deletions blenderproc/python/utility/Initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import bpy

from blenderproc.python.utility.GlobalStorage import GlobalStorage
from blenderproc.python.utility.Utility import Utility, reset_keyframes
from blenderproc.python.utility.Utility import reset_keyframes
from blenderproc.python.utility.SetupUtility import SetupUtility, is_using_external_bpy_module
from blenderproc.python.camera import CameraUtility
from blenderproc.python.utility.DefaultConfig import DefaultConfig
from blenderproc.python.renderer import RendererUtility


def handle_sigterm(_signum, _frame):
Utility.clean_temp_dir()
SetupUtility.clean_temp_dir()


def init(clean_up_scene: bool = True):
Expand All @@ -39,7 +39,7 @@ def init(clean_up_scene: bool = True):
# this is the only mandatory initialization point and any of the code in command_line.py
# isn't executed.
SetupUtility.setup_utility_paths(SetupUtility.determine_temp_dir(None))
atexit.register(Utility.clean_temp_dir)
atexit.register(SetupUtility.clean_temp_dir)
signal.signal(signal.SIGTERM, handle_sigterm)

if clean_up_scene:
Expand Down
55 changes: 34 additions & 21 deletions blenderproc/python/utility/SetupUtility.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import tarfile
from sys import platform
import shutil
import subprocess
import importlib
from io import BytesIO
Expand All @@ -17,14 +18,14 @@
from blenderproc.python.utility.DefaultConfig import DefaultConfig


def is_using_external_bpy_module():
def is_using_external_bpy_module() -> bool:
"""Returns True if the external bpy module is used, False otherwise.
At this point we don't check whether 'bpy' is available, this is handled in the first lines of
__init__.py. When using external by module, we assume it's available all the time as the
script had to fail before.
If using blenderproc's Blender installation, setup goes through the InstallUtility and
If using BlenderProc's Blender installation, setup goes through the InstallUtility and
SetupUtility.
"""
return os.environ.get("USE_EXTERNAL_BPY_MODULE", "0") == "1"
Expand Down Expand Up @@ -59,26 +60,28 @@ def setup(user_required_packages: Optional[List[str]] = None, blender_path: Opti
:return: List of sys.argv after removing blender specific commands
"""

if not is_using_external_bpy_module():
packages_path = SetupUtility.setup_pip(user_required_packages, blender_path, major_version, reinstall_packages)
if not SetupUtility.main_setup_called:
SetupUtility.main_setup_called = True
sys.path.append(packages_path)
is_debug_mode = "--background" not in sys.argv

# Setup temporary directory
if is_debug_mode:
SetupUtility.setup_utility_paths("examples/debugging/temp")
else:
SetupUtility.setup_utility_paths(sys.argv[sys.argv.index("--") + 2])
if is_using_external_bpy_module():
raise RuntimeError("USE_EXTERNAL_BPY_MODULE is set, calling this is not necessary in external mode.")

packages_path = SetupUtility.setup_pip(user_required_packages, blender_path, major_version, reinstall_packages)
if not SetupUtility.main_setup_called:
SetupUtility.main_setup_called = True
sys.path.append(packages_path)
is_debug_mode = "--background" not in sys.argv

# Setup temporary directory
if is_debug_mode:
SetupUtility.setup_utility_paths("examples/debugging/temp")
else:
SetupUtility.setup_utility_paths(sys.argv[sys.argv.index("--") + 2])

# Only prepare args in non-debug mode (In debug mode the arguments are already ready to use)
if not is_debug_mode:
# Cut off blender specific arguments
sys.argv = sys.argv[sys.argv.index("--") + 1:sys.argv.index("--") + 2] + \
sys.argv[sys.argv.index("--") + 3:]
elif debug_args is not None:
sys.argv = ["debug"] + debug_args
# Only prepare args in non-debug mode (In debug mode the arguments are already ready to use)
if not is_debug_mode:
# Cut off blender specific arguments
sys.argv = sys.argv[sys.argv.index("--") + 1:sys.argv.index("--") + 2] + \
sys.argv[sys.argv.index("--") + 3:]
elif debug_args is not None:
sys.argv = ["debug"] + debug_args

return sys.argv

Expand All @@ -95,6 +98,16 @@ def setup_utility_paths(temp_dir: str):
Utility.temp_dir = resolve_path(temp_dir)
os.makedirs(Utility.temp_dir, exist_ok=True)

@staticmethod
def clean_temp_dir() -> None:
""" Removes the temporary directory if it exists. """
# pylint: disable=import-outside-toplevel,cyclic-import
from blenderproc.python.utility.Utility import Utility
# pylint: enable=import-outside-toplevel,cyclic-import
if os.path.exists(Utility.temp_dir):
print("Cleaning temporary directory")
shutil.rmtree(Utility.temp_dir)

@staticmethod
def determine_python_paths(blender_path: Optional[str], major_version: Optional[str]) -> Union[str, str, str, str]:
""" Determines python binary, custom pip packages and the blender pip packages path.
Expand Down
7 changes: 0 additions & 7 deletions blenderproc/python/utility/Utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,6 @@ def get_temporary_directory() -> str:
"""
return Utility.temp_dir

@staticmethod
def clean_temp_dir() -> None:
""" Removes the temporary directory if it exists. """
if os.path.exists(Utility.temp_dir):
print("Cleaning temporary directory")
shutil.rmtree(Utility.temp_dir)

@staticmethod
def merge_dicts(source: Dict[Any, Any], destination: Dict[Any, Any]) -> Dict[Any, Any]:
""" Recursively copies all key value pairs from src to dest (Overwrites existing)
Expand Down

0 comments on commit 78d0bf2

Please sign in to comment.