From 14530436ad4ccb3a2a7ff6a65d79cdeb0a47d4a5 Mon Sep 17 00:00:00 2001 From: BruceSherwood Date: Sat, 5 Mar 2022 09:43:05 -0800 Subject: [PATCH] Implement scene.camera.follow(function) --- vpython/vpython.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/vpython/vpython.py b/vpython/vpython.py index 1cb1d5c..b17757b 100644 --- a/vpython/vpython.py +++ b/vpython/vpython.py @@ -188,7 +188,8 @@ class baseObj(object): updates = {'cmds':[], 'methods':[], 'attrs':{}} object_registry = {} ## idx -> instance attach_arrows = [] - attach_trails = [] ## needed only for functions + attach_trails = [] # needed only for functions + follow_objects = [] # entries are [invisible object to follow, function to call for pos, prevous pos] attrs = set() # each element is (idx, attr name) @classmethod @@ -232,6 +233,15 @@ def handle_attach(cls): # called when about to send data to the browser continue aa._last_val = fval + ## update every scene.camera.follow(function) + for aa in cls.follow_objects: + obj = aa[0] + val = aa[1]() + lastpos = aa[2] + if val != lastpos: + aa[2] = val + obj.pos = val + def __init__(self, **kwargs): if not (baseObj._view_constructed or baseObj._canvas_constructing): @@ -1338,7 +1348,7 @@ def round(self): return self._round @round.setter def round(self,value): - raise AttributeError('Cannot change the "round" attribute of an attach_arrow.') + raise AttributeError('Cannot change the "round" attribute of an arrow.') @property def scale(self): @@ -2770,7 +2780,6 @@ def project(self, **args): class Camera(object): def __init__(self, canvas): self._canvas = canvas - self._followthis = None self._pos = None @property @@ -2933,9 +2942,13 @@ def __init__(self, **args): distant_light(direction=vector(-0.88, -0.22, -0.44), color=color.gray(0.3)) baseObj._canvas_constructing = False - def follow(self, obj): ## should allow a function also + def follow(self, obj): if obj is None: self.addmethod('follow', 'None') + elif callable(obj): + b = box(visible=False) + baseObj.follow_objects.append([b, obj, vector(1.2e15,3.4e14,-5.6e13)]) + self.addmethod('follow', b.idx) else: self.addmethod('follow', obj.idx)