Skip to content

Create a new vapoursynth.Core every time you reload a script #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions vspreview/cores.py
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions vspreview/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,6 +17,7 @@
from PyQt5 import Qt
import vapoursynth as vs


from vspreview.core import (
AbstractMainWindow, AbstractToolbar, AbstractToolbars,
Frame, FrameInterval, Output, Time, TimeInterval,
Expand Down Expand Up @@ -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()

Expand Down