The results of render_image and the OVITO GUI in Qt are different

When I add MyOverlay (“Hello world” text) to the viewport, it appears in the rendered image generated by render_image (fig. 1). However, it does not show up in the GUI created using Qt and ovito.create_qt_widget (fig.2). I am using virtual env. Installed packages in fig3.

My code:

import sys

from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QSlider, QVBoxLayout
app = QApplication(sys.argv)

import ovito
from ovito.io import import_file
from ovito.vis import Viewport, ViewportOverlayInterface, PythonViewportOverlay

class MyOverlay(ViewportOverlayInterface):
def render(self, canvas: ViewportOverlayInterface.Canvas ,**kwargs):
canvas.draw_text(
text=“Hello world”,
pos=(0.5, 0.5),
anchor=“center”,
font_size=0.1,
color=(1,0,0),
alpha=0.5
)

filePath = r"C:\Users\Builder\Desktop\temp\cnt\cnt.xyz"

xlo, xhi = -15, 25
ylo, yhi = -15, 25
zlo, zhi = -15, 25
pbc = (True, True, True)

pipeline = import_file(filePath)
pipeline.add_to_scene()

mainwin = QMainWindow()
mainwin.setWindowTitle(‘OVITO Trajectory Viewer’)
central_widget = QWidget()
central_layout = QVBoxLayout(central_widget)
mainwin.setCentralWidget(central_widget)

vp = Viewport(type=Viewport.Type.Ortho, camera_dir=(1,0,0))
vp.overlays.append(PythonViewportOverlay( delegate=MyOverlay() ) )
vp.zoom_all()
vp.render_image(filename=“output.png”, size=(800,600))

vp_widget = vp.create_qt_widget()
central_layout.addWidget(vp_widget, 1)

time_slider = QSlider(Qt.Horizontal)
time_slider.setTickInterval(1)
time_slider.setTickPosition(QSlider.TicksBothSides)
central_layout.addWidget(time_slider)

def on_time_slider(frame: int):
ovito.scene.anim.current_frame = frame
time_slider.valueChanged.connect(on_time_slider)

mainwin.resize(800, 600)
mainwin.show()

def CustomModChangeSimBoxProperty(frame, data):
data.cell_[:, 0] = (xhi - xlo, 0, 0)
data.cell_[:, 1] = (0, yhi-ylo, 0)
data.cell_[:, 2] = (0, 0, zhi-zlo)
data.cell_[:, 3] = (xlo,ylo,zlo)
data.cell_.pbc = pbc

pipeline.modifiers.append(CustomModChangeSimBoxProperty)
data = pipeline.compute()

time_slider.setMaximum(pipeline.source.num_frames - 1)

vp.zoom_all((vp_widget.width(), vp_widget.height()))

sys.exit(app.exec())

Fig.1

Fig.2

Fig.3

image

Currently, overlays are only rendered by interactive viewport windows for which the “preview mode” is enabled. You can activate this mode programmatically by setting the (undocumented) flag vp.preview_mode=True.

1 Like