From dd3bdf0da1b01870e3426b6b0024d17dd5d0afc0 Mon Sep 17 00:00:00 2001 From: David Rusu Date: Sun, 8 Dec 2019 20:10:12 -0500 Subject: [PATCH] Use pybullet_utils.bullet_client for all our BulletClient needs --- .../gym/pybullet_envs/bullet/bullet_client.py | 29 ---------- .../bullet/minitaur_duck_gym_env.py | 6 +-- .../pybullet_envs/bullet/minitaur_gym_env.py | 6 +-- .../gym/pybullet_envs/bullet/racecarGymEnv.py | 6 +-- .../pybullet_envs/bullet/racecarZEDGymEnv.py | 6 +-- .../minitaur/envs/bullet_client.py | 54 ------------------- .../minitaur/envs/minitaur_gym_env.py | 6 +-- .../prediction/pybullet_sim_gym_env.py | 6 +-- 8 files changed, 18 insertions(+), 101 deletions(-) delete mode 100644 examples/pybullet/gym/pybullet_envs/bullet/bullet_client.py delete mode 100644 examples/pybullet/gym/pybullet_envs/minitaur/envs/bullet_client.py diff --git a/examples/pybullet/gym/pybullet_envs/bullet/bullet_client.py b/examples/pybullet/gym/pybullet_envs/bullet/bullet_client.py deleted file mode 100644 index bd248f1d8..000000000 --- a/examples/pybullet/gym/pybullet_envs/bullet/bullet_client.py +++ /dev/null @@ -1,29 +0,0 @@ -import functools -import inspect -import pybullet - - -class BulletClient(object): - """A wrapper for pybullet to manage different clients.""" - - def __init__(self, connection_mode=pybullet.DIRECT, options=""): - """Create a simulation and connect to it.""" - self._client = pybullet.connect(pybullet.SHARED_MEMORY) - if (self._client < 0): - print("options=", options) - self._client = pybullet.connect(connection_mode, options=options) - self._shapes = {} - - def __del__(self): - """Clean up connection if not already done.""" - try: - pybullet.disconnect(physicsClientId=self._client) - except pybullet.error: - pass - - def __getattr__(self, name): - """Inject the client id into Bullet functions.""" - attribute = getattr(pybullet, name) - if inspect.isbuiltin(attribute): - attribute = functools.partial(attribute, physicsClientId=self._client) - return attribute diff --git a/examples/pybullet/gym/pybullet_envs/bullet/minitaur_duck_gym_env.py b/examples/pybullet/gym/pybullet_envs/bullet/minitaur_duck_gym_env.py index 88d605f11..eefb74693 100644 --- a/examples/pybullet/gym/pybullet_envs/bullet/minitaur_duck_gym_env.py +++ b/examples/pybullet/gym/pybullet_envs/bullet/minitaur_duck_gym_env.py @@ -17,7 +17,7 @@ from gym import spaces from gym.utils import seeding import numpy as np import pybullet -from . import bullet_client +import pybullet_utils.bullet_client as bc from . import minitaur import pybullet_data from . import minitaur_env_randomizer @@ -150,9 +150,9 @@ class MinitaurBulletDuckEnv(gym.Env): self._action_repeat *= NUM_SUBSTEPS if self._is_render: - self._pybullet_client = bullet_client.BulletClient(connection_mode=pybullet.GUI) + self._pybullet_client = bc.BulletClient(connection_mode=pybullet.GUI) else: - self._pybullet_client = bullet_client.BulletClient() + self._pybullet_client = bc.BulletClient() self.seed() self.reset() diff --git a/examples/pybullet/gym/pybullet_envs/bullet/minitaur_gym_env.py b/examples/pybullet/gym/pybullet_envs/bullet/minitaur_gym_env.py index 7a595b47d..ee63044c1 100644 --- a/examples/pybullet/gym/pybullet_envs/bullet/minitaur_gym_env.py +++ b/examples/pybullet/gym/pybullet_envs/bullet/minitaur_gym_env.py @@ -14,7 +14,7 @@ from gym import spaces from gym.utils import seeding import numpy as np import pybullet -from . import bullet_client +import pybullet_utils.bullet_client as bc from . import minitaur import os import pybullet_data @@ -145,9 +145,9 @@ class MinitaurBulletEnv(gym.Env): self._action_repeat *= NUM_SUBSTEPS if self._is_render: - self._pybullet_client = bullet_client.BulletClient(connection_mode=pybullet.GUI) + self._pybullet_client = bc.BulletClient(connection_mode=pybullet.GUI) else: - self._pybullet_client = bullet_client.BulletClient() + self._pybullet_client = bc.BulletClient() self.seed() self.reset() diff --git a/examples/pybullet/gym/pybullet_envs/bullet/racecarGymEnv.py b/examples/pybullet/gym/pybullet_envs/bullet/racecarGymEnv.py index a0118d668..af9d5d545 100644 --- a/examples/pybullet/gym/pybullet_envs/bullet/racecarGymEnv.py +++ b/examples/pybullet/gym/pybullet_envs/bullet/racecarGymEnv.py @@ -12,7 +12,7 @@ import numpy as np import pybullet from . import racecar import random -from . import bullet_client +import pybullet_utils.bullet_client as bc import pybullet_data from pkg_resources import parse_version @@ -40,9 +40,9 @@ class RacecarGymEnv(gym.Env): self._renders = renders self._isDiscrete = isDiscrete if self._renders: - self._p = bullet_client.BulletClient(connection_mode=pybullet.GUI) + self._p = bc.BulletClient(connection_mode=pybullet.GUI) else: - self._p = bullet_client.BulletClient() + self._p = bc.BulletClient() self.seed() #self.reset() diff --git a/examples/pybullet/gym/pybullet_envs/bullet/racecarZEDGymEnv.py b/examples/pybullet/gym/pybullet_envs/bullet/racecarZEDGymEnv.py index 8137708ba..5e8641cef 100644 --- a/examples/pybullet/gym/pybullet_envs/bullet/racecarZEDGymEnv.py +++ b/examples/pybullet/gym/pybullet_envs/bullet/racecarZEDGymEnv.py @@ -10,7 +10,7 @@ from gym.utils import seeding import numpy as np import time import pybullet -from . import bullet_client +import pybullet_utils.bullet_client as bc from . import racecar import random import pybullet_data @@ -42,9 +42,9 @@ class RacecarZEDGymEnv(gym.Env): self._isDiscrete = isDiscrete if self._renders: - self._p = bullet_client.BulletClient(connection_mode=pybullet.GUI) + self._p = bc.BulletClient(connection_mode=pybullet.GUI) else: - self._p = bullet_client.BulletClient() + self._p = bc.BulletClient() self.seed() self.reset() diff --git a/examples/pybullet/gym/pybullet_envs/minitaur/envs/bullet_client.py b/examples/pybullet/gym/pybullet_envs/minitaur/envs/bullet_client.py deleted file mode 100644 index 54e0bd09e..000000000 --- a/examples/pybullet/gym/pybullet_envs/minitaur/envs/bullet_client.py +++ /dev/null @@ -1,54 +0,0 @@ -"""A wrapper for pybullet to manage different clients.""" -from __future__ import absolute_import -from __future__ import division -import functools -import inspect -import pybullet - - -class BulletClient(object): - """A wrapper for pybullet to manage different clients.""" - - def __init__(self, connection_mode=None): - """Creates a Bullet client and connects to a simulation. - - Args: - connection_mode: - `None` connects to an existing simulation or, if fails, creates a - new headless simulation, - `pybullet.GUI` creates a new simulation with a GUI, - `pybullet.DIRECT` creates a headless simulation, - `pybullet.SHARED_MEMORY` connects to an existing simulation. - """ - self._shapes = {} - - if connection_mode is None: - self._client = pybullet.connect(pybullet.SHARED_MEMORY) - if self._client >= 0: - return - else: - connection_mode = pybullet.DIRECT - self._client = pybullet.connect(connection_mode) - - def __del__(self): - """Clean up connection if not already done.""" - try: - pybullet.disconnect(physicsClientId=self._client) - except pybullet.error: - pass - - def __getattr__(self, name): - """Inject the client id into Bullet functions.""" - attribute = getattr(pybullet, name) - if inspect.isbuiltin(attribute): - if name not in [ - "invertTransform", - "multiplyTransforms", - "getMatrixFromQuaternion", - "getEulerFromQuaternion", - "computeViewMatrixFromYawPitchRoll", - "computeProjectionMatrixFOV", - "getQuaternionFromEuler", - ]: # A temporary hack for now. - attribute = functools.partial(attribute, physicsClientId=self._client) - return attribute diff --git a/examples/pybullet/gym/pybullet_envs/minitaur/envs/minitaur_gym_env.py b/examples/pybullet/gym/pybullet_envs/minitaur/envs/minitaur_gym_env.py index 44297d11b..a48d46a03 100644 --- a/examples/pybullet/gym/pybullet_envs/minitaur/envs/minitaur_gym_env.py +++ b/examples/pybullet/gym/pybullet_envs/minitaur/envs/minitaur_gym_env.py @@ -14,7 +14,7 @@ from gym import spaces from gym.utils import seeding import numpy as np import pybullet -from pybullet_envs.minitaur.envs import bullet_client +import pybullet_utils.bullet_client as bc import pybullet_data from pybullet_envs.minitaur.envs import minitaur from pybullet_envs.minitaur.envs import minitaur_derpy @@ -223,9 +223,9 @@ class MinitaurGymEnv(gym.Env): self._env_randomizers = convert_to_list(env_randomizer) if env_randomizer else [] self._episode_proto = minitaur_logging_pb2.MinitaurEpisode() if self._is_render: - self._pybullet_client = bullet_client.BulletClient(connection_mode=pybullet.GUI) + self._pybullet_client = bc.BulletClient(connection_mode=pybullet.GUI) else: - self._pybullet_client = bullet_client.BulletClient() + self._pybullet_client = bc.BulletClient() if self._urdf_version is None: self._urdf_version = DEFAULT_URDF_VERSION self._pybullet_client.setPhysicsEngineParameter(enableConeFriction=0) diff --git a/examples/pybullet/gym/pybullet_envs/prediction/pybullet_sim_gym_env.py b/examples/pybullet/gym/pybullet_envs/prediction/pybullet_sim_gym_env.py index 1c308a7fb..e7ba23040 100644 --- a/examples/pybullet/gym/pybullet_envs/prediction/pybullet_sim_gym_env.py +++ b/examples/pybullet/gym/pybullet_envs/prediction/pybullet_sim_gym_env.py @@ -14,7 +14,7 @@ from gym import spaces from gym.utils import seeding import numpy as np import pybullet -from pybullet_envs.bullet import bullet_client +import pybullet_utils.bullet_client as bc from pybullet_envs.prediction import boxstack_pybullet_sim @@ -71,10 +71,10 @@ class PyBulletSimGymEnv(gym.Env): print("urdf_root=" + self._urdf_root) if self._is_render: - self._pybullet_client = bullet_client.BulletClient(connection_mode=pybullet.GUI, + self._pybullet_client = bc.BulletClient(connection_mode=pybullet.GUI, options=optionstring) else: - self._pybullet_client = bullet_client.BulletClient() + self._pybullet_client = bc.BulletClient() if (debug_visualization == False): self._pybullet_client.configureDebugVisualizer(flag=self._pybullet_client.COV_ENABLE_GUI,