Skip to content

Commit 71a3d91

Browse files
committed
make a Path class and test animation of properties
1 parent 8d17683 commit 71a3d91

File tree

5 files changed

+60
-8
lines changed

5 files changed

+60
-8
lines changed

src/meshcat/animation.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def __init__(self, clips=None, default_framerate=30):
7676

7777
def lower(self):
7878
return [{
79-
u"path": unicode("/" + "/".join(path)),
79+
u"path": path.lower(),
8080
u"clip": clip.lower()
8181
} for (path, clip) in self.clips.items()]
8282

@@ -111,8 +111,12 @@ def set_transform(self, matrix):
111111
clip.set_property(self.current_frame, "position", "vector3", js_position(matrix))
112112
clip.set_property(self.current_frame, "quaternion", "quaternion", js_quaternion(matrix))
113113

114+
def set_property(self, prop, jstype, value):
115+
clip = self.get_clip()
116+
clip.set_property(self.current_frame, prop, jstype, value)
117+
114118
def __getitem__(self, path):
115-
return AnimationFrameVisualizer(self.animation, self.path + tuple(path.split("/"), self.current_frame))
119+
return AnimationFrameVisualizer(self.animation, self.path.append(path), self.current_frame)
116120

117121
def __enter__(self):
118122
return self

src/meshcat/commands.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def lower(self):
2626
return {
2727
u"type": u"set_object",
2828
u"object": self.object.lower(),
29-
u"path": unicode("/" + "/".join(self.path))
29+
u"path": self.path.lower()
3030
}
3131

3232

@@ -39,7 +39,7 @@ def __init__(self, matrix, path=[]):
3939
def lower(self):
4040
return {
4141
u"type": u"set_transform",
42-
u"path": unicode("/" + "/".join(self.path)),
42+
u"path": self.path.lower(),
4343
u"matrix": list(self.matrix.T.flatten())
4444
}
4545

@@ -52,7 +52,7 @@ def __init__(self, path):
5252
def lower(self):
5353
return {
5454
u"type": u"delete",
55-
u"path": unicode("/" + "/".join(self.path))
55+
u"path": self.path.lower()
5656
}
5757

5858

src/meshcat/path.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from __future__ import absolute_import, division, print_function
2+
3+
import sys
4+
if sys.version_info >= (3, 0):
5+
unicode = str
6+
7+
8+
class Path(object):
9+
__slots__ = ["entries"]
10+
11+
def __init__(self, entries=tuple()):
12+
self.entries = entries
13+
14+
def append(self, other):
15+
new_path = self.entries
16+
for element in other.split('/'):
17+
if len(element) == 0:
18+
new_path = tuple()
19+
else:
20+
new_path = new_path + (element,)
21+
return Path(new_path)
22+
23+
def lower(self):
24+
return unicode("/" + "/".join(self.entries))
25+
26+
def __hash__(self):
27+
return hash(self.entries)
28+
29+
def __eq__(self, other):
30+
return self.entries == other.entries

src/meshcat/tests/test_drawing.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ def tearDown(self):
101101

102102
class TestAnimation(VisualizerTest):
103103
def runTest(self):
104-
self.vis.delete()
105104
v = self.vis["shapes"]
106105
v.set_transform(tf.translation_matrix([1., 0, 0]))
107106
v["cube"].set_object(g.Box([0.1, 0.2, 0.3]))
@@ -112,3 +111,21 @@ def runTest(self):
112111
with animation.at_frame(v, 30) as frame_vis:
113112
frame_vis.set_transform(tf.translation_matrix([2, 0, 0]).dot(tf.rotation_matrix(np.pi/2, [0, 0, 1])))
114113
v.set_animation(animation)
114+
115+
116+
class TestCameraAnimation(VisualizerTest):
117+
def runTest(self):
118+
v = self.vis["shapes"]
119+
v.set_transform(tf.translation_matrix([1., 0, 0]))
120+
v["cube"].set_object(g.Box([0.1, 0.2, 0.3]))
121+
122+
animation = meshcat.animation.Animation()
123+
with animation.at_frame(v, 0) as frame_vis:
124+
frame_vis.set_transform(tf.translation_matrix([0, 0, 0]))
125+
with animation.at_frame(v, 30) as frame_vis:
126+
frame_vis.set_transform(tf.translation_matrix([2, 0, 0]).dot(tf.rotation_matrix(np.pi/2, [0, 0, 1])))
127+
with animation.at_frame(v, 0) as frame_vis:
128+
frame_vis["/Cameras/default/rotated/<object>"].set_property("zoom", "number", 1)
129+
with animation.at_frame(v, 30) as frame_vis:
130+
frame_vis["/Cameras/default/rotated/<object>"].set_property("zoom", "number", 0.5)
131+
v.set_animation(animation)

src/meshcat/visualizer.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import zmq
1010
import re
1111

12+
from .path import Path
1213
from .commands import SetObject, SetTransform, Delete, SetAnimation
1314
from .geometry import MeshPhongMaterial
1415

@@ -98,7 +99,7 @@ def __init__(self, zmq_url=None, window=None):
9899
self.window = ViewerWindow(zmq_url=zmq_url, start_server=(zmq_url is None))
99100
else:
100101
self.window = window
101-
self.path = ("meshcat",)
102+
self.path = Path(("meshcat",))
102103

103104
@staticmethod
104105
def view_into(window, path):
@@ -128,7 +129,7 @@ def jupyter_cell(self):
128129
""".format(url=self.url()))
129130

130131
def __getitem__(self, path):
131-
return Visualizer.view_into(self.window, self.path + tuple(path.split("/")))
132+
return Visualizer.view_into(self.window, self.path.append(path))
132133

133134
def set_object(self, geometry, material=None):
134135
return self.window.send(SetObject(geometry, material, self.path))

0 commit comments

Comments
 (0)