def __init__(self, width=1200, height=800, use_offscreen=True): #super().__init__() self.width, self.height = width, height self.use_offscreen = use_offscreen self.render_wireframe = False self.mat_constructor = pyrender.MetallicRoughnessMaterial self.trimesh_to_pymesh = pyrender.Mesh.from_trimesh self.scene = pyrender.Scene(bg_color=colors['white'], ambient_light=(0.3, 0.3, 0.3)) pc = pyrender.PerspectiveCamera(yfov=np.pi / 3.0, aspectRatio=float(width) / height) camera_pose = np.eye(4) camera_pose[:3, 3] = np.array([0, 0, 3.0]) self.camera_node = self.scene.add(pc, pose=camera_pose, name='pc-camera') self.figsize = (width, height) if self.use_offscreen: self.viewer = pyrender.OffscreenRenderer(*self.figsize) self.use_raymond_lighting(4.) else: self.viewer = Viewer(self.scene, use_raymond_lighting=True, viewport_size=self.figsize, cull_faces=False, run_in_thread=True)
def view_3d_scene(self): """ Render the scene in a 3D viewer. """ if self.state is None or self.camera is None: raise ValueError('Cannot render 3D scene before state is set! You can set the state with the reset() function') Viewer(self.scene, use_raymond_lighting=True)
def quick_color_visualize(): scene = Scene(bg_color=[0, 0, 0]) root = os.path.expanduser('~') mesh_path = '.keras/paz/datasets/ycb_models/035_power_drill/textured.obj' path = os.path.join(root, mesh_path) mesh = color_object(path) scene.add(mesh) Viewer(scene, use_raymond_lighting=True, flags=RenderFlags.FLAT)
def start(self): if self._physics_client is None: self._physics_client = pybullet.connect(pybullet.DIRECT) pybullet.setGravity(0, 0, -GRAVITY_ACCEL, physicsClientId=self._physics_client) self._key_to_id = {} self._key_to_com = {} if self._debug: self._create_scene() self._viewer = Viewer(self._scene, use_raymond_lighting=True, run_in_thread=True)
def run(self): """ """ self.viewer = Viewer(self.scene, use_raymond_lighting=False, cull_faces=False, run_in_thread=True) threading.Thread(target=self.accept_messages).start()
def _run(kwargs): """Internal method that runs the viewer Parameters ---------- kwargs : dict Other keyword arguments for the Viewer instance. """ if 'use_raymond_lighting' not in kwargs: kwargs['use_raymond_lighting'] = True viewer = Viewer(Visualizer3D._scene, viewport_size=Visualizer3D._size, **kwargs) if viewer.viewer_flags['save_directory']: Visualizer3D._save_directory = viewer.viewer_flags[ 'save_directory'] Visualizer3D._kwargs = {} return viewer
def main(mesh_path): # rendering conf ambient_light = 0.8 directional_light = 1.0 img_res = 512 cam_f = 500 cam_c = img_res / 2.0 scene = Scene(ambient_light=np.array( [ambient_light, ambient_light, ambient_light, 1.0])) mesh_v, mesh_vc, mesh_f = load_ply_data(mesh_path) mesh_ = trimesh.Trimesh(vertices=mesh_v, faces=mesh_f, vertex_colors=mesh_vc) points_mesh = Mesh.from_trimesh(mesh_, smooth=True, material=None) mesh_node = scene.add(points_mesh) cam = IntrinsicsCamera(fx=cam_f, fy=cam_f, cx=cam_c, cy=cam_c) cam_pose = np.array([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 2.0], [0.0, 0.0, 0.0, 1.0]]) direc_l = DirectionalLight(color=np.ones(3), intensity=directional_light) light_node_1 = scene.add(direc_l, pose=np.matmul(rotationy(30), rotationx(45))) direc_l = DirectionalLight(color=np.ones(3), intensity=directional_light) light_node_2 = scene.add(direc_l, pose=np.matmul(rotationy(-30), rotationx(45))) direc_l = DirectionalLight(color=np.ones(3), intensity=directional_light) light_node_3 = scene.add(direc_l, pose=np.matmul(rotationy(-180), rotationx(45))) direc_l = DirectionalLight(color=np.ones(3), intensity=(directional_light - 0.5)) light_node_4 = scene.add(direc_l, pose=np.matmul(rotationy(0), rotationx(-10))) ################ # rendering cam_node = scene.add(cam, pose=cam_pose) render_flags = { 'flip_wireframe': False, 'all_wireframe': False, 'all_solid': False, 'shadows': True, 'vertex_normals': False, 'face_normals': False, 'cull_faces': True, 'point_size': 1.0, } viewer_flags = { 'mouse_pressed': False, 'rotate': False, 'rotate_rate': np.pi / 6.0, 'rotate_axis': np.array([0.0, 1.0, 0.0]), 'view_center': np.array([0.0, 0.0, 0.0]), 'record': False, 'use_raymond_lighting': False, 'use_direct_lighting': False, 'lighting_intensity': 3.0, 'use_perspective_cam': True, 'window_title': 'DIT', 'refresh_rate': 25.0, 'fullscreen': False, 'show_world_axis': False, 'show_mesh_axes': False, 'caption': None, 'save_one_frame': False, } v = Viewer(scene, viewport_size=(512, 512), render_flags=render_flags, viewer_flags=viewer_flags, run_in_thread=False) v.close()
viewer_flags = { 'mouse_pressed': False, 'rotate': False, 'rotate_rate': np.pi / 6.0, 'rotate_axis': np.array([0.0, 1.0, 0.0]), 'view_center': np.array([0.0, 0.0, 0.0]), 'record': False, 'use_raymond_lighting': False, 'use_direct_lighting': False, 'lighting_intensity': 3.0, 'use_perspective_cam': True, 'save_directory': '/home/wanglz14/Desktop/video/', 'window_title': 'FOV Human', 'refresh_rate': 10.0, 'fullscreen': False, 'show_world_axis': False, 'show_mesh_axes': False, 'caption': None } v = Viewer(scene, viewport_size=(800, 800), render_flags=render_flags, viewer_flags=viewer_flags, run_in_thread=True) mesh = trimesh.load('../results/test/01.ply') points_mesh = Mesh.from_trimesh(mesh, smooth=True) v._render_lock.acquire() scene.remove_node(human_node) human_node = scene.add(points_mesh) v._render_lock.release()
from pyrender import Mesh, Scene, Viewer from io import BytesIO import numpy as np import trimesh import requests import time duck_source = "https://github.com/KhronosGroup/glTF-Sample-Models/raw/master/2.0/Duck/glTF-Binary/Duck.glb" duck = trimesh.load(BytesIO(requests.get(duck_source).content), file_type='glb') duckmesh = Mesh.from_trimesh(list(duck.geometry.values())[0]) scene = Scene(ambient_light=np.array([1.0, 1.0, 1.0, 1.0])) scene.add(duckmesh) orig_positions = duckmesh.primitives[0].positions vwr = Viewer(scene, run_in_thread=True, render_flags={ "cull_faces": False, "all_wireframe": False }, use_raymond_lighting=True, bg_color=[255, 0, 255]) while vwr.is_active: with vwr.render_lock: duckmesh.primitives[0].positions = orig_positions + np.random.randn( *orig_positions.shape) * 2 time.sleep(0.1)
scene.add_node(boxf_node) # ------------------------------------------------------------------------------ # By using the add() utility function # ------------------------------------------------------------------------------ drill_node = scene.add(drill_mesh, pose=drill_pose) bottle_node = scene.add(bottle_mesh, pose=bottle_pose) wood_node = scene.add(wood_mesh) direc_l_node = scene.add(direc_l, pose=cam_pose) spot_l_node = scene.add(spot_l, pose=cam_pose) # ============================================================================== # Using the viewer with a default camera # ============================================================================== v = Viewer(scene, shadows=True) # ============================================================================== # Using the viewer with a pre-specified camera # ============================================================================== cam_node = scene.add(cam, pose=cam_pose) v = Viewer(scene, central_node=drill_node) # ============================================================================== # Rendering offscreen from that camera # ============================================================================== r = OffscreenRenderer(viewport_width=640 * 2, viewport_height=480 * 2) color, depth = r.render(scene) import matplotlib.pyplot as plt
from io import BytesIO import numpy as np import requests import trimesh from pyrender import Mesh, Scene, Viewer duck_source = "https://github.com/KhronosGroup/glTF-Sample-Models/raw/master/2.0/Duck/glTF-Binary/Duck.glb" duck = trimesh.load(BytesIO(requests.get(duck_source).content), file_type="glb") duckmesh = Mesh.from_trimesh(list(duck.geometry.values())[0]) scene = Scene(ambient_light=np.array([1.0, 1.0, 1.0, 1.0])) scene.add(duckmesh) Viewer(scene)
file_type='glb') duckmesh = Mesh.from_trimesh(list(duck.geometry.values())[0]) # create all available backgrounds bg_idx = 0 bg_imgs = [ Image.open("./models/background.png"), Image.open("./models/wood_uv.png"), None ] background_texs = [ Texture(source=bg_img, source_channels="RGBA") if bg_img else None for bg_img in bg_imgs ] scene = Scene(ambient_light=np.array([1.0, 1.0, 1.0, 1.0]), bg_color=[255, 0, 255], background_texture=background_texs[0]) scene.add(duckmesh) def on_background_toggle(vwr): global bg_idx bg_idx = (bg_idx + 1) % 3 scene.background_texture = background_texs[bg_idx] Viewer(scene, registered_keys={"b": on_background_toggle}, run_in_thread=False, viewport_size=(640, 480))
# at_x, at_y, at_z = 0,0,0 # Create camera pose and add it to the scene new_campose, _ = lookAt(distance=distance, alpha=alpha, beta=beta, at_x=at_x, at_y=at_y, at_z=at_z) cam_node = scene.add(camera, pose=new_campose) # A light pointing from the camera's perspective could also be added # point_l_node = scene.add(point_l_cam, pose=new_campose) #============================================================================== # Use viewer to display scene #============================================================================== # View the scene view_render_flags = {'cull_faces': False, 'vertex_normals': False} v = Viewer(scene, render_flags=view_render_flags, viewport_size=(IMAGE_WIDTH, IMAGE_HEIGHT)) #============================================================================== # Prepare for dataset generation #============================================================================== flags = RenderFlags.SKIP_CULL_FACES r = OffscreenRenderer(viewport_width=IMAGE_WIDTH, viewport_height=IMAGE_HEIGHT) # color, _ = r.render(scene, flags=flags) # Generate dataset ? choice = input("Generate dataset? [y/n]: ") accepted_inputs = ['y','n'] while (not (choice in accepted_inputs)): choice = input("Generate dataset? [y/n]: ")
#============================================================================== scene = Scene(ambient_light=np.array([0.02, 0.02, 0.02, 1.0])) cam_node = scene.add(cam, pose=cam_pose) scene.main_camera_node = cam_node #============================================================================== drill_node = scene.add(drill_mesh) r = OffscreenRenderer(viewport_width=640, viewport_height=480) rf = pyrender.RenderFlags.NONE rf |= pyrender.RenderFlags.USE_RAW_DEPTH color, raw_depth = r.render(scene, flags=rf) r.delete() # unproject to get point cloud pcd = unproj(inv_mvp, raw_depth) #============================================================================== #------------------------------------------------------------------------------ # Creating meshes from point clouds #------------------------------------------------------------------------------ points_mesh = Mesh.from_points(pcd, colors=np.array((0.0, 1.0, 0.0, 1.0))) pcd_node = scene.add(points_mesh) #============================================================================== # Using the viewer with a pre-specified camera #============================================================================== v = Viewer(scene, render_flags={'point_size': 5})
vec = np.array([0, point_size_ndc]) vertices[0, 0:2] = vec vertices[1, 0:2] = _rot_mat(np.radians(120)) @ vec vertices[2, 0:2] = _rot_mat(np.radians(-120)) @ vec mat = MetallicRoughnessMaterial(doubleSided=True, baseColorFactor=[1.0, 0, 0, 1]) instances = [np.eye(4) for i in range(pts_2d.shape[0])] for i in range(pts_2d.shape[0]): instances[i][0, 3] = (2 * pts_2d[i][0] - width) / height instances[i][1, 3] = pts_2d[i][1] / height * 2 - 1 instances[i][2, 3] = -1 prim = Primitive(positions=vertices, mode=GLTF.TRIANGLES, material=mat, poses=instances) return Mesh(primitives=[prim]) pts_mesh = create_2d_point_mesh(640, 480, [[0, 0], [320, 240], [640, 480]], point_size_px=15) duck_node = scene.add(duckmesh) dn = scene.add_hud_node(Node(name="hud", mesh=pts_mesh)) Viewer(scene, run_in_thread=False, viewer_flags={"use_direct_lighting": True}, viewport_size=(640, 480))
e = subjectVertex if inside(e): if not inside(s): outputList.append(computeIntersection()) outputList.append(e) elif inside(s): outputList.append(computeIntersection()) s = e cp1 = cp2 return (outputList) # View the scene view_render_flags = {'cull_faces': False} v = Viewer(scene, viewport_size=(width, height), render_flags=view_render_flags) r = OffscreenRenderer(viewport_width=width, viewport_height=height) color, depth = r.render(scene, flags=RenderFlags.SKIP_CULL_FACES) # color = cvtColor(color, COLOR_RGB2BGR) def draw_point(img, imgpts): for point in imgpts: img = circle(img, tuple(point), 4, (255, 0, 0), 2) return img # draw the computed (x,y) coords on the image color = draw_point(color, points) plt.imshow(color)
obj_points_numpy) # Values close to 0 are 0 obj_points_numpy = obj_points_numpy.astype('float32') # Add spheres at the object points in obj_spoints # ball_trimesh = trimesh.creation.icosphere(subdivisions=3) # ball_mesh = Mesh.from_trimesh(ball_trimesh) # for row in obj_points: # scene.add(ball_mesh, pose=spatial_transform_Matrix(scale=0.005, t_x=row[0], t_y=row[1], t_z=row[2])) #============================================================================== # Attempt to convert 3D to 2D position #============================================================================== # View the scene view_render_flags = {'cull_faces': False} v = Viewer(scene, render_flags=view_render_flags) #============================================================================== # Rendering offscreen from that camera #============================================================================== dimensions = 600 # dimensions = 1280 r = OffscreenRenderer(viewport_width=dimensions, viewport_height=dimensions) # Generate dataset ? question_string = "Generate chess board dataset? (select no because it's already there) [y/n]: " choice = input(question_string) accepted_inputs = ['y', 'n'] while (not (choice in accepted_inputs)): choice = input(question_string)