Skip to content

Commit 0ceee4c

Browse files
authored
Merge pull request #1250 from compas-dev/sceneobjects
Scene Objects for None Context
2 parents 18c1b46 + 9af222b commit 0ceee4c

File tree

11 files changed

+56
-38
lines changed

11 files changed

+56
-38
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9494
* Fixed typo in name `Rhino.Geometry.MeshingParameters` in `compas_rhino.geometry.RhinoBrep.to_meshes()`.
9595
* Fixed `TypeErrorException` when serializing a `Mesh` which has been converted from Rhino.
9696
* Fixed color conversions in `compas_rhion.conversions.mesh_to_compas`.
97+
* Changed `SceneObject` registration to allow for `None` context.
9798
* Changed `compas.data.Data.name` to be stored in `compas.data.Data.attributes`.
9899
* Changed `compas.data.Data.__jsondump__` to include `compas.data.Data.attributes` if the dict is not empty.
99100
* Changed `compas.data.Data.__jsonload__` to update `compas.data.Data.attributes` if the attribute dict is provided.
@@ -114,6 +115,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
114115
### Removed
115116

116117
* Removed `compas_rhino.forms`. Forms will be moved to `compas_ui`.
118+
* Removed `compas.scene.NoSceneObjectContextError`.
117119
* Removed `compas.datastructures.Datastructure.attributes` and `compas.datastructures.Datastructure.name` (moved to `compas.data.Data`).
118120
* Removed `attributes` from `compas.datastructures.Graph.data`.
119121
* Removed `attributes` from `compas.datastructures.Network.data`.

src/compas/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135

136136
__all_plugins__ = [
137137
"compas.geometry.booleans.booleans_shapely",
138+
"compas.scene",
138139
]
139140

140141

src/compas/scene/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from __future__ import division
1010

1111
from .exceptions import SceneObjectNotRegisteredError
12-
from .exceptions import NoSceneObjectContextError
1312
from .sceneobject import SceneObject
1413
from .meshobject import MeshObject
1514
from .graphobject import GraphObject
@@ -26,9 +25,23 @@
2625
from .scene import SceneObjectNode
2726
from .scene import SceneTree
2827

28+
from compas.plugins import plugin
29+
from compas.geometry import Geometry
30+
from compas.datastructures import Mesh
31+
from compas.datastructures import Graph
32+
from compas.datastructures import VolMesh
33+
34+
35+
@plugin(category="factories", pluggable_name="register_scene_objects")
36+
def register_scene_objects_base():
37+
register(Geometry, GeometryObject, context=None)
38+
register(Mesh, MeshObject, context=None)
39+
register(Graph, GraphObject, context=None)
40+
register(VolMesh, VolMeshObject, context=None)
41+
42+
2943
__all__ = [
3044
"SceneObjectNotRegisteredError",
31-
"NoSceneObjectContextError",
3245
"SceneObject",
3346
"MeshObject",
3447
"GraphObject",

src/compas/scene/context.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from compas.plugins import PluginValidator
66
from compas.plugins import pluggable
77

8-
from .exceptions import NoSceneObjectContextError
98
from .exceptions import SceneObjectNotRegisteredError
109

1110
ITEM_SCENEOBJECT = defaultdict(dict)
@@ -69,7 +68,7 @@ def is_viewer_open():
6968
return Scene.viewerinstance is not None
7069

7170

72-
def _detect_current_context():
71+
def detect_current_context():
7372
"""Chooses an appropriate context depending on available contexts and open instances. with the following priority:
7473
1. Viewer
7574
2. Plotter
@@ -82,6 +81,10 @@ def _detect_current_context():
8281
Name of an available context, used as key in :attr:`SceneObject.ITEM_SCENEOBJECT`
8382
8483
"""
84+
85+
if not ITEM_SCENEOBJECT:
86+
register_scene_objects()
87+
8588
if is_viewer_open():
8689
return "Viewer"
8790
if compas.is_grasshopper():
@@ -93,12 +96,13 @@ def _detect_current_context():
9396
other_contexts = [v for v in ITEM_SCENEOBJECT.keys()]
9497
if other_contexts:
9598
return other_contexts[0]
96-
raise NoSceneObjectContextError()
99+
100+
return None
97101

98102

99103
def _get_sceneobject_cls(data, **kwargs):
100104
# in any case user gets to override the choice
101-
context_name = kwargs.get("context") or _detect_current_context()
105+
context_name = kwargs.get("context") or detect_current_context()
102106

103107
dtype = type(data)
104108
cls = None
@@ -122,9 +126,6 @@ def _get_sceneobject_cls(data, **kwargs):
122126

123127

124128
def get_sceneobject_cls(item, **kwargs):
125-
if not ITEM_SCENEOBJECT:
126-
register_scene_objects()
127-
128129
if item is None:
129130
raise ValueError(
130131
"Cannot create a scene object for None. Please ensure you pass a instance of a supported class."

src/compas/scene/exceptions.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,3 @@
55

66
class SceneObjectNotRegisteredError(Exception):
77
"""Exception that is raised when no scene object is registered for a given data type."""
8-
9-
10-
class NoSceneObjectContextError(Exception):
11-
"""Exception that is raised when no scene object context is assigned is registered for a given data type."""
12-
13-
def __init__(self):
14-
error_message = "No context defined."
15-
error_message += "\n\nThis usually means that the script that you are running requires"
16-
error_message += "\na CAD environment but it is being ran as a standalone script"
17-
error_message += "\n(ie. from the command line or code editor)."
18-
super(NoSceneObjectContextError, self).__init__(error_message)

src/compas/scene/geometryobject.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,7 @@ def __init__(self, geometry, **kwargs):
5252
self.show_points = kwargs.get("show_points", False)
5353
self.show_lines = kwargs.get("show_lines", True)
5454
self.show_surfaces = kwargs.get("show_surfaces", True)
55+
56+
def draw(self):
57+
"""Draw the geometry. Implemented by child classes."""
58+
raise NotImplementedError

src/compas/scene/graphobject.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
from __future__ import absolute_import
33
from __future__ import division
44

5-
from abc import abstractmethod
6-
75
from compas.geometry import transform_points
86
from .sceneobject import SceneObject
97
from .descriptors.colordict import ColorDictAttribute
@@ -90,7 +88,6 @@ def node_xyz(self):
9088
def node_xyz(self, node_xyz):
9189
self._node_xyz = node_xyz
9290

93-
@abstractmethod
9491
def draw_nodes(self, nodes=None, color=None, text=None):
9592
"""Draw the nodes of the graph.
9693
@@ -115,7 +112,6 @@ def draw_nodes(self, nodes=None, color=None, text=None):
115112
"""
116113
raise NotImplementedError
117114

118-
@abstractmethod
119115
def draw_edges(self, edges=None, color=None, text=None):
120116
"""Draw the edges of the graph.
121117
@@ -140,6 +136,10 @@ def draw_edges(self, edges=None, color=None, text=None):
140136
"""
141137
raise NotImplementedError
142138

139+
def draw(self):
140+
"""Draw the network."""
141+
raise NotImplementedError
142+
143143
def clear_nodes(self):
144144
"""Clear the nodes of the graph.
145145

src/compas/scene/meshobject.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
from __future__ import absolute_import
33
from __future__ import division
44

5-
from abc import abstractmethod
6-
75
from compas.geometry import transform_points
86
from .sceneobject import SceneObject
97
from .descriptors.colordict import ColorDictAttribute
@@ -99,7 +97,6 @@ def vertex_xyz(self):
9997
def vertex_xyz(self, vertex_xyz):
10098
self._vertex_xyz = vertex_xyz
10199

102-
@abstractmethod
103100
def draw_vertices(self, vertices=None, color=None, text=None):
104101
"""Draw the vertices of the mesh.
105102
@@ -124,7 +121,6 @@ def draw_vertices(self, vertices=None, color=None, text=None):
124121
"""
125122
raise NotImplementedError
126123

127-
@abstractmethod
128124
def draw_edges(self, edges=None, color=None, text=None):
129125
"""Draw the edges of the mesh.
130126
@@ -149,7 +145,6 @@ def draw_edges(self, edges=None, color=None, text=None):
149145
"""
150146
raise NotImplementedError
151147

152-
@abstractmethod
153148
def draw_faces(self, faces=None, color=None, text=None):
154149
"""Draw the faces of the mesh.
155150
@@ -188,6 +183,16 @@ def draw_mesh(self, *args, **kwargs):
188183
"""
189184
return self.draw(*args, **kwargs)
190185

186+
def draw(self):
187+
"""draw the mesh.
188+
189+
Returns
190+
-------
191+
None
192+
193+
"""
194+
raise NotImplementedError
195+
191196
def clear(self):
192197
"""Clear all components of the mesh.
193198

src/compas/scene/scene.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from .context import clear
66
from .context import redraw
7+
from .context import detect_current_context
78
from .sceneobject import SceneObject
89

910

@@ -207,7 +208,7 @@ class Scene(Data):
207208
def __init__(self, name=None, context=None):
208209
super(Scene, self).__init__(name)
209210
self._tree = SceneTree("Scene")
210-
self.context = context
211+
self.context = context or detect_current_context()
211212

212213
@property
213214
def data(self):
@@ -288,6 +289,10 @@ def clear_objects(self):
288289

289290
def redraw(self):
290291
"""Redraw the scene."""
292+
293+
if not self.context:
294+
raise ValueError("No context detected.")
295+
291296
self.clear_objects()
292297

293298
drawn_objects = []

src/compas/scene/volmeshobject.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
from __future__ import absolute_import
33
from __future__ import division
44

5-
from abc import abstractmethod
6-
75
from compas.geometry import transform_points
86
from .sceneobject import SceneObject
97
from .descriptors.colordict import ColorDictAttribute
@@ -108,7 +106,6 @@ def vertex_xyz(self):
108106
def vertex_xyz(self, vertex_xyz):
109107
self._vertex_xyz = vertex_xyz
110108

111-
@abstractmethod
112109
def draw_vertices(self, vertices=None, color=None, text=None):
113110
"""Draw the vertices of the mesh.
114111
@@ -133,7 +130,6 @@ def draw_vertices(self, vertices=None, color=None, text=None):
133130
"""
134131
raise NotImplementedError
135132

136-
@abstractmethod
137133
def draw_edges(self, edges=None, color=None, text=None):
138134
"""Draw the edges of the mesh.
139135
@@ -158,7 +154,6 @@ def draw_edges(self, edges=None, color=None, text=None):
158154
"""
159155
raise NotImplementedError
160156

161-
@abstractmethod
162157
def draw_faces(self, faces=None, color=None, text=None):
163158
"""Draw the faces of the mesh.
164159
@@ -183,7 +178,6 @@ def draw_faces(self, faces=None, color=None, text=None):
183178
"""
184179
raise NotImplementedError
185180

186-
@abstractmethod
187181
def draw_cells(self, cells=None, color=None, text=None):
188182
"""Draw the cells of the mesh.
189183
@@ -208,6 +202,10 @@ def draw_cells(self, cells=None, color=None, text=None):
208202
"""
209203
raise NotImplementedError
210204

205+
def draw(self):
206+
"""Draw the volmesh."""
207+
raise NotImplementedError
208+
211209
def clear_vertices(self):
212210
"""Clear the vertices of the mesh.
213211

0 commit comments

Comments
 (0)