File size: 4,530 Bytes
cf8614b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import unittest
import warnings
import tempfile
from tests.core import TestCore
from tests.core import ASSET_DIR
from pyrep.objects.object import Object
from pyrep.objects.shape import Shape
from pyrep.objects.dummy import Dummy
from pyrep.objects.joint import Joint
from pyrep.objects.proximity_sensor import ProximitySensor
from pyrep.objects.force_sensor import ForceSensor
from pyrep.objects.cartesian_path import CartesianPath
from pyrep.errors import WrongObjectTypeError
import os
from os import path
import numpy as np


class TestPyrep(TestCore):

    def test_get_object_wrong_type(self):
        with self.assertRaises(WrongObjectTypeError):
            ProximitySensor('dynamic_cube')

    def test_get_shape(self):
        cube = Shape('dynamic_cube')
        self.assertIsInstance(cube, Shape)

    def test_get_joint(self):
        cube = Joint('prismatic_joint')
        self.assertIsInstance(cube, Joint)

    def test_get_proximity_sensor(self):
        cube = ProximitySensor('proximity_sensor')
        self.assertIsInstance(cube, ProximitySensor)

    def test_get_force_sensor(self):
        cube = ForceSensor('force_sensor')
        self.assertIsInstance(cube, ForceSensor)

    def test_get_cartesian_path(self):
        cube = CartesianPath('cartesian_path')
        self.assertIsInstance(cube, CartesianPath)

    def test_step(self):
        cube = Shape('dynamic_cube')
        start_pos = cube.get_position()
        [self.pyrep.step() for _ in range(2)]
        end_pos = cube.get_position()
        self.assertFalse(np.allclose(start_pos, end_pos))

    def test_load_model(self):
        m = self.pyrep.import_model(path.join(ASSET_DIR, 'loadable_model.ttm'))
        self.assertIsInstance(m, Shape)

    def test_export_scene(self):
        scene_file = tempfile.mktemp('.ttt')
        self.pyrep.export_scene(scene_file)
        os.remove(scene_file)

    def test_group_objects(self):
        top = Dummy('cubes_under_dummy')
        self.assertEqual(
            len(top.get_objects_in_tree(exclude_base=True)), 3)
        cubes = [Shape('cube%d' % i) for i in range(3)]
        ob = self.pyrep.group_objects(cubes)
        self.assertIsInstance(ob, Object)
        self.assertEqual(
            len(top.get_objects_in_tree(exclude_base=True)), 1)

    def test_merge_objects(self):
        top = Dummy('cubes_under_dummy')
        self.assertEqual(
            len(top.get_objects_in_tree(exclude_base=True)), 3)
        cubes = [Shape('cube%d' % i) for i in range(3)]
        ob = self.pyrep.merge_objects(cubes)
        self.assertIsInstance(ob, Object)
        self.assertEqual(
            len(top.get_objects_in_tree(exclude_base=True)), 1)

    def test_set_configuration_tree(self):
        dynamic_cube = Shape('dynamic_cube')
        pos = dynamic_cube.get_position()
        config = dynamic_cube.get_configuration_tree()
        self.assertIsNotNone(config)
        [self.pyrep.step() for _ in range(10)]
        self.pyrep.set_configuration_tree(config)
        self.assertTrue(np.allclose(pos, dynamic_cube.get_position()))

    def test_create_texture_and_get_texture(self):
        plane, texture = self.pyrep.create_texture(
            path.join(ASSET_DIR, 'wood_texture.jpg'))
        self.assertGreaterEqual(texture.get_texture_id(), 0)
        self.assertEqual(texture.get_texture_id(),
                         plane.get_texture().get_texture_id())

    def test_get_objects_in_tree(self):
        objects = self.pyrep.get_objects_in_tree()
        for obj in objects:
            self.assertIsInstance(obj, Object)

        dummys = [Dummy('nested_dummy%d' % i) for i in range(3)]
        for root_obj in [dummys[0], dummys[0].get_handle()]:
            objects = self.pyrep.get_objects_in_tree(
                root_obj, exclude_base=False, first_generation_only=False)
            self.assertListEqual(objects, dummys)
            for obj in objects:
                self.assertIs(type(obj), Dummy)

            self.assertListEqual(
                self.pyrep.get_objects_in_tree(
                    root_obj, exclude_base=True, first_generation_only=False),
                    dummys[1:])
            self.assertListEqual(
                self.pyrep.get_objects_in_tree(
                    root_obj, exclude_base=False,first_generation_only=True),
                    dummys[:-1])

    def test_get_collection_by_name(self):
        self.assertIsInstance(self.pyrep.get_collection_handle_by_name('Panda_arm'), int)

if __name__ == '__main__':
    unittest.main()