Skip to content

Commit

Permalink
Merge pull request #764 from compas-dev/robot_visuals_blender
Browse files Browse the repository at this point in the history
hide meshes on instantiation of RobotModelArtist
  • Loading branch information
beverlylytle authored Feb 10, 2021
2 parents 7dce88d + 1a4108d commit bdf7b4d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 30 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added ability of `RobotModel.add_link` to accept primitives in addition to meshes.
* Fixed bug regarding the computation of `Joint.current_origin`.
* Fixed bug regarding a repeated call to `RobotModel.add_joint`.
* Fixed bug in `compas_blender.RobotModelArtist.update`.
* Fixed bug in `compas.datastructures.mesh_slice_plane`.
* Fixed bug where initialising a `compas_blender.artists.Robotmodelartist` would create a new collection for each mesh and then also not put the mesh iton the created collection.
* Changed the initialisation of `compas_blender.artists.Robotmodelartist` to include a `collection`-parameter instead of a `layer`-parameter to be more consistent with Blender's nomenclature.
Expand Down Expand Up @@ -528,12 +529,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Split up network and mesh naming conventions.
* Reworked network face cycle finding.
* Updated mesh from lines.
* Updated network plotter in correspondance with network.
* Updated network plotter in correspondence with network.
* Integrated mixin functionality and removed mixins.
* Meshes are now initially hidden in `compas_blender.artists.RobotModelArtist`.
* `compas_blender.artists.RobotModelArtist.draw_visual` and `compas_blender.artists.RobotModelArtist.draw_collision` now show those meshes.
* Renamed the method `draw_geometry` of `compas.robots.base_artist.RobotModelBaseArtist` to `create_geometry`.

### Removed

* Removed parallelisation from network algorithms.
* Removed parallelization from network algorithms.
* Removed numba based dr implementations.

## [0.15.0] 2020-01-24
Expand Down
20 changes: 10 additions & 10 deletions src/compas/robots/base_artist/_artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ def transform(self, geometry, transformation):
Parameters
----------
geometry : object
A CAD-specific (i.e. native) geometry object as returned by :meth:`draw_geometry`.
A CAD-specific (i.e. native) geometry object as returned by :meth:`create_geometry`.
transformation : `Transformation`
**COMPAS** transformation to update the geometry object.
"""
raise NotImplementedError

def draw_geometry(self, geometry, name=None, color=None):
def create_geoemetry(self, geometry, name=None, color=None):
"""Draw a **COMPAS** geometry in the respective CAD environment.
Note
Expand Down Expand Up @@ -60,7 +60,7 @@ class BaseRobotModelArtist(AbstractRobotModelArtist):
There are two methods that implementers of this base class should provide, one
is concerned with the actual creation of geometry in the native format of the
CAD environment (:meth:`draw_geometry`) and the other is one to apply a transformation
CAD environment (:meth:`create_geometry`) and the other is one to apply a transformation
to geometry (:meth:`transform`).
Attributes
Expand Down Expand Up @@ -117,7 +117,7 @@ def detach_tool_model(self):
def create(self, link=None, context=None):
"""Recursive function that triggers the drawing of the robot model's geometry.
This method delegates the geometry drawing to the :meth:`draw_geometry`
This method delegates the geometry drawing to the :meth:`create_geometry`
method. It transforms the geometry based on the saved initial
transformation from the robot model.
Expand Down Expand Up @@ -150,7 +150,7 @@ def create(self, link=None, context=None):
else:
mesh_name_components = [self.model.name, mesh_type, context, link.name, str(i)]
mesh_name = '.'.join(mesh_name_components)
native_mesh = self.draw_geometry(mesh, name=mesh_name, color=color)
native_mesh = self.create_geoemetry(mesh, name=mesh_name, color=color)

self.transform(native_mesh, item.init_transformation)

Expand Down Expand Up @@ -288,22 +288,22 @@ def update_tool(self, joint_state=None, visual=True, collision=True, transformat

def draw_visual(self):
"""Draws all visual geometry of the robot model."""
for native_geometry in self._draw_model_geometry(self.model, 'visual'):
for native_geometry in self._iter_geometry(self.model, 'visual'):
yield native_geometry
if self.attached_tool_model:
for native_geometry in self._draw_model_geometry(self.attached_tool_model, 'visual'):
for native_geometry in self._iter_geometry(self.attached_tool_model, 'visual'):
yield native_geometry

def draw_collision(self):
"""Draws all collision geometry of the robot model."""
for native_geometry in self._draw_model_geometry(self.model, 'collision'):
for native_geometry in self._iter_geometry(self.model, 'collision'):
yield native_geometry
if self.attached_tool_model:
for native_geometry in self._draw_model_geometry(self.attached_tool_model, 'collision'):
for native_geometry in self._iter_geometry(self.attached_tool_model, 'collision'):
yield native_geometry

@staticmethod
def _draw_model_geometry(model, geometry_type):
def _iter_geometry(model, geometry_type):
for link in model.iter_links():
for item in getattr(link, geometry_type):
if item.native_geometry:
Expand Down
39 changes: 23 additions & 16 deletions src/compas_blender/artists/robotmodelartist.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,44 @@ class RobotModelArtist(BaseRobotModelArtist):
----------
model : :class:`compas.robots.RobotModel`
Robot model.
layer : str, optional
The name of the layer that will contain the robot meshes.
"""

def __init__(self, model, collection=None, layer=None):
self.view_layer = layer
def __init__(self, model, collection=None):
self.collection = collection
super(RobotModelArtist, self).__init__(model)

def transform(self, native_mesh, transformation):
native_mesh.matrix_world @= mathutils.Matrix(transformation.matrix)
native_mesh.matrix_world = mathutils.Matrix(transformation.matrix) @ native_mesh.matrix_world

def draw_geometry(self, geometry, name=None, color=None):
def create_geoemetry(self, geometry, name=None, color=None):
# Imported colors take priority over a the parameter color

if 'mesh_color.diffuse' in geometry.attributes:
color = geometry.attributes['mesh_color.diffuse']

# If we have a color, we'll discard alpha because draw_mesh is hard coded for a=1
if color:
r, g, b, _a = color
color = [r, g, b]
color = (r, g, b)
else:
color = [1., 1., 1.]
color = (1., 1., 1.)

if self.collection:
if self.collection not in bpy.data.collections.keys():
compas_blender.utilities.create_collection(self.collection)
if self.collection and self.collection not in bpy.data.collections.keys():
compas_blender.utilities.create_collection(self.collection)

v, f = geometry.to_vertices_and_faces()
return compas_blender.draw_mesh(vertices=v, faces=f, name=name, color=color, centroid=False, collection=self.collection)

def redraw(self, timeout=None):
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
native_mesh = compas_blender.draw_mesh(vertices=v, faces=f, name=name, color=color, centroid=False, collection=self.collection)
native_mesh.hide_set(True)
return native_mesh

def redraw(self, timeout=0.0):
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1, time_limit=timeout)

def draw_visual(self):
visuals = super(RobotModelArtist, self).draw_visual()
for visual in visuals:
visual.hide_set(False)

def draw_collision(self):
collisions = super(RobotModelArtist, self).draw_collision()
for collision in collisions:
collision.hide_set(False)
2 changes: 1 addition & 1 deletion src/compas_ghpython/artists/robotmodelartist.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self, model):
def transform(self, native_mesh, transformation):
xtransform(native_mesh, transformation)

def draw_geometry(self, geometry, name=None, color=None):
def create_geoemetry(self, geometry, name=None, color=None):
if color:
color = rgb_to_rgb(color[0], color[1], color[2])
vertices, faces = geometry.to_vertices_and_faces()
Expand Down
2 changes: 1 addition & 1 deletion src/compas_rhino/artists/robotmodelartist.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def transform(self, native_mesh, transformation):
T = xform_from_transformation(transformation)
native_mesh.Transform(T)

def draw_geometry(self, geometry, name=None, color=None):
def create_geoemetry(self, geometry, name=None, color=None):
# Imported colors take priority over a the parameter color
if 'mesh_color.diffuse' in geometry.attributes:
color = geometry.attributes['mesh_color.diffuse']
Expand Down

0 comments on commit bdf7b4d

Please sign in to comment.