Skip to content
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

Add option to always get screenshots from plotter shows #5197

Merged
merged 11 commits into from
Nov 18, 2023
2 changes: 2 additions & 0 deletions pyvista/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
# Set where figures are saved
FIGURE_PATH = os.environ.get("PYVISTA_FIGURE_PATH", None)

ON_SCREENSHOT = os.environ.get("PYVISTA_ON_SCREENSHOT", "false").lower() == "true"

# Send VTK messages to the logging module:
send_errors_to_logging()

Expand Down
9 changes: 7 additions & 2 deletions pyvista/plotting/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from threading import Thread
import time
from typing import Dict, Optional
import uuid
import warnings
import weakref

Expand Down Expand Up @@ -6404,7 +6405,7 @@ def __init__(
notebook = scooby.in_ipykernel()

self.notebook = notebook
if self.notebook:
if self.notebook or pyvista.ON_SCREENSHOT:
off_screen = True
self.off_screen = off_screen

Expand Down Expand Up @@ -6760,7 +6761,11 @@ def fun(plotter):
"A screenshot is unable to be taken as the render window is not current or rendering is suppressed."
)
if _is_current:
self.last_image = self.screenshot(screenshot, return_img=True)
if pyvista.ON_SCREENSHOT:
filename = uuid.uuid4().hex
self.last_image = self.screenshot(filename, return_img=True)
else:
self.last_image = self.screenshot(screenshot, return_img=True)
self.last_image_depth = self.get_image_depth()
# NOTE: after this point, nothing from the render window can be accessed
# as if a user pressed the close button, then it destroys the
Expand Down
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
pyvista.OFF_SCREEN = True


@fixture()
def global_variables_reset():
tmp_screenshots = pyvista.ON_SCREENSHOT
tmp_figurepath = pyvista.FIGURE_PATH
yield
pyvista.ON_SCREENSHOT = tmp_screenshots
pyvista.FIGURE_PATH = tmp_figurepath


@fixture(scope='session', autouse=True)
def set_mpl():
"""Avoid matplotlib windows popping up."""
Expand Down
25 changes: 25 additions & 0 deletions tests/plotting/test_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
./plotting/test_plotting.py

"""
import os

import numpy as np
import pytest

Expand Down Expand Up @@ -464,3 +466,26 @@ def test_plotter_update_coordinates(sphere):
raise RuntimeError("Convert error this method")
if pv._version.version_info >= (0, 47):
raise RuntimeError("Remove this method")


def test_only_screenshots_flag(sphere, tmpdir, global_variables_reset):
pv.FIGURE_PATH = str(tmpdir)
pv.ON_SCREENSHOT = True

entries = os.listdir(pv.FIGURE_PATH)
pl = pv.Plotter()
pl.add_mesh(sphere)
pl.show()
entries_after = os.listdir(pv.FIGURE_PATH)
assert len(entries) + 1 == len(entries_after)

res_file = list(set(entries_after) - set(entries))[0]
pv.ON_SCREENSHOT = False
sphere_screenshot = "sphere_screenshot.png"
pl = pv.Plotter()
pl.add_mesh(sphere)
pl.show(screenshot=sphere_screenshot)
sphere_path = os.path.join(pv.FIGURE_PATH, sphere_screenshot)
res_path = os.path.join(pv.FIGURE_PATH, res_file)
error = pv.compare_images(sphere_path, res_path)
assert error < 100