Skip to content

Instantly share code, notes, and snippets.

@arpit15
Created March 23, 2021 13:37
Show Gist options
  • Save arpit15/ca3e252995c10a2d4339d43ed69c2ea1 to your computer and use it in GitHub Desktop.
Save arpit15/ca3e252995c10a2d4339d43ed69c2ea1 to your computer and use it in GitHub Desktop.
# This is the basic script that can render images for a given object. It gives RGB image, Depth Image, and Segmentation Mask.
import pybullet as pb
physicsClient = pb.connect(pb.GUI)
import numpy as np
import pybullet_data
pb.setAdditionalSearchPath(pybullet_data.getDataPath())
planeId = pb.loadURDF('plane.urdf')
# sphereId = pb.loadURDF('simple_sphere.urdf', basePosition = [0, 0, 1], baseOrientation = [0,0,0,1], useFixedBase = 1)
sphereId = pb.loadURDF('../data/sphereCollision/simple_sphere_red.urdf', basePosition = [0, 0, 1.5], baseOrientation = [0,0,0,1], useFixedBase = 0)
collision_shape_id = pb.createCollisionShape(shapeType=pb.GEOM_MESH, fileName='../data/sphereCollision/hollow-sphere.obj')
visual_shape_id = pb.createVisualShape(shapeType=pb.GEOM_MESH, fileName='../data/sphereCollision/hollow-sphere.obj')
bullet_body_id = pb.createMultiBody(
basePosition=[0,0,0], baseOrientation=[0,0,0,1],
baseCollisionShapeIndex=collision_shape_id, baseVisualShapeIndex=visual_shape_id,
)
collisionFilterGroup = 0
collisionFilterMask = 0
pb.setCollisionFilterGroupMask(sphereId, -1, collisionFilterGroup, collisionFilterMask)
enableCollision = 0
pb.setCollisionFilterPair(planeId, sphereId, -1, -1, True)
pb.setCollisionFilterPair(bullet_body_id, sphereId, -1, -1, enableCollision)
# # Create Visual Shape for the object
# visualShapeId = pb.createVisualShape(shapeType = pb.GEOM_MESH, fileName = 'random_urdfs/000/000.obj', rgbaColor = None, meshScale = [0.1, 0.1, 0.1])
# collisionShapeId = pb.createCollisionShape(shapeType = pb.GEOM_MESH, fileName = 'random_urdfs/000/000_coll.obj', meshScale = [0.1, 0.1, 0.1])
# # Until now the visual shape and the collider are two separate entities. We have to use the below functions to create the object instance in the simulation.
# multiBodyId = pb. createMultiBody(baseMass = 1.0, baseCollisionShapeIndex = collisionShapeId, baseVisualShapeIndex = visualShapeId, basePosition = [0,0,1], baseOrientation = pb.getQuaternionFromEuler([0, 0, 0]))
# Adding texture to the object
import os, glob, random
# texture_paths = glob.glob(os.path.join('dtd', '**', '*.jpg'), recursive = True)
# random_texture_path = texture_paths[random.randint(0, len(texture_paths)-1)]
# textureId = pb. loadTexture(random_texture_path)
# pb.changeVisualShape(multiBodyId, -1, textureUniqueId = textureId)
viewMatrix = pb.computeViewMatrix(
cameraEyePosition=[0, 0, 0],
cameraTargetPosition=[0, 0,5],
cameraUpVector=[0, 1, 0.8])
projectionMatrix = pb.computeProjectionMatrixFOV(
fov=100.0,
aspect=1.0,
nearVal=0.001,
farVal=10)
# Adding gravitational force
pb.setGravity(0, 0, -9.8)
pb.setRealTimeSimulation(1)
while(True):
width, height, rgbImg, depthImg, segImg = pb.getCameraImage(
width=224,
height=224,
viewMatrix=viewMatrix,
projectionMatrix=projectionMatrix)
# input()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment