Skip to content

Instantly share code, notes, and snippets.

@jgehring
Created April 17, 2020 22:52
Show Gist options
  • Save jgehring/95d42638454b016cc9cf434b552a1952 to your computer and use it in GitHub Desktop.
Save jgehring/95d42638454b016cc9cf434b552a1952 to your computer and use it in GitHub Desktop.
from tempfile import NamedTemporaryFile
import imageio
import pybullet as p
import pybullet_data
# Test MCJF file
df = NamedTemporaryFile()
df.write('''
<mujoco>
<worldbody>
<body pos="0 0 1">
<geom type="box" size=".1 .2 .3" rgba="0 .9 0 1"/>
</body>
<body pos="1 0 1">
<geom type="sphere" size=".2" pos="0 0 0" rgba="0 0 .9 1"/>
</body>
</worldbody>
</mujoco>
'''.encode('utf-8'))
df.flush()
def render_img(pitch):
w, h = 500, 500
view_matrix = p.computeViewMatrixFromYawPitchRoll(cameraTargetPosition=(0,
0,
0),
distance=5,
yaw=0,
pitch=pitch,
roll=0,
upAxisIndex=2)
proj_matrix = p.computeProjectionMatrixFOV(fov=60,
aspect=w / h,
nearVal=0.1,
farVal=100.0)
_, _, px, _, _ = p.getCameraImage(width=w,
height=h,
viewMatrix=view_matrix,
projectionMatrix=proj_matrix,
renderer=p.ER_BULLET_HARDWARE_OPENGL,
flags=p.ER_NO_SEGMENTATION_MASK)
return px[:, :, :3]
# Test GUI mode
p.connect(p.GUI)
p.loadURDF(pybullet_data.getDataPath() + '/plane.urdf')
mjbox = p.loadMJCF(df.name)[0]
print(f'box mass {mjbox} {p.getDynamicsInfo(mjbox, -1)[0]}')
print(f'box size {mjbox} {p.getCollisionShapeData(mjbox, -1)[0][3]}')
vs = p.createVisualShape(p.GEOM_BOX,
halfExtents=(0.1, 0.2, 0.3),
rgbaColor=(0.9, 0, 0, 1))
cs = p.createCollisionShape(p.GEOM_BOX, halfExtents=[0.1, 0.2, 0.3])
p.createMultiBody(baseMass=0,
baseCollisionShapeIndex=cs,
baseVisualShapeIndex=vs,
basePosition=(-1, 0, 1),
baseOrientation=(0, 0, 0, 1))
imageio.imwrite('render_gui_1.png', render_img(-10))
imageio.imwrite('render_gui_2.png', render_img(-90))
p.disconnect()
# Test direct mode
p.connect(p.DIRECT)
p.loadURDF(pybullet_data.getDataPath() + '/plane.urdf')
mjbox = p.loadMJCF(df.name)[0]
print(f'box mass {mjbox} {p.getDynamicsInfo(mjbox, -1)[0]}')
print(f'box size {mjbox} {p.getCollisionShapeData(mjbox, -1)[0][3]}')
vs = p.createVisualShape(p.GEOM_BOX,
halfExtents=(0.1, 0.2, 0.3),
rgbaColor=(0.9, 0, 0, 1))
cs = p.createCollisionShape(p.GEOM_BOX, halfExtents=[0.1, 0.2, 0.3])
p.createMultiBody(baseMass=0,
baseCollisionShapeIndex=cs,
baseVisualShapeIndex=vs,
basePosition=(-1, 0, 1),
baseOrientation=(0, 0, 0, 1))
imageio.imwrite('render_direct_1.png', render_img(-10))
imageio.imwrite('render_direct_2.png', render_img(-90))
p.disconnect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment