Skip to content

Commit

Permalink
Merge 96b0fa3 into 5cc1da0
Browse files Browse the repository at this point in the history
  • Loading branch information
Stéphane Caron committed Jan 19, 2023
2 parents 5cc1da0 + 96b0fa3 commit 31d4020
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 3 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ pip install meshcat-shapes

## Shapes

| ``meshcat_shapes.frame`` | ``meshcat_shapes.point`` |
|--------------------------|--------------------------|
| <img src="https://github.com/stephane-caron/meshcat-shapes/raw/main/gallery/frame.png" width="250"> | <img src="https://github.com/stephane-caron/meshcat-shapes/raw/main/gallery/point.png" width="250"> |
| ``meshcat_shapes.frame`` | ``meshcat_shapes.point`` | ``meshcat_shapes.textarea`` |
|--------------------------|--------------------------|-----------------------------|
| <img src="https://github.com/stephane-caron/meshcat-shapes/raw/main/gallery/frame.png" width="250"> | <img src="https://github.com/stephane-caron/meshcat-shapes/raw/main/gallery/point.png" width="250"> | <img src="https://github.com/stephane-caron/meshcat-shapes/raw/main/gallery/textarea.png" width="250"> |

PRs are welcome: open one if you have implemented a shape that can be useful to others.

Expand All @@ -43,4 +43,9 @@ meshcat_shapes.point(
radius=0.05,
color=0xFF0000,
)

meshcat_shapes.textarea(
vis["text"],
"super easy",
)
```
46 changes: 46 additions & 0 deletions examples/textarea.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2022 Stéphane Caron
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import meshcat
import numpy as np

import meshcat_shapes

if __name__ == "__main__":
vis = meshcat.Visualizer().open()

meshcat_shapes.textarea(
vis["foo"],
"super easy",
)

meshcat_shapes.textarea(
vis["bar"],
"barely an inconvenience",
width=1.5,
height=1.5,
font_size=200,
)

Rx = meshcat.transformations.rotation_matrix(0.5 * np.pi, [1.0, 0.0, 0.0])
Rz = meshcat.transformations.rotation_matrix(0.5 * np.pi, [0.0, 0.0, 1.0])
vis["foo"].set_transform(Rz.dot(Rx))

trans = meshcat.transformations.translation_matrix([0.0, 1.0, 0.2])
vis["bar"].set_transform(trans @ Rz @ Rx)

input("Press Enter to exit...")
Binary file added gallery/textarea.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions meshcat_shapes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

from .frame import frame
from .point import point
from .textarea import textarea

__all__ = [
"frame",
"point",
"textarea",
]
168 changes: 168 additions & 0 deletions meshcat_shapes/textarea.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2023 Inria
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This file incorporates work covered by the following copyright and permission
# notice:
#
# geometry.py from https://github.com/rdeits/meshcat-python/pull/32
# Copyright (c) 2018 Shen Shen
# License: MIT

"""Text area.
Notes:
To be simplified once https://github.com/rdeits/meshcat-python/pull/111
makes it to a released version of meshcat-python.
"""

import meshcat
from meshcat.geometry import Geometry, Mesh, MeshPhongMaterial, Texture


class Plane(Geometry):
"""Plane segment.
Attributes:
height: Plane segment height.
height_segments: Number of height segments.
width: Plane segment width.
width_segments: Number of width segments.
"""

height: float
height_segments: int
width: float
width_segments: int

def __init__(
self,
width: float = 1.0,
height: float = 1.0,
width_segments: int = 1,
height_segments: int = 1,
):
"""Initialize plane segment.
Args:
width: Plane segment width.
height: Plane segment height.
width_segments: Number of width segments.
height_segments: Number of height segments.
"""
super().__init__()
self.height = height
self.height_segments = height_segments
self.width = width
self.width_segments = width_segments

def lower(self, _):
"""Serialize object.
Args:
object_data: Unused.
"""
return {
"uuid": self.uuid,
"type": "PlaneGeometry",
"width": self.width,
"height": self.height,
"widthSegments": self.width_segments,
"heightSegments": self.height_segments,
}


class TextTexture(Texture):
"""Text texture.
Attributes:
font_face: Font face.
font_size: Font size in px.
text: Text content.
"""

font_face: str
font_size: int
text: str

def __init__(
self,
text: str,
font_size: int = 100,
font_face: str = "sans-serif",
):
"""Initialize text texture.
Args:
text: Text content.
font_size: Font size in px.
font_face: Font face.
"""
super().__init__()
self.font_face = font_face
self.font_size = font_size
self.text = text

def lower(self, _):
"""Serialize texture.
Args:
object_data: Unused.
"""
return {
"uuid": self.uuid,
"type": "_text",
"text": self.text,
"font_size": self.font_size,
"font_face": self.font_face,
}


def textarea(
handle: meshcat.Visualizer,
text: str,
width: float = 1.0,
height: float = 1.0,
font_size: int = 100,
font_face: str = "sans-serif",
) -> None:
"""
Set MeshCat handle to a text area.
Args:
handle: MeshCat handle to attach the frame to.
text: Content to write inside the text area.
width: Text area width.
height: Text area height.
font_size: Font size in px.
font_face: Font face.
Note:
As per the de-facto standard (Blender, OpenRAVE, RViz, ...), the
x-axis is red, the y-axis is green and the z-axis is blue.
"""
handle.set_object(
Mesh(
Plane(width=width, height=height),
MeshPhongMaterial(
map=TextTexture(
text, font_size=font_size, font_face=font_face
),
transparent=True,
needsUpdate=True,
),
)
)

0 comments on commit 31d4020

Please sign in to comment.