Skip to content

Commit 72e9fef

Browse files
Merge pull request #200 from vpython/improve_follow
Implement scene.camera.follow(function)
2 parents 5f12bcd + 1453043 commit 72e9fef

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

vpython/vpython.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ class baseObj(object):
188188
updates = {'cmds':[], 'methods':[], 'attrs':{}}
189189
object_registry = {} ## idx -> instance
190190
attach_arrows = []
191-
attach_trails = [] ## needed only for functions
191+
attach_trails = [] # needed only for functions
192+
follow_objects = [] # entries are [invisible object to follow, function to call for pos, prevous pos]
192193
attrs = set() # each element is (idx, attr name)
193194

194195
@classmethod
@@ -232,6 +233,15 @@ def handle_attach(cls): # called when about to send data to the browser
232233
continue
233234
aa._last_val = fval
234235

236+
## update every scene.camera.follow(function)
237+
for aa in cls.follow_objects:
238+
obj = aa[0]
239+
val = aa[1]()
240+
lastpos = aa[2]
241+
if val != lastpos:
242+
aa[2] = val
243+
obj.pos = val
244+
235245
def __init__(self, **kwargs):
236246
if not (baseObj._view_constructed or
237247
baseObj._canvas_constructing):
@@ -1338,7 +1348,7 @@ def round(self):
13381348
return self._round
13391349
@round.setter
13401350
def round(self,value):
1341-
raise AttributeError('Cannot change the "round" attribute of an attach_arrow.')
1351+
raise AttributeError('Cannot change the "round" attribute of an arrow.')
13421352

13431353
@property
13441354
def scale(self):
@@ -2770,7 +2780,6 @@ def project(self, **args):
27702780
class Camera(object):
27712781
def __init__(self, canvas):
27722782
self._canvas = canvas
2773-
self._followthis = None
27742783
self._pos = None
27752784

27762785
@property
@@ -2933,9 +2942,13 @@ def __init__(self, **args):
29332942
distant_light(direction=vector(-0.88, -0.22, -0.44), color=color.gray(0.3))
29342943
baseObj._canvas_constructing = False
29352944

2936-
def follow(self, obj): ## should allow a function also
2945+
def follow(self, obj):
29372946
if obj is None:
29382947
self.addmethod('follow', 'None')
2948+
elif callable(obj):
2949+
b = box(visible=False)
2950+
baseObj.follow_objects.append([b, obj, vector(1.2e15,3.4e14,-5.6e13)])
2951+
self.addmethod('follow', b.idx)
29392952
else:
29402953
self.addmethod('follow', obj.idx)
29412954

0 commit comments

Comments
 (0)