Skip to content

Commit 437a35a

Browse files
committedFeb 15, 2019
finalizing
1 parent 8d6f3e7 commit 437a35a

File tree

4 files changed

+40
-53
lines changed

4 files changed

+40
-53
lines changed
 

‎demo.ipynb

+22-4
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157
"cell_type": "markdown",
158158
"metadata": {},
159159
"source": [
160-
"In addition to the usual geometries / objects, it is also possible to write 2d texts, either onto the scene:"
160+
"MeshCat supports simple 2d texts rendering. For example, to write 2d texts onto a geometry or object:"
161161
]
162162
},
163163
{
@@ -166,14 +166,14 @@
166166
"metadata": {},
167167
"outputs": [],
168168
"source": [
169-
"vis.set_text('hello world')"
169+
"vis.set_text('Hello, world!',g.Box([1, 1, 1]),font_size=20)"
170170
]
171171
},
172172
{
173173
"cell_type": "markdown",
174174
"metadata": {},
175175
"source": [
176-
"Or onto an object:"
176+
"It is also possible to simple write 'floating' texts onto a scene without attached to an object (e.g., for scene discription):"
177177
]
178178
},
179179
{
@@ -182,7 +182,25 @@
182182
"metadata": {},
183183
"outputs": [],
184184
"source": [
185-
"vis.set_text('hello world',g.Box([0.2, 0.2, 0.2]))"
185+
"vis.set_text('Hello, world!')"
186+
]
187+
},
188+
{
189+
"cell_type": "markdown",
190+
"metadata": {},
191+
"source": [
192+
"Under the hood, the 'floating' texts are written onto a `g.Plane()` geometry, so to change the texts size:"
193+
]
194+
},
195+
{
196+
"cell_type": "code",
197+
"execution_count": null,
198+
"metadata": {},
199+
"outputs": [],
200+
"source": [
201+
"for i in np.linspace(10,2,20):\n",
202+
" vis.set_text('Hello, world!', plane_width=2*i,plane_height=i,font_size=200)\n",
203+
" time.sleep(0.05)"
186204
]
187205
},
188206
{

‎src/meshcat/commands.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,17 @@ def lower(self):
3636
class SetText:
3737
__slots__ = ["object", "path"]
3838

39-
def __init__(self, text, geometry_or_object, material=None, path=[]):
40-
self.text_texture = TextTexture(text)
39+
def __init__(self, text, geometry_or_object, plane_width=10,
40+
plane_height=5, material=None, path=[], **kwargs):
41+
self.text_texture = TextTexture(text, **kwargs)
4142
if isinstance(geometry_or_object, Object):
4243
if material is not None:
4344
raise(ArgumentError(
4445
"Please supply either an Object OR a Geometry and a Material"))
4546
self.object = geometry_or_object
4647
else:
4748
if geometry_or_object is None:
48-
geometry_or_object = Plane(width=10, height=5)
49+
geometry_or_object = Plane(width=plane_width, height=plane_height)
4950
# if writing onto the scene, default material is transparent
5051
material = MeshPhongMaterial(map=self.text_texture,
5152
needsUpdate=True, transparent=True)

‎src/meshcat/geometry.py

+7-39
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ class Ring(Geometry):
9797
Optional thetaStart and thetaLength by default gives a full ring (2pi angle)
9898
"""
9999

100-
def __init__(self, innerRadius, outerRadius, thetaStart=0, thetaLength=np.pi * 2):
100+
def __init__(self, innerRadius, outerRadius, thetaStart=0,
101+
thetaLength=np.pi * 2):
101102
super(Ring, self).__init__()
102103
self.innerRadius = innerRadius
103104
self.outerRadius = outerRadius
@@ -138,36 +139,6 @@ def lower(self, object_data):
138139
u"thetaLength": self.thetaLength
139140
}
140141

141-
142-
# class Text(Geometry):
143-
144-
# def __init__(self, text, font='helvetiker', size=100, height=50,
145-
# curveSegments=12, bevelEnabled=False, bevelThickness=10,
146-
# bevelSize=8, bevelSegments=3):
147-
# super(Text, self).__init__()
148-
# self.text = text
149-
# self.font = font
150-
# self.size = size
151-
# self.curveSegments = curveSegments
152-
# self.bevelEnabled = bevelEnabled
153-
# self.bevelThickness = bevelThickness
154-
# self.bevelSize = bevelSize
155-
# self.bevelSegments = bevelSegments
156-
157-
# def lower(self, object_data):
158-
# {
159-
# u"uuid": self.uuid,
160-
# u"type": u"_textgeometry",
161-
# u"text": self.text,
162-
# u"font": self.font,
163-
# u"size": self.size,
164-
# u"height": self.height,
165-
# u"curveSegments": self.curveSegments
166-
# }
167-
168-
169-
170-
171142
class Plane(Geometry):
172143

173144
def __init__(self, width=1, height=1, widthSegments=1, heightSegments=1):
@@ -217,11 +188,10 @@ def lower(self, object_data):
217188
}
218189

219190

220-
221191
class MeshMaterial(Material):
222192

223193
def __init__(self, color=0xffffff, reflectivity=0.5, map=None,
224-
transparent = False, **kwargs):
194+
transparent=False, **kwargs):
225195
super(MeshMaterial, self).__init__()
226196
self.color = color
227197
self.reflectivity = reflectivity
@@ -285,20 +255,18 @@ def __init__(self):
285255
def lower(self, object_data):
286256
return {
287257
u"uuid": self.uuid,
288-
# hacky, needs a nominal "url" filed to get rid of JS side warning,
289-
# so putting an empty one here
290258
u"url": ""
291259
}
292260

293261

294262
class TextTexture(Texture):
295263

296-
def __init__(self, text, font_size = 96, font_face='sans-serif',
297-
width=200, height=100, position=[10, 10]):
264+
def __init__(self, text, font_size=96, font_face='sans-serif',
265+
width=200, height=100, position=[10, 10]):
298266
super(TextTexture, self).__init__()
299267
self.text = text
300-
# font_size will be passed to the JS side as is; however if the text
301-
# width exceeds canvas width, font_size will be reduced.
268+
# font_size will be passed to the JS side as is; however if the
269+
# text width exceeds canvas width, font_size will be reduced.
302270
self.font_size = font_size
303271
self.font_face = font_face
304272
self.width = width

‎src/meshcat/visualizer.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ def capture(pattern, s):
2121
else:
2222
return match.groups()[0]
2323

24+
2425
def match_zmq_url(line):
2526
return capture(r"^zmq_url=(.*)$", line)
2627

28+
2729
def match_web_url(line):
2830
return capture(r"^web_url=(.*)$", line)
2931

@@ -54,12 +56,9 @@ def __init__(self, zmq_url, start_server):
5456
# callback in the server until we reconnect.
5557
self.connect_zmq()
5658

57-
5859
print("You can open the visualizer by visiting the following URL:")
5960
print(self.web_url)
6061

61-
62-
6362
def connect_zmq(self):
6463
self.zmq_socket = self.context.socket(zmq.REQ)
6564
self.zmq_socket.connect(self.zmq_url)
@@ -137,9 +136,11 @@ def set_object(self, geometry, material=None):
137136
def set_transform(self, matrix=np.eye(4)):
138137
return self.window.send(SetTransform(matrix, self.path))
139138

140-
def set_text(self,text, geometry=None,material=None):
141-
return self.window.send(SetText(text,geometry,material,self.path))
142-
139+
def set_text(self, text, geometry=None, plane_width=10, plane_height=5,
140+
material=None,**kwargs):
141+
return self.window.send(SetText(text, geometry, plane_width=plane_width,
142+
plane_height=plane_height, material=material, path=self.path, **kwargs))
143+
143144
def set_animation(self, animation, play=True, repetitions=1):
144145
return self.window.send(SetAnimation(animation, play=play, repetitions=repetitions))
145146

@@ -166,4 +167,3 @@ def __repr__(self):
166167

167168
while True:
168169
time.sleep(100)
169-

0 commit comments

Comments
 (0)