Analog Clock example

This example renders an analog clock with the current time. It shows an alternate way to update an application continually through a QTimer, instead of having to derive from vrAEBase and attaching the script to VRED’s application loop. The QTimer serves essentially the same purpose. Since we do not specify an update interval, the timer will trigger every time QT updates.

Apart from this, the new decoupled interface is used for finding nodes and updating the rotation of vrdGeometryNodes.

When the script is running, the clock can be stopped with clock.stop(). It can be restarted again with clock.start() and completely reset with clock.reset().

analog_clock.py


print("Executing analog clock script!")

newScene()
loadGeometry("$VRED_EXAMPLES/geo/analog_clock.osb")
updateScene()

import math
from datetime import datetime
from PySide2.QtCore import QTimer
from PySide2.QtGui import QVector3D

# Deactivate clock: clock.stop()
# Reactivate clock: clock.start()
# Stop-Delete-Reset clock: clock.reset()


class clockWork():

    def __init__(self):
        self.needle_hours = vrNodeService.findNode("Clock_Needle_Hours")
        self.needle_minutes = vrNodeService.findNode("Clock_Needle_Minutes")
        self.needle_seconds = vrNodeService.findNode("Clock_Needle_Seconds")
        self.timer = QTimer();
        self.timer.timeout.connect(self.updateClock)

    def start(self):
        if self.timer.isActive():
            return
        self.timer.start()
        vrLogInfo('Clock activated')

    def stop(self):
        self.timer.stop()
        vrLogInfo('Stopping clock.')

    def reset(self):
        self.stop()
        self.needle_hours.setRotationAsEuler(QVector3D(0,0,0))
        self.needle_minutes.setRotationAsEuler(QVector3D(0,0,0))
        self.needle_seconds.setRotationAsEuler(QVector3D(0,0,0))
        vrLogInfo('Resetting clock.')              

    def getAngle(self, arg):
        return (arg / 60) * 360

    def getAngleHour(self, hour, minute):
        return (hour * 30) + (minute / 60) * 30

    def updateClock(self):
        now = datetime.now()

        seconds_angle = self.getAngle(now.second)
        minutes_angle = self.getAngle(now.minute)
        hours_angle = self.getAngleHour(now.hour, now.minute)    

        self.needle_seconds.setRotationAsEuler(QVector3D(0, 0, -seconds_angle))
        self.needle_minutes.setRotationAsEuler(QVector3D(0, 0, -minutes_angle))
        self.needle_hours.setRotationAsEuler(QVector3D(0, 0, -hours_angle))


clock = clockWork()
clock.start()