SourajaKundu123's picture
Upload folder using huggingface_hub
4cebba5 verified
Raw History Blame Contribute Delete
8 kB
import os, math, random
if "PYOPENGL_PLATFORM" not in os.environ:
if not os.environ.get("DISPLAY"):
os.environ["PYOPENGL_PLATFORM"] = "egl"
import numpy as np
import pybullet as p
pb = p
import pybullet_data
import trimesh
import pyrender
import imageio
G = 9.81
RESOLUTION = (480, 480)
FPS = 30
STEP_RATE = 240
def _hsv_rgb(h, s, v):
hi = int(h*6)%6; f=h*6-int(h*6); p_=v*(1-s); q=v*(1-f*s); t=v*(1-(1-f)*s)
return [(v,t,p_),(q,v,p_),(p_,v,t),(p_,q,v),(t,p_,v),(v,p_,q)][hi]
def random_hue_color(s=0.85, v=0.90):
r, g, b = _hsv_rgb(random.random(), s, v)
return [r, g, b, 1.0]
def muted_grey(lo=0.30, hi=0.55):
c = random.uniform(lo, hi)
return [c, c + random.uniform(-0.03, 0.03), c + random.uniform(-0.03, 0.03), 1.0]
def floor_color():
return muted_grey(0.38, 0.52)
def _look_at(eye, target, up=(0,0,1)):
eye=np.array(eye,float); target=np.array(target,float); up=np.array(up,float)
fwd=target-eye; fwd/=np.linalg.norm(fwd)
right=np.cross(fwd,up)
if np.linalg.norm(right)<1e-8: right=np.array([1.,0.,0.])
right/=np.linalg.norm(right); up2=np.cross(right,fwd)
m=np.eye(4); m[:3,0]=right; m[:3,1]=up2; m[:3,2]=-fwd; m[:3,3]=eye
return m
def _q2r(xyzw):
x,y,z,w=xyzw
return np.array([[1-2*(y*y+z*z),2*(x*y-z*w),2*(x*z+y*w)],
[2*(x*y+z*w),1-2*(x*x+z*z),2*(y*z-x*w)],
[2*(x*z-y*w),2*(y*z+x*w),1-2*(x*x+y*y)]])
def _set_pose(scene, node, pos, quat):
R=_q2r(quat); m=np.eye(4); m[:3,:3]=R; m[:3,3]=np.array(pos)
scene.set_pose(node, pose=m)
def _make_renderer(cam_eye, cam_target):
W,H=RESOLUTION
ren=pyrender.OffscreenRenderer(W,H)
sc=pyrender.Scene(ambient_light=[0.10,0.10,0.12,1.0],bg_color=[0.06,0.06,0.10,1.0])
for color,intensity,eye in [([1.00,0.95,0.88],5.5,[-3,-4,6]),
([0.50,0.62,1.00],2.0,[4,3,4]),
([0.90,0.90,0.90],0.8,[0,-5,1])]:
sc.add(pyrender.DirectionalLight(color=color,intensity=intensity),
pose=_look_at(eye,[0,0,0]))
cam=pyrender.PerspectiveCamera(yfov=math.pi/4,aspectRatio=W/H,znear=0.05,zfar=300.)
sc.add(cam,pose=_look_at(cam_eye,cam_target))
return ren,sc
def _mat(color,metallic=0.05,roughness=0.5):
return pyrender.MetallicRoughnessMaterial(baseColorFactor=color,
metallicFactor=metallic,roughnessFactor=roughness)
def _add_sphere(sc,name,nodes,r,color,metallic=0.1,roughness=0.3):
tm=trimesh.creation.icosphere(subdivisions=3,radius=r)
nodes[name]=sc.add(pyrender.Mesh.from_trimesh(tm,material=_mat(color,metallic,roughness),smooth=True),pose=np.eye(4))
def _add_box(sc,name,nodes,he,color,metallic=0.05,roughness=0.55):
tm=trimesh.creation.box(extents=[e*2 for e in he])
nodes[name]=sc.add(pyrender.Mesh.from_trimesh(tm,material=_mat(color,metallic,roughness),smooth=False),pose=np.eye(4))
def _add_cylinder(sc,name,nodes,r,h,color,metallic=0.05,roughness=0.5):
tm=trimesh.creation.cylinder(radius=r,height=h,sections=32)
nodes[name]=sc.add(pyrender.Mesh.from_trimesh(tm,material=_mat(color,metallic,roughness),smooth=True),pose=np.eye(4))
def _add_capsule(sc,name,nodes,r,h,color):
tm=trimesh.creation.capsule(radius=r,height=h,count=[8,8])
nodes[name]=sc.add(pyrender.Mesh.from_trimesh(tm,material=_mat(color),smooth=True),pose=np.eye(4))
def _update_string(sc,nodes,name,p1,p2,radius=0.006,color=None):
if color is None: color=[0.22,0.16,0.10,1.0]
if name in nodes: sc.remove_node(nodes.pop(name))
p1=np.array(p1,float); p2=np.array(p2,float)
diff=p2-p1; length=float(np.linalg.norm(diff))
if length<1e-6: return
mid=(p1+p2)*0.5; z=diff/length
up=np.array([0.,0.,1.]) if abs(z[2])<0.95 else np.array([0.,1.,0.])
x=np.cross(up,z); x/=np.linalg.norm(x); y=np.cross(z,x)
m=np.eye(4); m[:3,0]=x; m[:3,1]=y; m[:3,2]=z; m[:3,3]=mid
tm=trimesh.creation.cylinder(radius=radius,height=length,sections=8)
mat=pyrender.MetallicRoughnessMaterial(baseColorFactor=color,metallicFactor=0.3,roughnessFactor=0.7)
nodes[name]=sc.add(pyrender.Mesh.from_trimesh(tm,material=mat,smooth=True),pose=m)
def _save_video(frames,path,fps=FPS):
w=imageio.get_writer(path,fps=fps,codec='libx264',quality=5,macro_block_size=None)
for f in frames: w.append_data(f)
w.close()
def _init_physics():
c=p.connect(p.DIRECT)
p.setGravity(0,0,-G,physicsClientId=c)
p.setAdditionalSearchPath(pybullet_data.getDataPath(),physicsClientId=c)
p.setPhysicsEngineParameter(fixedTimeStep=1.0/STEP_RATE,
restitutionVelocityThreshold=0.2,useSplitImpulse=1,
splitImpulsePenetrationThreshold=-0.02,contactSlop=0.0,
physicsClientId=c)
return c
import math, pybullet as pb, numpy as np
N_FRAMES=150; CAM_EYE=(3.5, -4.5, 2.2); CAM_TARGET=(0.5, 0.0, 0.4)
A=1.1628946679559673; OMG=11.25143956684665; FR=0.5770325859317526
PUCK_R=0.1; PUCK_H=0.06; PM=0.18; NP=5
PANEL_M=50.0
PUCK_COLS=[[0.9, 0.13500000000000004, 0.32504568583185994, 1.0], [0.13500000000000004, 0.9, 0.2990710892974331, 1.0], [0.13500000000000004, 0.7494865781672225, 0.9, 1.0], [0.13500000000000004, 0.9, 0.3249753326487045, 1.0], [0.13500000000000004, 0.6700854374764816, 0.9, 1.0]]
MARK_COLS=[[1.0, 0.050000000000000044, 0.9533932691485902, 1.0], [1.0, 0.050000000000000044, 0.09100445296653115, 1.0], [1.0, 0.050000000000000044, 0.4573177504975948, 1.0], [1.0, 0.050000000000000044, 0.4016412703490756, 1.0], [1.0, 0.3600213931372337, 0.050000000000000044, 1.0]]
def main():
c=_init_physics(); ren,sc=_make_renderer(CAM_EYE,CAM_TARGET); nodes={}
plane_bid=pb.loadURDF("plane.urdf",[0,0,0],physicsClientId=c)
pb.changeDynamics(plane_bid,-1,lateralFriction=0.04,physicsClientId=c)
_add_box(sc,"fl",nodes,[4,4,0.02],[0.38,0.38,0.42,1.0]); _set_pose(sc,nodes["fl"],[0,0,-0.02],[0,0,0,1])
he=[1.0,0.7,0.04]; pz=he[2]
s=pb.createCollisionShape(pb.GEOM_BOX,halfExtents=he,physicsClientId=c)
pbid=pb.createMultiBody(PANEL_M,s,-1,[0,0,pz],[0,0,0,1],physicsClientId=c)
pb.changeDynamics(pbid,-1,lateralFriction=FR,restitution=0.1,linearDamping=0.0,angularDamping=0.5,physicsClientId=c)
_add_box(sc,"panel",nodes,he,[0.5, 0.5, 0.55, 1.0]); bids=[("panel",pbid)]
sphere_R=PUCK_H/2; top_z=2*he[2]; puck_z0=top_z+sphere_R+0.001
pos_xy=[(-0.55,-0.20),(-0.25,0.25),(0.05,-0.20),(0.30,0.25),(0.60,-0.20)]
disc_Iz=0.5*PM*PUCK_R*PUCK_R; big_I=100.0
mark_r=0.022; mark_off=np.array([PUCK_R*0.78,0.0,PUCK_H/2+mark_r])
for i in range(NP):
x,y=pos_xy[i]; od=1 if i%2==0 else -1
s=pb.createCollisionShape(pb.GEOM_SPHERE,radius=sphere_R,physicsClientId=c)
bid=pb.createMultiBody(PM,s,-1,[x,y,puck_z0],[0,0,0,1],physicsClientId=c)
pb.changeDynamics(bid,-1,lateralFriction=FR,restitution=0.0,linearDamping=0.0,angularDamping=0.0,localInertiaDiagonal=[big_I,big_I,disc_Iz],physicsClientId=c)
pb.resetBaseVelocity(bid,[0,0,0],[0,0,OMG*od],physicsClientId=c)
_add_cylinder(sc,f"p{i}",nodes,PUCK_R,PUCK_H,PUCK_COLS[i])
_add_sphere(sc,f"m{i}",nodes,mark_r,MARK_COLS[i]); bids.append((f"p{i}",bid))
F=PANEL_M*A; frames=[]
for fi in range(N_FRAMES):
for __ in range(8):
pb.applyExternalForce(pbid,-1,[F,0,0],[0,0,0],pb.LINK_FRAME,physicsClientId=c)
pb.stepSimulation(physicsClientId=c)
for nm,bid in bids:
pp,oo=pb.getBasePositionAndOrientation(bid,physicsClientId=c); _set_pose(sc,nodes[nm],pp,oo)
for i in range(NP):
_,bid=bids[i+1]; pp,oo=pb.getBasePositionAndOrientation(bid,physicsClientId=c)
R=_q2r(oo); mw=np.array(pp)+R@mark_off
_set_pose(sc,nodes[f"m{i}"],mw.tolist(),oo)
rgba,_=ren.render(sc,flags=pyrender.RenderFlags.RGBA); frames.append(rgba[:,:,:3])
_save_video(frames,"video.mp4"); pb.disconnect(c); ren.delete(); print("Saved video.mp4")
if __name__=="__main__": main()