Skip to content

Commit 0c866d1

Browse files
committed
clean up the function
1 parent b730a4d commit 0c866d1

1 file changed

Lines changed: 40 additions & 19 deletions

File tree

src/compas/scene/context.py

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,36 +119,57 @@ def detect_current_context():
119119
return None
120120

121121

122-
def _get_sceneobject_cls(data, **kwargs):
123-
# in any case user gets to override the choice
124-
context_name = kwargs.get("context") or detect_current_context()
122+
def get_sceneobject_cls(item, context=None, **kwargs):
123+
"""Get the scene object class for a given item in the current context. If no context is provided, the current context is detected.
124+
If the exact item type is not registered, a closest match in its inheritance hierarchy is used.
125125
126-
dtype = type(data)
126+
Parameters
127+
----------
128+
item : :class:`~compas.data.Data`
129+
The item to get the scene object class for.
130+
context : Literal['Viewer', 'Rhino', 'Grasshopper', 'Blender'], optional
131+
The visualization context in which the pair should be registered.
132+
**kwargs : dict
133+
Additional keyword arguments.
134+
135+
Raises
136+
------
137+
ValueError
138+
If the item is None.
139+
SceneObjectNotRegisteredError
140+
If no scene object is registered for the item type in the current context.
141+
142+
Returns
143+
-------
144+
:class:`~compas.scene.SceneObject`
145+
The scene object class for the given item.
146+
147+
"""
148+
149+
if item is None:
150+
raise ValueError("Cannot create a scene object for None. Please ensure you pass a instance of a supported class.")
151+
152+
if not ITEM_SCENEOBJECT:
153+
register_scene_objects()
154+
155+
if context is None:
156+
context = detect_current_context()
157+
158+
itemtype = type(item)
127159
cls = None
128160

129161
if "sceneobject_type" in kwargs:
130162
cls = kwargs["sceneobject_type"]
131163
else:
132-
context = ITEM_SCENEOBJECT[context_name]
164+
context = ITEM_SCENEOBJECT[context]
133165

134-
for type_ in inspect.getmro(dtype):
135-
cls = context.get(type_, None)
166+
for inheritancetype in inspect.getmro(itemtype):
167+
cls = context.get(inheritancetype, None)
136168
if cls is not None:
137169
break
138170

139171
if cls is None:
140-
raise SceneObjectNotRegisteredError("No scene object is registered for this data type: {} in this context: {}".format(dtype, context_name))
141-
142-
return cls
143-
144-
145-
def get_sceneobject_cls(item, **kwargs):
146-
if not ITEM_SCENEOBJECT:
147-
register_scene_objects()
148-
149-
if item is None:
150-
raise ValueError("Cannot create a scene object for None. Please ensure you pass a instance of a supported class.")
172+
raise SceneObjectNotRegisteredError("No scene object is registered for this data type: {} in this context: {}".format(itemtype, context))
151173

152-
cls = _get_sceneobject_cls(item, **kwargs)
153174
PluginValidator.ensure_implementations(cls)
154175
return cls

0 commit comments

Comments
 (0)