diff --git a/vspreview/cores.py b/vspreview/cores.py new file mode 100644 index 0000000..72957b8 --- /dev/null +++ b/vspreview/cores.py @@ -0,0 +1,35 @@ +from vapoursynth import EnvironmentPolicyAPI, register_policy +from vapoursynth import EnvironmentData + + +__all__ = ("create_and_register_policy",) + + +class VSPreviewEnvironmentPolicy: + _current_environment: EnvironmentData + _api: EnvironmentPolicyAPI + + def reload_core(self): + old_environment = self._current_environment + self._current_environment = self._api.create_environment() + self._api.destroy_environment(old_environment) + + def on_policy_registered(self, special_api: EnvironmentPolicyAPI): + self._current_environment = special_api.create_environment() + self._api = special_api + + def on_policy_cleared(self): + pass + + def get_current_environment(self) -> EnvironmentData: + return self._current_environment + + def set_environment(self, environment) -> EnvironmentData: + if environment != self._current_environment: + raise ValueError("The passed environment is not valid.") + + +def create_and_register_policy() -> VSPreviewEnvironmentPolicy: + policy = VSPreviewEnvironmentPolicy() + register_policy(policy) + return policy diff --git a/vspreview/main.py b/vspreview/main.py index a5d2ac1..0022f41 100644 --- a/vspreview/main.py +++ b/vspreview/main.py @@ -1,5 +1,13 @@ from __future__ import annotations +# import vspreview.cores as early as possible: +# This is so other modules cannot accidentally +# use and lock us into a different policy. +from vspreview.cores import create_and_register_policy +policy = create_and_register_policy() + +# And now back to our regular programing: + import logging import os from pathlib import Path @@ -9,6 +17,7 @@ from PyQt5 import Qt import vapoursynth as vs + from vspreview.core import ( AbstractMainWindow, AbstractToolbar, AbstractToolbars, Frame, FrameInterval, Output, Time, TimeInterval, @@ -608,6 +617,8 @@ def reload_script(self) -> None: vs.clear_outputs() self.graphics_scene.clear() self.outputs.clear() + policy.reload_core() + # make sure old filter graph is freed gc.collect()