Skip to content

Instantly share code, notes, and snippets.

@fgolemo
Last active February 4, 2021 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fgolemo/c462ddea0d32e6060305d424cead2f54 to your computer and use it in GitHub Desktop.
Save fgolemo/c462ddea0d32e6060305d424cead2f54 to your computer and use it in GitHub Desktop.
PyBullet shitty rendering sample
import time
import numpy as np
import pybullet_utils.bullet_client as bc
import pybullet
import pybullet_data
import matplotlib.pyplot as plt
from tqdm import trange
# simulator 0
p = bc.BulletClient(connection_mode=pybullet.DIRECT)
p.setAdditionalSearchPath(pybullet_data.getDataPath())
frequency = 240 # Hz
p.setGravity(0, 0, -10) # good enough
p.setTimeStep(1 / frequency)
p.setRealTimeSimulation(0)
# load checkerboard floor
planeId = p.loadURDF("plane.urdf")
scale = .05
meshScale = np.array([1, 1, 1]) * scale
# visual shape and collision shape are defined separately
cube_visual = p.createVisualShape(
shapeType=p.GEOM_BOX,
halfExtents=meshScale,
rgbaColor=[1, 0, 0, 1],
specularColor=[0, 1, 0])
cube_collision = p.createCollisionShape(
shapeType=p.GEOM_BOX, halfExtents=meshScale)
# this is where the camera view matrix are specified
cam_view = p.computeViewMatrix(
cameraEyePosition=[.4, .3, .2],
cameraTargetPosition=[0, .3, 0.1],
cameraUpVector=[0, 0, 1])
# rendering resolution
cam_width = 200
cam_height = 200
# camera projection matrix (intrinsics)
cam_proj = p.computeProjectionMatrixFOV(
fov=90, aspect=cam_width / cam_height, nearVal=0.1, farVal=10)
# for the cube this next command is what actually puts the cube into the simulator.
cube = 0.createMultiBody(
baseMass=.1,
baseInertialFramePosition=[0, 0, 0],
baseCollisionShapeIndex=cube_collision,
baseVisualShapeIndex=cube_visual,
basePosition=[0, 0.2, 0],
baseOrientation=p.getQuaternionFromEuler([0, 0, 0]))
# stabilize simulation
for i in range(20):
p.stepSimulation()
# live plotting
plt.ion()
fig, ax = plt.subplots(nrows=1, ncols=1)
ax0 = ax[0].imshow(
np.zeros((200, 200, 3)),
interpolation='none',
animated=True,
label="test")
plot = plt.gca()
# here we're running the simulator for a fixed number of steps.
steps = 1000
start = time.time()
for i in trange(steps):
# step both simulators
p.stepSimulation()
# get camera snapshot of real robot - in experiments this does NOT have to be run every cycle
img0 = p.getCameraImage(
cam_width,
cam_height,
cam_view,
cam_proj,
renderer=p.ER_BULLET_HARDWARE_OPENGL,
flags=p.ER_NO_SEGMENTATION_MASK)
ax0_img = getImg(img0, 200, 200)
ax0.set_data(ax0_img)
fig.suptitle(f"step {i}", fontsize=16)
plt.pause(0.001)
i += 1
if i == 50:
# this is just for fun: apply a force to the cube and see it fly away, in case you ever need that for something
p.applyExternalForce(
objectUniqueId=cube,
linkIndex=-1,
forceObj=[0, 15, 0],
posObj=[0, 0, 0.05],
flags=p.WORLD_FRAME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment