Genesis
Genesis copied to clipboard
[Bug]: Why do mesh objects stick together and disappear?"
Bug Description
Here is my code script:
import genesis as gs
import trimesh
import os
import time
import numpy as np
def calculate_triangle_center(obj_file):
"""计算三角形的中心点位置"""
vertices = []
with open(obj_file, 'r') as f:
for line in f:
if line.startswith('v '): # 只读取顶点行
# 分割行并转换为浮点数
_, x, y, z = line.split()
vertices.append([float(x),float(z), float(y)]) #置换y与z的坐标,将块体位置投放到xz平面,用于模拟块体坠落
if len(vertices) == 3: # 只需要前三个顶点
break
if len(vertices) < 3:
return (0, 0, 0) # 如果没有足够的顶点,返回原点
# 计算三个顶点的平均值
vertices = np.array(vertices)
center = np.mean(vertices, axis=0)
return tuple(center) # 返回(x, y, z)元组
def main():
# 初始化Genesis
gs.init(backend=gs.gpu,
theme = 'light',
precision='32'
) # 使用GPU后端
# 创建场景,使用用户给定的参数
scene = gs.Scene(
sim_options=gs.options.SimOptions(substeps=8),
show_FPS=False,
show_viewer=True,
# rigid_options=gs.options.RigidOptions(
# dt=0.01,
# gravity=(0,0,-0.8),
# enable_collision=True,
# constraint_solver=gs.constraint_solver.Newton,
# iterations=50,
# tolerance=1e-7,
# integrator=gs.integrator.implicitfast,
# use_contact_island=True,
# ),
viewer_options = gs.options.ViewerOptions(
res = (640, 480), # 降低分辨率
camera_pos = (10, 10, 10), # 调整相机位置到更高的位置
camera_lookat = (5, 5, 5), # 看向物体下落的位置
camera_fov = 60, # 增加视场角以看到更多场景
max_FPS = 30, # 降低帧率
),
vis_options = gs.options.VisOptions(
show_world_frame = True, # 显示原点坐标系
world_frame_size = 5, # 坐标系长度(米)
show_link_frame = False, # 不显示实体链接坐标系
show_cameras = False, # 不显示相机网格和视锥
plane_reflection = False, # 开启平面反射
ambient_light = (0.1, 0.1, 0.1), # 环境光
),
# renderer = gs.renderers.Rasterizer(), # 使用光栅化渲染器
)
# frictionless_rigid = gs.materials.Rigid(needs_coup=True, coup_friction=0.5)
# 读取从prism_0.obj到prism_8.obj的文件
temp_dir = "examples/obj/temp/temp"
# 使用数字排序而不是字符串排序
def get_prism_number(filename):
# 从文件名中提取数字
return int(filename.replace('prism_', '').replace('.obj', ''))
# 获取所有obj文件并按数字排序
obj_files = [f for f in os.listdir(temp_dir) if f.endswith('.obj')]
obj_files = sorted(obj_files, key=get_prism_number)
# 只保留0-8的文件
obj_files = [f for f in obj_files if 0 <= get_prism_number(f) <= 5]
print(f"将渲染以下文件: {obj_files}")
E, nu = 1.e7,0.3
rho = 100.0
friction = 0.5
# 保存物体引用,用于后续设置初始速度
prism_objects = []
for obj_file in obj_files:
file_path = os.path.join(temp_dir, obj_file)
# 计算三棱柱体积和质量(可选)
mesh = trimesh.load(file_path)
volume = mesh.volume
mass = volume * 10.0 # 假设密度为10
# 计算物体的中心位置
center_pos = calculate_triangle_center(file_path)
print(f"物体 {obj_file} 的中心位置: {center_pos}")
# 添加到场景
obj = scene.add_entity(
material=gs.materials.Rigid(friction=friction),
morph=gs.morphs.Mesh(
file=file_path,
scale=1,
# pos=center_pos,
),
surface=gs.surfaces.Default(color=(1, 0, 0)),
)
prism_objects.append(obj)
# 定义初始速度(只包含线性速度,3个分量)
v_obj_init = gs.tensor([0.0, 0.0, 0, 0, 0, 0], requires_grad=True)
# 定义阻尼参数
damping = gs.tensor([0.0, 0.5, 0.0, 0, 0, 0])
# obj2 = scene.add_entity(
# gs.morphs.Box(pos=(-0.4, 0, 0.06), size=(0.1, 0.1, 0.1)),
# surface=gs.surfaces.Default(color=(0, 0, 1)),
# material=gs.materials.Rigid(gravity_compensation=1.0),
# )
# Support
table = scene.add_entity(
gs.morphs.Box(pos=(0.0, 0, -10), size=(20, 20, 0.5), fixed=True),
material=gs.materials.Rigid(friction=friction),
)
# 添加录屏相机(必须在scene.build()之前)
cam = scene.add_camera(
res = (640, 480),
pos = (20, 0,20),
lookat = (5, 0, 5),
fov = 40,
GUI = False,
)
# 构建场景
scene.build()
# 录屏主循环
cam.start_recording()
input("Start?")
for i in range(1000):
if i < 400:
obj.control_dofs_velocity([2, 2, -2, 0, 0, 0])
else:
obj.control_dofs_velocity([0, 0, 0, 0, 0, 0])
scene.step()
cam.render()
cam.stop_recording(save_to_filename='examples/obj/prism_simulation.mp4', fps=30)
print('录屏已结束,视频已保存。')
# 继续交互式运行(可选)
while True:
scene.step()
if __name__ == "__main__":
main()
Steps to Reproduce
Here is my obj files
Expected Behavior
Looking forward to your response.
Release version or Commit ID
first
The issue is not clear for now. Could you share a video?
https://github.com/user-attachments/assets/0d937290-4aad-4370-a84b-6c3ede6bf0c3 Hello.From this video, you can see that at the beginning of the scene, objects suddenly disappear, making further simulation impossible. I think the possible reasons are: 1) shared vertices between models, and 2) a minimum size limitation for blocks. I'm not entirely sure if Genesis has limitations in this regard, and I hope to get a response from the developers. Thank you.