Author Topic: Size bar issues in v3.0.0-dev322  (Read 106 times)

rkoch

  • Newbie
  • *
  • Posts: 2
Size bar issues in v3.0.0-dev322
« on: March 27, 2019, 02:24:53 PM »
Good morning,

In the past I've successfull used the code provided here to generate a size bar in rendered images:
https://www.ovito.org/manual/viewport_overlays.python_script.html

Code: [Select]
from PyQt5.QtCore import *
from PyQt5.QtGui import *

# Parameters:
bar_length = 40   # Simulation units (e.g. Angstroms)
bar_color = QColor(0,0,0)
label_text = "{} nm".format(bar_length/10)
label_color = QColor(255,255,255)

# This function is called by OVITO on every viewport update.
def render(painter, **args):
if args['is_perspective']:
raise Exception("This only works with non-perspective viewports.")

# Compute length of bar in screen spaaace
screen_length = 0.5 * bar_length * painter.window().height() / args['fov']

# Define geometry of bar in screen spaaace
height = 0.07 * painter.window().height()
margin = 0.02 * painter.window().height()
rect = QRectF(margin, margin, screen_length, height)

# Render bar
painter.fillRect(rect, bar_color)

# Render text label
font = painter.font()
font.setPixelSize(height)
painter.setFont(font)
painter.setPen(QPen(label_color))
painter.drawText(rect, Qt.AlignCenter, label_text)

Now, in version 3.0.0-dev322, this code is returning:
Code: [Select]
The Python script has exited with an error.
Traceback (most recent call last):
  File "<string>", line 12, in render
KeyError: 'is_perspective'

If I remove the offending lines:
Code: [Select]
The Python script has exited with an error.
Traceback (most recent call last):
  File "<string>", line 14, in render
AttributeError: 'PyScript.Arguments' object has no attribute 'window'

It appears that some syntax has changed?
I understand that this is a dev version, but I rather like the new features it introduces and would like to avoid downgrading.

Any help is appreciated,
Rob
« Last Edit: March 27, 2019, 02:53:11 PM by rkoch »

Constanze Kalcher

  • Administrator
  • Sr. Member
  • *****
  • Posts: 277
Re: Size bar issues in v3.0.0-dev322
« Reply #1 on: March 27, 2019, 02:57:12 PM »
Dear Rob,

yes, in OVITO 3 quite some changes have been made to the python API (and also the PythonViewportOverlay class). Please see
https://www.ovito.org/manual_testing/python/introduction/version_changes.html
for a detailed overview.

Here you can find the updated version of the scale-bar example script:
https://www.ovito.org/manual_testing/python/introduction/examples.html#example-scale-bar-overlay

-Constanze

rkoch

  • Newbie
  • *
  • Posts: 2
Re: Size bar issues in v3.0.0-dev322
« Reply #2 on: March 27, 2019, 03:09:29 PM »
Excellent, thank you. This worked perfectly.  Eventually I really should take a bit of time and learn some of the syntax behind the python interface in Ovito.

Cheers,
Rob