line-objects-update.py 2.08 KB
Newer Older
1 2 3 4 5 6 7 8 9
# This script showcases lines and parked runnables
#
# The script creates a line object between the positions of the Earth and the Moon. Then,
# it parks a runnable which updates the line points with the new positions of the
# objects, so that the line is always up to date, even when the objects move. Finally,
# time is started to showcase the line movement.

# Created by Toni Sagrista

10
from py4j.java_gateway import JavaGateway, GatewayParameters, CallbackServerParameters
11

12
class LineUpdaterRunnable(object):
13 14
    def __init__(self, polyline):
        self.polyline = polyline
15

16 17 18
    def run(self):
        earthp = gs.getObjectPosition("Earth")
        moonp = gs.getObjectPosition("Moon")
19 20 21 22 23 24 25 26 27
        pl = self.polyline.getPointCloud()

        pl.setX(0, earthp[0])
        pl.setY(0, earthp[1])
        pl.setZ(0, earthp[2])
        pl.setX(1, moonp[0])
        pl.setY(1, moonp[1])
        pl.setZ(1, moonp[2])

28 29
        self.polyline.markForUpdate()

30 31 32 33 34 35 36 37 38
    def toString():
        return "line-update-runnable"

    class Java:
        implements = ["java.lang.Runnable"]

gateway = JavaGateway(gateway_parameters=GatewayParameters(auto_convert=True),
                      callback_server_parameters=CallbackServerParameters())
gs = gateway.entry_point
39 40 41 42

gs.cameraStop()

gs.stopSimulationTime()
43 44
gs.setVisibility("element.orbits", True)
gs.setCameraLock(True)
45 46 47 48 49

gs.setFov(49)

gs.goToObject("Earth", 91.38e-2)

50
print("We will now add a line between the Earth and Moon")
51 52 53 54

earthp = gs.getObjectPosition("Earth")
moonp = gs.getObjectPosition("Moon")

55
gs.addPolyline("line-em", [earthp[0], earthp[1], earthp[2], moonp[0], moonp[1], moonp[2]], [ 1., .2, .2, .8 ], 1 )
56

57
gs.sleep(0.5)
58

59
# create line
60 61
line_em = gs.getObject("line-em")

62
# park the line updater
63 64
gs.parkRunnable("line-updater", LineUpdaterRunnable(line_em))

65
gs.setSimulationPace(1e5)
66 67
gs.startSimulationTime()

68
gs.sleep(20)
69 70 71

gs.stopSimulationTime()

72 73
# clean up and finish
print("Cleaning up and ending")
74 75 76 77 78 79 80

gs.unparkRunnable("line-updater")
gs.removeModelObject("line-em")
gs.cameraStop()

gs.maximizeInterfaceWindow()
gs.enableInput()
81 82 83 84

# close connection
gateway.close()