アナログ時計のサンプル

このサンプルでは、現在の時刻を示すアナログ時計をレンダリングします。vrAEBase からスクリプトを取得して VRED のアプリケーション ループにアタッチする代わりに、QTimer を介してアプリケーションを継続的に更新する別の方法を示します。QTimer は基本的に同じ目的で使用されます。更新間隔は指定されていないため、タイマは QT が更新されるたびにトリガされます。

これ以外に、分離された新しいインタフェースがあり、ノードの検索と vrdGeometryNodes の回転の更新に使用されます。

スクリプトの実行中は、clock.stop() を使用してクロックを停止できます。再起動するには clock.start() を、完全にリセットするには 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()