Skip to content

Commit

Permalink
move atexit to pybind
Browse files Browse the repository at this point in the history
  • Loading branch information
fbxiang committed Nov 2, 2023
1 parent edbfd39 commit 628d09e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 44 deletions.
50 changes: 16 additions & 34 deletions manualtest/stereodepth.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import sapien
from sapien import Scene, Entity, Pose, ActorBuilder, VisualMaterialRecord
from sapien import Scene, Entity, Pose, ActorBuilder
from sapien.physx import PhysxSystem
from sapien.render import RenderSystem
from sapien.utils.viewer import Viewer
from sapien.sensor import StereoDepthSensor, StereoDepthSensorConfig

import numpy as np
import matplotlib.pyplot as plt
import open3d as o3d
import trimesh
import torch


Expand All @@ -20,7 +20,7 @@ def build_scene(render_system, physx_system):
.add_plane_visual(
Pose(q=[0.7071068, 0, -0.7071068, 0]),
[10, 10, 10],
VisualMaterialRecord(
sapien.render.RenderMaterial(
base_color=np.array([202, 164, 114, 256]) / 256, specular=0.5
),
"",
Expand All @@ -36,7 +36,7 @@ def build_scene(render_system, physx_system):
.add_sphere_visual(
Pose(),
0.06,
VisualMaterialRecord(
sapien.render.RenderMaterial(
base_color=[0.2, 0.2, 0.8, 1.0], roughness=0.5, metallic=0.0
),
"",
Expand All @@ -53,7 +53,7 @@ def build_scene(render_system, physx_system):
.add_sphere_visual(
Pose(),
0.07,
VisualMaterialRecord(
sapien.render.RenderMaterial(
base_color=[1.0, 1.0, 1.0, 1.0],
roughness=0.3,
metallic=0.0,
Expand All @@ -75,7 +75,7 @@ def build_scene(render_system, physx_system):
Pose(),
0.02,
0.1,
VisualMaterialRecord(
sapien.render.RenderMaterial(
base_color=[0.8, 0.7, 0.1, 1.0], roughness=0.01, metallic=0.95
),
"",
Expand All @@ -92,7 +92,7 @@ def build_scene(render_system, physx_system):
.add_box_visual(
Pose(),
[0.09, 0.09, 0.09],
VisualMaterialRecord(
sapien.render.RenderMaterial(
base_color=[0.8, 0.2, 0.2, 1.0], roughness=0.01, metallic=1.0
),
"",
Expand Down Expand Up @@ -148,45 +148,27 @@ def main():
ir_l, ir_r = sensor.get_ir()
depth = sensor.get_depth()

# Testing cuda buffer for depth
# depth_cuda = sensor.get_depth_cuda()
# torch_tensor = torch.as_tensor(depth_cuda)
# depth = torch_tensor.numpy()

plt.subplot(221)
plt.title("RGB Image")
plt.imshow((rgb * 255).astype(np.uint8))
plt.subplot(222)
plt.title("Left Infrared Image")
plt.imshow((ir_l * 255).astype(np.uint8), cmap='gray')
plt.imshow((ir_l * 255).astype(np.uint8), cmap="gray")
plt.subplot(223)
plt.title("Right Infrared Image")
plt.imshow((ir_r * 255).astype(np.uint8), cmap='gray')
plt.imshow((ir_r * 255).astype(np.uint8), cmap="gray")
plt.subplot(224)
plt.title("Depth Map")
plt.imshow(depth)
plt.show()

pc = sensor.get_pointcloud(with_rgb=True) # From RGB camera's view with x rightward, y downward, z forward
pcd = o3d.geometry.PointCloud(o3d.utility.Vector3dVector(pc[..., :3] * np.array([1, -1, -1]))) # Change axis direction for easier view
pcd.colors = o3d.utility.Vector3dVector(pc[..., 3:])
o3d.visualization.draw_geometries([pcd])

# Testing cuda buffer for pointcloud
# pc = sensor.get_pointcloud_cuda(with_rgb=True) # From RGB camera's view with x rightward, y downward, z forward
# pc = torch.as_tensor(pc).numpy()
# pcd = o3d.geometry.PointCloud(o3d.utility.Vector3dVector(pc[..., :3] * np.array([1, -1, -1]))) # Change axis direction for easier view
# pcd.colors = o3d.utility.Vector3dVector(pc[..., 3:])
# o3d.visualization.draw_geometries([pcd])

# viewer = Viewer(resolutions=(1920, 1080), shader_dir="../vulkan_shader/ibl")
# viewer.set_scene(scene)
# viewer.set_camera_pose(Pose([-0.420344, -0.218716, 0.339383], [0.927951, -0.0967264, 0.211487, 0.291245]))
# while not viewer.closed:
# physx_system.step()
# render_system.step()
# viewer.render()
# viewer.close()
pc = sensor.get_pointcloud(
with_rgb=True
) # From RGB camera's view with x rightward, y downward, z forward
pcd = trimesh.PointCloud(
pc[..., :3] * np.array([1, -1, -1]), pc[..., 3:]
) # Change axis direction for easier view
pcd.show()


if __name__ == "__main__":
Expand Down
4 changes: 0 additions & 4 deletions python/py_package/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,3 @@

from . import utils
from . import asset

import atexit

atexit.register(physx._unload)
2 changes: 0 additions & 2 deletions python/py_package/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ from sapien.wrapper.renderer import SapienRenderer
from sapien.wrapper.scene import Scene
from sapien.pysapien import System
from sapien.wrapper.scene import Widget
import atexit
import os
import pkg_resources
import platform
Expand All @@ -41,7 +40,6 @@ __all__ = [
"System",
"Widget",
"asset",
"atexit",
"internal_renderer",
"math",
"os",
Expand Down
2 changes: 0 additions & 2 deletions python/py_package/pysapien/physx/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1101,8 +1101,6 @@ class PhysxSystem(sapien.pysapien.System):
def timestep(self, arg1: float) -> None:
pass
pass
def _unload() -> None:
pass
def get_default_material() -> PhysxMaterial:
pass
def set_default_material(static_friction: float, dynamic_friction: float, restitution: float) -> None:
Expand Down
5 changes: 3 additions & 2 deletions python/pybind/physx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,6 @@ template <> struct type_caster<physx::PxForceMode::Enum> {
Generator<int> init_physx(py::module &sapien) {
auto m = sapien.def_submodule("physx");

m.def("_unload", []() { MeshManager::Clear(); });

auto PyPhysxSceneConfig = py::class_<PhysxSceneConfig>(m, "PhysxSceneConfig");
PyPhysxSceneConfig.def(py::init<>())
.def_readwrite("gravity", &PhysxSceneConfig::gravity)
Expand Down Expand Up @@ -855,4 +853,7 @@ set some motion axes of the dynamic rigid body to be locked
.def("get_default_material", &PhysxDefault::getDefaultMaterial);

////////// end global //////////

auto atexit = py::module_::import("atexit");
atexit.attr("register")(py::cpp_function([]() { MeshManager::Clear(); }));
}

0 comments on commit 628d09e

Please sign in to comment.