Skip to content

Commit

Permalink
feat(external_bpy): Setup temporary dir in init when in external mode
Browse files Browse the repository at this point in the history
And signal handlers, so everything works the same when using blenderproc module
in standalone script.
  • Loading branch information
Griperis committed Dec 10, 2024
1 parent 7096b07 commit 7a162cf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
14 changes: 4 additions & 10 deletions blenderproc/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import argparse
import os
import shutil
import signal
import sys
import subprocess
Expand All @@ -13,6 +12,7 @@
# 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 @@ -141,12 +141,6 @@ def cli():
# Setup temp dir
temp_dir = SetupUtility.determine_temp_dir(args.temp_dir)

def clean_temp_dir():
# If temp dir should not be kept and temp dir still exists => remove it
if not args.keep_temp_dir and os.path.exists(temp_dir):
print("Cleaning temporary directory")
shutil.rmtree(temp_dir)

# Setup env vars
used_environment = dict(os.environ, PYTHONPATH=repo_root_directory, PYTHONNOUSERSITE="1")
# this is done to enable the import of blenderproc inside the blender internal python environment
Expand Down Expand Up @@ -185,7 +179,7 @@ def clean_temp_dir():
finally:
assert script_directory in sys.path
sys.path.remove(script_directory)
clean_temp_dir()
Utility.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 @@ -213,7 +207,7 @@ def clean_temp_dir():

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

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

# Clean up
clean_temp_dir()
Utility.clean_temp_dir()

sys.exit(p.returncode)
# Import the required entry point
Expand Down
18 changes: 16 additions & 2 deletions blenderproc/python/utility/Initializer.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
""" This module provides functions to init a BlenderProc scene. """

import atexit
import os
import random
import signal

from numpy import random as np_random
import bpy

from blenderproc.python.utility.GlobalStorage import GlobalStorage
from blenderproc.python.utility.Utility import reset_keyframes
from blenderproc.python.utility.SetupUtility import is_using_external_bpy_module
from blenderproc.python.utility.Utility import Utility, 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()


def init(clean_up_scene: bool = True):
""" Initializes BlenderProc.
Expand All @@ -28,6 +34,14 @@ def init(clean_up_scene: bool = True):
raise RuntimeError("BlenderProc has already been initialized via bproc.init(), this should not be done twice. "
"If you want to clean up the scene, use bproc.clean_up().")

if is_using_external_bpy_module():
# When in external mode we setup the temporary directory, and the cleanup handlers here, as
# 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)
signal.signal(signal.SIGTERM, handle_sigterm)

if clean_up_scene:
clean_up(clean_up_camera=True)

Expand Down
8 changes: 8 additions & 0 deletions blenderproc/python/utility/Utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import csv
import shutil
import sys
import threading
from types import TracebackType
Expand Down Expand Up @@ -101,6 +102,13 @@ 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 7a162cf

Please sign in to comment.