Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement scene.camera.follow(function) #200

Merged
merged 1 commit into from
Mar 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 17 additions & 4 deletions vpython/vpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down