Skip to content

Commit 758b94c

Browse files
authored
Use defined object class in get_objects_in_tree (#102)
1 parent 52925e4 commit 758b94c

File tree

9 files changed

+40
-10
lines changed

9 files changed

+40
-10
lines changed

pyrep/objects/cartesian_path.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Tuple, List
22
from pyrep.backend import sim
3-
from pyrep.objects.object import Object
3+
from pyrep.objects.object import Object, object_type_to_class
44
from pyrep.const import ObjectType
55

66

@@ -90,3 +90,6 @@ def _script_call(self, func: str, ints=(), floats=(), strings=(), bytes=''):
9090
return sim.simExtCallScriptFunction(
9191
func, sim.sim_scripttype_addonscript,
9292
list(ints), list(floats), list(strings), bytes)
93+
94+
95+
object_type_to_class[ObjectType.PATH] = CartesianPath

pyrep/objects/dummy.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pyrep.objects.object import Object
1+
from pyrep.objects.object import Object, object_type_to_class
22
from pyrep.const import ObjectType
33
from pyrep.backend import sim
44

@@ -21,3 +21,6 @@ def create(size=0.01) -> 'Dummy':
2121

2222
def _get_requested_type(self) -> ObjectType:
2323
return ObjectType.DUMMY
24+
25+
26+
object_type_to_class[ObjectType.DUMMY] = Dummy

pyrep/objects/force_sensor.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Tuple, List
22
from pyrep.backend import sim
3-
from pyrep.objects.object import Object
3+
from pyrep.objects.object import Object, object_type_to_class
44
from pyrep.const import ObjectType
55

66

@@ -20,3 +20,6 @@ def read(self) -> Tuple[List[float], List[float]]:
2020
"""
2121
_, forces, torques = sim.simReadForceSensor(self._handle)
2222
return forces, torques
23+
24+
25+
object_type_to_class[ObjectType.FORCE_SENSOR] = ForceSensor

pyrep/objects/joint.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Tuple, List, Union
22
from pyrep.backend import sim
33
from pyrep.const import JointType, JointMode
4-
from pyrep.objects.object import Object
4+
from pyrep.objects.object import Object, object_type_to_class
55
from pyrep.const import ObjectType
66

77

@@ -246,3 +246,6 @@ def set_joint_mode(self, value: JointMode) -> None:
246246
:param value: The new joint mode value.
247247
"""
248248
sim.simSetJointMode(self._handle, value.value)
249+
250+
251+
object_type_to_class[ObjectType.JOINT] = Joint

pyrep/objects/object.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import numpy as np
77

88

9+
object_type_to_class = {}
10+
11+
912
class Object(object):
1013
"""Base class for V-REP scene objects that are used for building a scene.
1114
@@ -598,7 +601,9 @@ def get_objects_in_tree(self, object_type=ObjectType.ALL, exclude_base=True,
598601
self._handle, object_type.value, options)
599602
objects = []
600603
for h in handles:
601-
objects.append(Object(h))
604+
object_type = ObjectType(sim.simGetObjectType(h))
605+
cls = object_type_to_class.get(object_type, Object)
606+
objects.append(cls(h))
602607
return objects
603608

604609
def copy(self) -> 'Object':

pyrep/objects/proximity_sensor.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from math import sqrt
22

33
from pyrep.backend import sim
4-
from pyrep.objects.object import Object
4+
from pyrep.objects.object import Object, object_type_to_class
55
from pyrep.const import ObjectType
66

77

@@ -36,3 +36,6 @@ def is_detected(self, obj: Object) -> bool:
3636
state, point = sim.simCheckProximitySensor(
3737
self._handle, obj.get_handle())
3838
return state == 1
39+
40+
41+
object_type_to_class[ObjectType.PROXIMITY_SENSOR] = ProximitySensor

pyrep/objects/shape.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import List, Tuple
22
from pyrep.backend import sim
3-
from pyrep.objects.object import Object
3+
from pyrep.objects.object import Object, object_type_to_class
44
from pyrep.const import ObjectType, PrimitiveShape, TextureMappingMode
55
from pyrep.textures.texture import Texture
66
import os
@@ -356,3 +356,6 @@ def ungroup(self) -> List['Shape']:
356356
"""
357357
handles = sim.simUngroupShape(self.get_handle())
358358
return [Shape(handle) for handle in handles]
359+
360+
361+
object_type_to_class[ObjectType.SHAPE] = Shape

pyrep/objects/vision_sensor.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import math
22
from typing import List, Union
33
from pyrep.backend import sim
4-
from pyrep.objects.object import Object
4+
from pyrep.objects.object import Object, object_type_to_class
55
import numpy as np
66
from pyrep.const import ObjectType, PerspectiveMode, RenderMode
77

@@ -265,3 +265,6 @@ def set_far_clipping_plane(self, far_clipping: float) -> None:
265265
sim.simSetObjectFloatParameter(
266266
self._handle, sim.sim_visionfloatparam_far_clipping, far_clipping
267267
)
268+
269+
270+
object_type_to_class[ObjectType.VISION_SENSOR] = VisionSensor

tests/test_objects.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,12 @@ def test_get_bounding_box(self):
142142

143143
def test_get_objects_in_tree(self):
144144
dummys = [Dummy('nested_dummy%d' % i) for i in range(3)]
145-
self.assertListEqual(dummys[0].get_objects_in_tree(
146-
exclude_base=False, first_generation_only=False), dummys)
145+
146+
objects = dummys[0].get_objects_in_tree(
147+
exclude_base=False, first_generation_only=False)
148+
self.assertListEqual(objects, dummys)
149+
for obj in objects:
150+
self.assertIs(type(obj), Dummy)
147151

148152
self.assertListEqual(
149153
dummys[0].get_objects_in_tree(

0 commit comments

Comments
 (0)