Skip to content

Commit d75f468

Browse files
committed
correct typing of scene objects
1 parent 9856318 commit d75f468

File tree

5 files changed

+81
-44
lines changed

5 files changed

+81
-44
lines changed

src/compas/scene/graphobject.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,18 @@ class GraphObject(SceneObject):
4949
nodecolor = ColorDictAttribute()
5050
edgecolor = ColorDictAttribute()
5151

52-
def __init__(self, show_nodes=True, show_edges=True, nodecolor=None, edgecolor=None, nodesize=1.0, edgewidth=1.0, **kwargs):
53-
# type: (bool | list, bool | list, dict | compas.colors.Color | None, dict | compas.colors.Color | None, float, float, dict) -> None
54-
super(GraphObject, self).__init__(**kwargs)
52+
def __init__(
53+
self,
54+
show_nodes=True, # type: bool | list
55+
show_edges=True, # type: bool | list
56+
nodecolor=None, # type: dict | compas.colors.Color | None
57+
edgecolor=None, # type: dict | compas.colors.Color | None
58+
nodesize=1.0, # type: float
59+
edgewidth=1.0, # type: float
60+
**kwargs # type: dict
61+
): # fmt: skip
62+
# type: (...) -> None
63+
super(GraphObject, self).__init__(**kwargs) # type: ignore
5564
self._node_xyz = None
5665
self.show_nodes = show_nodes
5766
self.show_edges = show_edges
@@ -75,7 +84,7 @@ def settings(self):
7584
@property
7685
def graph(self):
7786
# type: () -> compas.datastructures.Graph
78-
return self.item
87+
return self.item # type: ignore
7988

8089
@graph.setter
8190
def graph(self, graph):
@@ -97,16 +106,17 @@ def transformation(self, transformation):
97106

98107
@property
99108
def node_xyz(self):
100-
# type: () -> dict[int | str | tuple, list[float]]
101-
if self._node_xyz is None:
102-
points = self.graph.nodes_attributes("xyz")
103-
points = transform_points(points, self.worldtransformation)
104-
self._node_xyz = dict(zip(self.graph.nodes(), points))
105-
return self._node_xyz
109+
# type: () -> dict[int | str, list[float]]
110+
if self.graph:
111+
if self._node_xyz is None:
112+
points = self.graph.nodes_attributes("xyz")
113+
points = transform_points(points, self.worldtransformation)
114+
self._node_xyz = dict(zip(self.graph.nodes(), points))
115+
return self._node_xyz # type: ignore
106116

107117
@node_xyz.setter
108118
def node_xyz(self, node_xyz):
109-
# type: (dict[int | str | tuple, list[float]]) -> None
119+
# type: (dict[int | str, list[float]]) -> None
110120
self._node_xyz = node_xyz
111121

112122
def draw_nodes(self):

src/compas/scene/meshobject.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,20 @@ class MeshObject(SceneObject):
5757
edgecolor = ColorDictAttribute()
5858
facecolor = ColorDictAttribute()
5959

60-
def __init__(self, show_vertices=False, show_edges=False, show_faces=True, vertexcolor=None, edgecolor=None, facecolor=None, vertexsize=1.0, edgewidth=1.0, **kwargs):
61-
# type: (bool | list, bool | list, bool | list, dict | compas.colors.Color | None, dict | compas.colors.Color | None, dict | compas.colors.Color | None, float, float, dict) -> None
62-
super(MeshObject, self).__init__(**kwargs)
60+
def __init__(
61+
self,
62+
show_vertices=False, # type: bool | list
63+
show_edges=False, # type: bool | list
64+
show_faces=True, # type: bool | list
65+
vertexcolor=None, # type: dict | compas.colors.Color | None
66+
edgecolor=None, # type: dict | compas.colors.Color | None
67+
facecolor=None, # type: dict | compas.colors.Color | None
68+
vertexsize=1.0, # type: float
69+
edgewidth=1.0, # type: float
70+
**kwargs # dict
71+
): # fmt: skip
72+
# type: (...) -> None
73+
super(MeshObject, self).__init__(**kwargs) # type: ignore
6374
self._vertex_xyz = None
6475
self.show_vertices = show_vertices
6576
self.show_edges = show_edges
@@ -89,7 +100,7 @@ def settings(self):
89100
@property
90101
def mesh(self):
91102
# type: () -> compas.datastructures.Mesh
92-
return self.item
103+
return self.item # type: ignore
93104

94105
@mesh.setter
95106
def mesh(self, mesh):
@@ -112,11 +123,12 @@ def transformation(self, transformation):
112123
@property
113124
def vertex_xyz(self):
114125
# type: () -> dict[int, list[float]]
115-
if self._vertex_xyz is None:
116-
points = self.mesh.vertices_attributes("xyz")
117-
points = transform_points(points, self.worldtransformation)
118-
self._vertex_xyz = dict(zip(self.mesh.vertices(), points))
119-
return self._vertex_xyz
126+
if self.mesh:
127+
if self._vertex_xyz is None:
128+
points = self.mesh.vertices_attributes("xyz")
129+
points = transform_points(points, self.worldtransformation)
130+
self._vertex_xyz = dict(zip(self.mesh.vertices(), points))
131+
return self._vertex_xyz # type: ignore
120132

121133
@vertex_xyz.setter
122134
def vertex_xyz(self, vertex_xyz):

src/compas/scene/scene.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def add(node, parent, items):
6666
return scene
6767

6868
def __init__(self, name="Scene", context=None):
69-
# type: (str | "Scene", str | None) -> None
69+
# type: (str, str | None) -> None
7070
super(Scene, self).__init__(name=name)
7171
super(Scene, self).add(TreeNode(name="ROOT"))
7272
self.context = context or detect_current_context()
@@ -113,7 +113,6 @@ def add(self, item, parent=None, **kwargs):
113113
def clear(self):
114114
# type: () -> None
115115
"""Clear everything from the current context of the scene."""
116-
clear()
117116

118117
def clear_objects(self):
119118
# type: () -> None

src/compas/scene/sceneobject.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import compas.data # noqa: F401
1010
import compas.datastructures # noqa: F401
1111
import compas.geometry # noqa: F401
12+
import compas.scene # noqa: F401
1213
from compas.colors import Color
1314
from compas.data import Data
1415
from compas.datastructures import TreeNode
@@ -87,9 +88,23 @@ def __new__(cls, item=None, **kwargs):
8788
sceneobject_cls = get_sceneobject_cls(item, **kwargs)
8889
return super(SceneObject, cls).__new__(sceneobject_cls)
8990

90-
def __init__(self, item=None, name=None, color=None, opacity=1.0, show=True, frame=None, transformation=None, context=None, **kwargs): # fmt: skip
91-
# type: (compas.data.Data | None, str | None, compas.colors.Color | None, float, bool, compas.geometry.Frame | None, compas.geometry.Transformation | None, str | None, dict) -> None
92-
name = item.name if isinstance(item, Data) and name is None else name
91+
def __init__(
92+
self,
93+
item=None, # type: compas.data.Data | None
94+
name=None, # type: str | None
95+
color=None, # type: compas.colors.Color | None
96+
opacity=1.0, # type: float
97+
show=True, # type: bool
98+
frame=None, # type: compas.geometry.Frame | None
99+
transformation=None, # type: compas.geometry.Transformation | None
100+
context=None, # type: str | None
101+
**kwargs # type: dict
102+
): # fmt: skip
103+
# type: (...) -> None
104+
if not isinstance(item, Data):
105+
raise ValueError("The item assigned to this scene object should be a data object: {}".format(type(item)))
106+
107+
name = name or item.name
93108
super(SceneObject, self).__init__(name=name, **kwargs)
94109
# the scene object needs to store the context
95110
# because it has no access to the tree and/or the scene before it is added
@@ -125,12 +140,12 @@ def __repr__(self):
125140

126141
@property
127142
def scene(self):
128-
# type: () -> compas.scene.Scene
143+
# type: () -> compas.scene.Scene | None
129144
return self.tree
130145

131146
@property
132147
def item(self):
133-
# type: () -> compas.geometry.Geometry | compas.datastructures.Datastructure
148+
# type: () -> compas.data.Data
134149
return self._item
135150

136151
@property
@@ -180,7 +195,7 @@ def worldtransformation(self):
180195

181196
@property
182197
def contrastcolor(self):
183-
# type: () -> compas.colors.Color
198+
# type: () -> compas.colors.Color | None
184199
if not self._contrastcolor:
185200
if self.color:
186201
if self.color.is_light:

src/compas/scene/volmeshobject.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import division
33
from __future__ import print_function
44

5+
import compas.colors # noqa: F401
56
import compas.datastructures # noqa: F401
67
import compas.geometry # noqa: F401
78
from compas.geometry import transform_points
@@ -68,20 +69,20 @@ class VolMeshObject(SceneObject):
6869

6970
def __init__(
7071
self,
71-
show_vertices=False,
72-
show_edges=True,
73-
show_faces=False,
74-
show_cells=True,
75-
vertexcolor=None,
76-
edgecolor=None,
77-
facecolor=None,
78-
cellcolor=None,
79-
vertexsize=1.0,
80-
edgewidth=1.0,
81-
**kwargs, # fmt: skip
82-
):
83-
# type: (bool | list, bool | list, bool | list, bool | list, compas.colors.Color | dict, compas.colors.Color | dict, compas.colors.Color | dict, compas.colors.Color | dict, float | dict, float | dict, dict) -> None
84-
super(VolMeshObject, self).__init__(**kwargs)
72+
show_vertices=False, # type: bool | list
73+
show_edges=True, # type: bool | list
74+
show_faces=False, # type: bool | list
75+
show_cells=True, # type: bool | list
76+
vertexcolor=None, # type: compas.colors.Color | dict | None
77+
edgecolor=None, # type: compas.colors.Color | dict | None
78+
facecolor=None, # type: compas.colors.Color | dict | None
79+
cellcolor=None, # type: compas.colors.Color | dict | None
80+
vertexsize=1.0, # type: float
81+
edgewidth=1.0, # type: float
82+
**kwargs # type: dict
83+
): # fmt: skip
84+
# type: (...) -> None
85+
super(VolMeshObject, self).__init__(**kwargs) # type: ignore
8586
self._vertex_xyz = None
8687
self.show_vertices = show_vertices
8788
self.show_edges = show_edges
@@ -111,7 +112,7 @@ def settings(self):
111112
@property
112113
def volmesh(self):
113114
# type: () -> compas.datastructures.VolMesh
114-
return self.item
115+
return self.item # type: ignore
115116

116117
@volmesh.setter
117118
def volmesh(self, volmesh):
@@ -138,7 +139,7 @@ def vertex_xyz(self):
138139
points = self.volmesh.vertices_attributes("xyz") # type: ignore
139140
points = transform_points(points, self.worldtransformation)
140141
self._vertex_xyz = dict(zip(self.volmesh.vertices(), points)) # type: ignore
141-
return self._vertex_xyz
142+
return self._vertex_xyz # type: ignore
142143

143144
@vertex_xyz.setter
144145
def vertex_xyz(self, vertex_xyz):

0 commit comments

Comments
 (0)