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¶
1# © 2024 Autodesk, Inc. All rights reserved.
2
3print("Executing analog clock script!")
4
5newScene()
6loadGeometry("$VRED_EXAMPLES/geo/analog_clock.osb")
7updateScene()
8
9from datetime import datetime
10from PySide6.QtCore import QTimer
11from PySide6.QtGui import QVector3D
12
13# Deactivate clock: clock.stop()
14# Reactivate clock: clock.start()
15# Stop-Delete-Reset clock: clock.reset()
16
17
18class clockWork():
19
20 def __init__(self):
21 self.needle_hours = vrNodeService.findNode("Clock_Needle_Hours")
22 self.needle_minutes = vrNodeService.findNode("Clock_Needle_Minutes")
23 self.needle_seconds = vrNodeService.findNode("Clock_Needle_Seconds")
24 self.timer = QTimer();
25 self.timer.timeout.connect(self.updateClock)
26
27 def start(self):
28 if self.timer.isActive():
29 return
30 self.timer.start()
31 vrLogInfo('Clock activated')
32
33 def stop(self):
34 self.timer.stop()
35 vrLogInfo('Stopping clock.')
36
37 def reset(self):
38 self.stop()
39 self.needle_hours.setRotationAsEuler(QVector3D(0,0,0))
40 self.needle_minutes.setRotationAsEuler(QVector3D(0,0,0))
41 self.needle_seconds.setRotationAsEuler(QVector3D(0,0,0))
42 vrLogInfo('Resetting clock.')
43
44 def getAngle(self, arg):
45 return (arg / 60) * 360
46
47 def getAngleHour(self, hour, minute):
48 return (hour * 30) + (minute / 60) * 30
49
50 def updateClock(self):
51 now = datetime.now()
52
53 seconds_angle = self.getAngle(now.second)
54 minutes_angle = self.getAngle(now.minute)
55 hours_angle = self.getAngleHour(now.hour, now.minute)
56
57 self.needle_seconds.setRotationAsEuler(QVector3D(0, 0, -seconds_angle))
58 self.needle_minutes.setRotationAsEuler(QVector3D(0, 0, -minutes_angle))
59 self.needle_hours.setRotationAsEuler(QVector3D(0, 0, -hours_angle))
60
61
62clock = clockWork()
63clock.start()