Skip to content

Commit

Permalink
Merge pull request #1367 from compas-dev/fix-scene-consistency-and-se…
Browse files Browse the repository at this point in the history
…rialisation

Fix Scene Object implementation consistency and Scene serialisation
  • Loading branch information
tomvanmele authored Jun 7, 2024
2 parents 8d40e38 + d4ad97e commit 10a8ee1
Show file tree
Hide file tree
Showing 38 changed files with 1,051 additions and 2,212 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added implementation of `compute_vertices`, `compute_edges`, `compute_faces` to `compas.geometry.Cylinder`.
* Added implementation of `compute_vertices`, `compute_edges`, `compute_faces` to `compas.geometry.Sphere`.
* Added implementation of `compute_vertices`, `compute_edges`, `compute_faces` to `compas.geometry.Torus`.
* Added `compas_blender.scene.ShapeObject`.
* Added `compas.geometry.vector.__radd__`.
* Added `compas.geometry.vector.__rsub__`.
* Added `compas.geometry.vector.__rmul__`.
Expand All @@ -32,12 +33,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Changed check for empty vertices and faces to use `is None` to add support for `numpy` arrays.
* Changed order of `u` and `v` of `compas.geometry.SphericalSurface` to the match the excpected parametrisation.
* Changed `compas.geometry.Shape.to_vertices_and_faces` to use `Shape.vertices` and `Shape.faces` or `Shape.triangles`.
* Changed default of `compas.scene.descriptors.color.ColorAttribute` to `None` to support native coloring in CAD contexts.
* Changed `compas.colors.ColorDict.__data__` and `compas.colors.ColorDict.__from_data__` to properly support serialisation.
* Moved `compas_blender.utilities.drawing` to `compas_blender.drawing` with backward compatible imports and deprecation warning.
* Moved `compas_ghpython.utilities.drawing` to `compas_ghpython.drawing` with backward compatible imports and deprecation warning.
* Moved `compas_rhino.utilities.drawing` to `compas_rhino.drawing` with backward compatible imports and deprecation warning.
* Changed `draw_nodes` and `draw_edges` of `compas_blender.scene.GraphObject`, `compas_ghpython.scene.GraphObject`, and `compas_rhino.scene.GraphObject` to use only attributes instead of parameters.
* Changed `draw_vertices`, `draw_edges` and `draw_faces` of `compas_blender.scene.MeshObject`, `compas_ghpython.scene.MeshObject`, and `compas_rhino.scene.MeshObject` to use only attributes instead of parameters.
* Changed `draw_vertices`, `draw_edges` and `draw_faces` of `compas_blender.scene.VolMeshObject`, `compas_ghpython.scene.VolMeshObject`, and `compas_rhino.scene.VolMeshObject` to use only attributes instead of parameters.
* Changed registration of `Capsule`, `Cone`, `Cylinder`, `Sphere`, `Torus` to `ShapeObject` in `compas_blender.scene`.
* Updated `compas.geometry.vector.__mul__` to allow element-wise multiplication with another vector.
* Updated `compas.geometry.vector.__truediv__` to allow element-wise division with another vector.

### Removed

* Removed `System`dependency in `compas_ghpython/utilities/drawing.py`.
* Removed `System` dependency in `compas_ghpython/utilities/drawing.py`.
* Removed GH plugin for `compas.scene.clear` since it clashed with the Rhino version.

## [2.1.1] 2024-05-14

Expand Down
10 changes: 9 additions & 1 deletion src/compas/colors/colordict.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ class ColorDict(Data):

@property
def __data__(self):
return {"default": self.default.__data__, "dict": self._dict}
return {"default": self.default, "dict": self._dict}

@classmethod
def __from_data__(cls, data):
colordict = cls(data["default"])
# note: this is specific to color dicts for vertices of meshes
# perhaps the color dict needs to be subclassed per scene object type
colordict.update({int(key): value for key, value in data["dict"].items()})
return colordict

def __init__(self, default, name=None):
super(ColorDict, self).__init__(name=name)
Expand Down
3 changes: 0 additions & 3 deletions src/compas/scene/assemblyobject.py

This file was deleted.

1 change: 0 additions & 1 deletion src/compas/scene/descriptors/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class ColorAttribute(object):

def __init__(self, default=None, **kwargs):
super(ColorAttribute, self).__init__(**kwargs)
default = default or Color.black()
self.default = Color.coerce(default)

def __set_name__(self, owner, name):
Expand Down
6 changes: 2 additions & 4 deletions src/compas/scene/geometryobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,5 @@ def __init__(self, geometry, **kwargs):
self.show_points = kwargs.get("show_points", False)
self.show_lines = kwargs.get("show_lines", True)
self.show_surfaces = kwargs.get("show_surfaces", True)

def draw(self):
"""Draw the geometry. Implemented by child classes."""
raise NotImplementedError
# note: either lines should be renamed to curves
# or surfaces should be renamed to faces?
84 changes: 44 additions & 40 deletions src/compas/scene/graphobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from __future__ import division
from __future__ import print_function

import compas.colors # noqa: F401
import compas.datastructures # noqa: F401
import compas.geometry # noqa: F401
from compas.geometry import transform_points

from .descriptors.colordict import ColorDictAttribute
Expand Down Expand Up @@ -31,7 +34,7 @@ class GraphObject(SceneObject):
The size of the nodes. Default is ``1.0``.
edgewidth : float
The width of the edges. Default is ``1.0``.
show_nodes : Union[bool, sequence[float]]
show_nodes : Union[bool, sequence[hashable]]
Flag for showing or hiding the nodes. Default is ``True``.
show_edges : Union[bool, sequence[tuple[hashable, hashable]]]
Flag for showing or hiding the edges. Default is ``True``.
Expand All @@ -46,64 +49,76 @@ class GraphObject(SceneObject):
nodecolor = ColorDictAttribute()
edgecolor = ColorDictAttribute()

def __init__(self, graph, **kwargs):
def __init__(self, graph, show_nodes=True, show_edges=True, nodecolor=None, edgecolor=None, nodesize=1.0, edgewidth=1.0, **kwargs):
# type: (compas.datastructures.Graph, bool | list, bool | list, compas.colors.Color | dict | None, compas.colors.Color | dict | None, float | dict, float | dict, dict) -> None
super(GraphObject, self).__init__(item=graph, **kwargs)
self._graph = None
self._node_xyz = None
self.graph = graph
self.nodecolor = kwargs.get("nodecolor", self.color)
self.edgecolor = kwargs.get("edgecolor", self.color)
self.nodesize = kwargs.get("nodesize", 1.0)
self.edgewidth = kwargs.get("edgewidth", 1.0)
self.show_nodes = kwargs.get("show_nodes", True)
self.show_edges = kwargs.get("show_edges", True)
self.show_nodes = show_nodes
self.show_edges = show_edges
self.nodecolor = nodecolor or self.color
self.edgecolor = edgecolor or self.color
self.nodesize = nodesize
self.edgewidth = edgewidth

@property
def settings(self):
# type: () -> dict
settings = super(GraphObject, self).settings
settings["show_nodes"] = self.show_nodes
settings["show_edges"] = self.show_edges
settings["nodecolor"] = self.nodecolor
settings["edgecolor"] = self.edgecolor
settings["nodesize"] = self.nodesize
settings["edgewidth"] = self.edgewidth
return settings

@property
def graph(self):
# type: () -> compas.datastructures.Graph
return self._graph

@graph.setter
def graph(self, graph):
# type: (compas.datastructures.Graph) -> None
self._graph = graph
self._transformation = None
self._node_xyz = None

@property
def transformation(self):
# type: () -> compas.geometry.Transformation | None
return self._transformation

@transformation.setter
def transformation(self, transformation):
# type: (compas.geometry.Transformation) -> None
self._node_xyz = None
self._transformation = transformation

@property
def node_xyz(self):
# type: () -> dict[int | str | tuple, list[float]]
if self._node_xyz is None:
points = self.graph.nodes_attributes("xyz") # type: ignore
points = self.graph.nodes_attributes("xyz")
points = transform_points(points, self.worldtransformation)
self._node_xyz = dict(zip(self.graph.nodes(), points)) # type: ignore
self._node_xyz = dict(zip(self.graph.nodes(), points))
return self._node_xyz

@node_xyz.setter
def node_xyz(self, node_xyz):
# type: (dict[int | str | tuple, list[float]]) -> None
self._node_xyz = node_xyz

def draw_nodes(self, nodes=None, color=None, text=None):
def draw_nodes(self):
"""Draw the nodes of the graph.
Parameters
----------
nodes : list[int], optional
The nodes to include in the drawing.
Default is all nodes.
color : tuple[float, float, float] | :class:`compas.colors.Color` | dict[int, tuple[float, float, float] | :class:`compas.colors.Color`], optional
The color of the nodes,
as either a single color to be applied to all nodes,
or a color dict, mapping specific nodes to specific colors.
text : dict[int, str], optional
The text labels for the nodes
as a text dict, mapping specific nodes to specific text labels.
Nodes are drawn based on the values of
* `self.show_nodes`
* `self.nodecolor`
* `self.nodesize`
Returns
-------
Expand All @@ -113,21 +128,14 @@ def draw_nodes(self, nodes=None, color=None, text=None):
"""
raise NotImplementedError

def draw_edges(self, edges=None, color=None, text=None):
def draw_edges(self):
"""Draw the edges of the graph.
Parameters
----------
edges : list[tuple[int, int]], optional
The edges to include in the drawing.
Default is all edges.
color : tuple[float, float, float] | :class:`compas.colors.Color` | dict[tuple[int, int], tuple[float, float, float] | :class:`compas.colors.Color`], optional
The color of the edges,
as either a single color to be applied to all edges,
or a color dict, mapping specific edges to specific colors.
text : dict[tuple[int, int]], optional
The text labels for the edges
as a text dict, mapping specific edges to specific text labels.
Edges are drawn based on the values of
* `self.show_edges`
* `self.edgecolor`
* `self.edgewidth`
Returns
-------
Expand All @@ -137,10 +145,6 @@ def draw_edges(self, edges=None, color=None, text=None):
"""
raise NotImplementedError

def draw(self):
"""Draw the network."""
raise NotImplementedError

def clear_nodes(self):
"""Clear the nodes of the graph.
Expand Down
Loading

0 comments on commit 10a8ee1

Please sign in to comment.