Giter Site home page Giter Site logo

meshrender's Introduction

meshrender

NOTE: SUPPORT FOR THIS REPOSITORY IS BEING ENDED SOON. BUT FEAR NOT, THERE IS A (MUCH BETTER) REPLACEMENT!

I've written a much better GLTF 2.0 compliant renderer that can do shadows, textures, and physically-based rendering. Check it out here: https://github.com/mmatl/pyrender

A simple, OpenGL 3+ based Python scene renderer

Build Status

This package, which is build on top of PyOpenGL, is designed to make it easy to render images of 3D scenes in pure Python. It supports a scene abstraction and allows users to specify material properties, camera intrinsics, and lighting.

Extensive API documentation is provided here, but an example of using the library to render color and depth images is shown below.

    import numpy as np
    import trimesh
    from autolab_core import RigidTransform
    from perception import CameraIntrinsics, RenderMode

    from meshrender import Scene, MaterialProperties, AmbientLight, PointLight, SceneObject, VirtualCamera

    # Start with an empty scene
    scene = Scene()

    #====================================
    # Add objects to the scene
    #====================================

    # Begin by loading meshes
    cube_mesh = trimesh.load_mesh('cube.obj')
    sphere_mesh = trimesh.load_mesh('sphere.obj')

    # Set up each object's pose in the world
    cube_pose = RigidTransform(
        rotation=np.eye(3),
        translation=np.array([0.0, 0.0, 0.0]),
        from_frame='obj',
        to_frame='world'
    )
    sphere_pose = RigidTransform(
        rotation=np.eye(3),
        translation=np.array([1.0, 1.0, 0.0]),
        from_frame='obj',
        to_frame='world'
    )

    # Set up each object's material properties
    cube_material = MaterialProperties(
        color = np.array([0.1, 0.1, 0.5]),
        k_a = 0.3,
        k_d = 1.0,
        k_s = 1.0,
        alpha = 10.0,
        smooth=False
    )
    sphere_material = MaterialProperties(
        color = np.array([0.1, 0.1, 0.5]),
        k_a = 0.3,
        k_d = 1.0,
        k_s = 1.0,
        alpha = 10.0,
        smooth=True
    )

    # Create SceneObjects for each object
    cube_obj = SceneObject(cube_mesh, cube_pose, cube_material)
    sphere_obj = SceneObject(sphere_mesh, sphere_pose, sphere_material)

    # Add the SceneObjects to the scene
    scene.add_object('cube', cube_obj)
    scene.add_object('sphere', sphere_obj)

    #====================================
    # Add lighting to the scene
    #====================================

    # Create an ambient light
    ambient = AmbientLight(
        color=np.array([1.0, 1.0, 1.0]),
        strength=1.0
    )

    # Create a point light
    point = PointLight(
        location=np.array([1.0, 2.0, 3.0]),
        color=np.array([1.0, 1.0, 1.0]),
        strength=10.0
    )

    # Add the lights to the scene
    scene.ambient_light = ambient # only one ambient light per scene
    scene.add_light('point_light_one', point)

    #====================================
    # Add a camera to the scene
    #====================================

    # Set up camera intrinsics
    ci = CameraIntrinsics(
        frame = 'camera',
        fx = 525.0,
        fy = 525.0,
        cx = 319.5,
        cy = 239.5,
        skew=0.0,
        height=480,
        width=640
    )

    # Set up the camera pose (z axis faces away from scene, x to right, y up)
    cp = RigidTransform(
        rotation = np.array([
            [0.0, 0.0, -1.0],
            [0.0, 1.0,  0.0],
            [1.0, 0.0,  0.0]
        ]),
        translation = np.array([-0.3, 0.0, 0.0]),
        from_frame='camera',
        to_frame='world'
    )

    # Create a VirtualCamera
    camera = VirtualCamera(ci, cp)

    # Add the camera to the scene
    scene.camera = camera

    #====================================
    # Render images
    #====================================

    # Render raw numpy arrays containing color and depth
    color_image_raw, depth_image_raw = scene.render(render_color=True)

    # Alternatively, just render a depth image
    depth_image_raw = scene.render(render_color=False)

    # Alternatively, collect wrapped images
    wrapped_color, wrapped_depth, wrapped_segmask = scene.wrapped_render(
        [RenderMode.COLOR, RenderMode.DEPTH, RenderMode.SEGMASK]
    )

meshrender's People

Contributors

jeffmahler avatar mmatl avatar rickstaa avatar visatish avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

meshrender's Issues

PyPI version bump?

The version that's in PyPI has a serious memory leak when you render multiple times in one script.

This problem is fixed in master, though.

Vertex / Face color on same mesh

Good afternoon,

Thank you very much for putting this library together, it is very convinient and useful. I'm now interested in being able to set an independant color to each of the triangles of my mesh. However, it is said in the documentation that this feature is not yet officially available. Is there a workaround I can implement in order to be able to do so?

I Thank you in advance for you answer.

Failed to run example code: 'NoneType' object is not iterable

Hi. When I ran the example code in README, it showed

WARNING:root:Failed to import geometry msgs in rigid_transformations.py.
WARNING:root:Failed to import ros dependencies in rigid_transforms.py
WARNING:root:autolab_core not installed as catkin package, RigidTransform ros methods will be unavailable
Unable to import DataStreamSyncer and Recorder! Likely due to missing multiprocess
WARNING:root:autolab_perception is not installed as a catkin package - ROS msg conversions will not be available for image wrappers
WARNING:root:autolab_perception is not installed as a catkin package - ROS msg conversions will not be available for image wrappers
/usr/local/lib/python3.5/dist-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
WARNING:root:Unable to import pylibfreenect2. Python-only Kinect driver may not work properly.
WARNING:root:Unable to import Primsense sensor modules! Likely due to missing OpenNI2.
WARNING:root:Failed to import ROS in ensenso_sensor.py. ROS functionality not available
WARNING:root:Unable to import generic sensor modules!.
Exception ignored in: <bound method Scene.__del__ of <meshrender.scene.Scene object at 0x7f005f878e80>>
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/meshrender-0.0.6-py3.5.egg/meshrender/scene.py", line 284, in __del__
  File "/usr/local/lib/python3.5/dist-packages/meshrender-0.0.6-py3.5.egg/meshrender/scene.py", line 281, in close
  File "/usr/local/lib/python3.5/dist-packages/meshrender-0.0.6-py3.5.egg/meshrender/scene.py", line 169, in close_renderer
  File "/usr/local/lib/python3.5/dist-packages/meshrender-0.0.6-py3.5.egg/meshrender/render.py", line 222, in close
  File "/home/meng/meshrender/.eggs/PyOpenGL-3.1.0-py3.5.egg/OpenGL/platform/baseplatform.py", line 401, in __call__
  File "/home/meng/meshrender/.eggs/PyOpenGL-3.1.0-py3.5.egg/OpenGL/platform/baseplatform.py", line 381, in load
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 954, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 887, in _find_spec
TypeError: 'NoneType' object is not iterable
Exception ignored in: <bound method OpenGLRenderer.__del__ of <meshrender.render.OpenGLRenderer object at 0x7effbc333cf8>>
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/meshrender-0.0.6-py3.5.egg/meshrender/render.py", line 583, in __del__
  File "/usr/local/lib/python3.5/dist-packages/meshrender-0.0.6-py3.5.egg/meshrender/render.py", line 222, in close
  File "/home/meng/meshrender/.eggs/PyOpenGL-3.1.0-py3.5.egg/OpenGL/platform/baseplatform.py", line 401, in __call__
  File "/home/meng/meshrender/.eggs/PyOpenGL-3.1.0-py3.5.egg/OpenGL/platform/baseplatform.py", line 381, in load
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 954, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 887, in _find_spec
TypeError: 'NoneType' object is not iterable

I used sudo python3 setup.py install to install this package under ubuntu16.04. I could not find cube.obj or sphere.obj so I downloaded from other repos(http://people.sc.fsu.edu/~jburkardt/data/obj/cube.obj, https://github.com/SaschaWillems/Vulkan/blob/master/data/models/sphere.obj). And I tried to plot the images generated, but I could not see anything other than background. I thought there could be sth wrong with PyOpenGL (mine is 3.1.0), but I don't know how to solve it. Thanks for your help.

Fail to install

Hello. Many thanks for your code. However, when I am trying to install, I meet the errors below. May you give some hints to solve it? Many thanks.

pip install mesh-renderer
Collecting mesh-renderer
Using cached mesh_renderer-1.4.tar.gz (21 kB)
Building wheels for collected packages: mesh-renderer
Building wheel for mesh-renderer (setup.py) ... error
ERROR: Command errored out with exit status 1:
command: /home/yzx/PycharmProjects/Myenv/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-wvqwbrng/mesh-renderer/setup.py'"'"'; file='"'"'/tmp/pip-install-wvqwbrng/mesh-renderer/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-pp6pkq6k
cwd: /tmp/pip-install-wvqwbrng/mesh-renderer/
Complete output (39 lines):
running bdist_wheel
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.8
copying mesh_renderer.py -> build/lib.linux-x86_64-3.8
copying camera_utils.py -> build/lib.linux-x86_64-3.8
copying rasterize_triangles.py -> build/lib.linux-x86_64-3.8
running build_ext
2020-08-26 17:40:41.943057: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1
Traceback (most recent call last):
File "", line 1, in
File "/tmp/pip-install-wvqwbrng/mesh-renderer/setup.py", line 108, in
setup(
File "/home/yzx/PycharmProjects/Myenv/lib/python3.8/site-packages/setuptools/init.py", line 165, in setup
return distutils.core.setup(**attrs)
File "/usr/lib/python3.8/distutils/core.py", line 148, in setup
dist.run_commands()
File "/usr/lib/python3.8/distutils/dist.py", line 966, in run_commands
self.run_command(cmd)
File "/usr/lib/python3.8/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/usr/lib/python3/dist-packages/wheel/bdist_wheel.py", line 223, in run
self.run_command('build')
File "/usr/lib/python3.8/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/usr/lib/python3.8/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/usr/lib/python3.8/distutils/command/build.py", line 135, in run
self.run_command(cmd_name)
File "/usr/lib/python3.8/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/usr/lib/python3.8/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/usr/lib/python3.8/distutils/command/build_ext.py", line 340, in run
self.build_extensions()
File "/tmp/pip-install-wvqwbrng/mesh-renderer/setup.py", line 70, in build_extensions
assert tf_version_major == 1
AssertionError

ERROR: Failed building wheel for mesh-renderer
Running setup.py clean for mesh-renderer
Failed to build mesh-renderer
Installing collected packages: mesh-renderer
Running setup.py install for mesh-renderer ... error
ERROR: Command errored out with exit status 1:
command: /home/yzx/PycharmProjects/Myenv/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-wvqwbrng/mesh-renderer/setup.py'"'"'; file='"'"'/tmp/pip-install-wvqwbrng/mesh-renderer/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' install --record /tmp/pip-record-z356jsnx/install-record.txt --single-version-externally-managed --compile --install-headers /home/yzx/PycharmProjects/Myenv/include/site/python3.8/mesh-renderer
cwd: /tmp/pip-install-wvqwbrng/mesh-renderer/
Complete output (41 lines):
running install
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.8
copying mesh_renderer.py -> build/lib.linux-x86_64-3.8
copying camera_utils.py -> build/lib.linux-x86_64-3.8
copying rasterize_triangles.py -> build/lib.linux-x86_64-3.8
running build_ext
2020-08-26 17:40:43.783594: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1
Traceback (most recent call last):
File "", line 1, in
File "/tmp/pip-install-wvqwbrng/mesh-renderer/setup.py", line 108, in
setup(
File "/home/yzx/PycharmProjects/Myenv/lib/python3.8/site-packages/setuptools/init.py", line 165, in setup
return distutils.core.setup(**attrs)
File "/usr/lib/python3.8/distutils/core.py", line 148, in setup
dist.run_commands()
File "/usr/lib/python3.8/distutils/dist.py", line 966, in run_commands
self.run_command(cmd)
File "/usr/lib/python3.8/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/home/yzx/PycharmProjects/Myenv/lib/python3.8/site-packages/setuptools/command/install.py", line 61, in run
return orig.install.run(self)
File "/usr/lib/python3.8/distutils/command/install.py", line 589, in run
self.run_command('build')
File "/usr/lib/python3.8/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/usr/lib/python3.8/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/usr/lib/python3.8/distutils/command/build.py", line 135, in run
self.run_command(cmd_name)
File "/usr/lib/python3.8/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/usr/lib/python3.8/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/usr/lib/python3.8/distutils/command/build_ext.py", line 340, in run
self.build_extensions()
File "/tmp/pip-install-wvqwbrng/mesh-renderer/setup.py", line 70, in build_extensions
assert tf_version_major == 1
AssertionError
----------------------------------------
ERROR: Command errored out with exit status 1: /home/yzx/PycharmProjects/Myenv/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-wvqwbrng/mesh-renderer/setup.py'"'"'; file='"'"'/tmp/pip-install-wvqwbrng/mesh-renderer/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' install --record /tmp/pip-record-z356jsnx/install-record.txt --single-version-externally-managed --compile --install-headers /home/yzx/PycharmProjects/Myenv/include/site/python3.8/mesh-renderer Check the logs for full command output.

RuntimeError: implement_array_function method already has a docstring

RuntimeError: implement_array_function method already has a docstring
============================================================================
Edit setup.cfg to change the build options

BUILDING MATPLOTLIB
            matplotlib: yes [2.2.0]
                python: yes [3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019,
                        21:26:53) [MSC v.1916 32 bit (Intel)]]
              platform: yes [win32]

REQUIRED DEPENDENCIES AND EXTENSIONS
----------------------------------------

ERROR: Command "python setup.py egg_info" failed with error code 1 in C:\Users\fan8\AppData\Local\Temp\pip-install-s6y0hc8w\meshrender\

ValueError: Meshrender requires OpenGL 3+!

Hi
After installing meshreder, I got the following warning when run my code.

-- End pasted text --

WARNING:root:Failed to import geometry msgs in rigid_transformations.py.
WARNING:root:Failed to import ros dependencies in rigid_transforms.py
WARNING:root:autolab_core not installed as catkin package, RigidTransform ros methods will be unavailable
Unable to import DataStreamSyncer and Recorder! Likely due to missing multiprocess
WARNING:root:autolab_perception is not installed as a catkin package - ROS msg conversions will not be available for image wrappers
WARNING:root:autolab_perception is not installed as a catkin package - ROS msg conversions will not be available for image wrappers
WARNING:root:autolab_perception is not installed as a catkin package - ROS msg conversions will not be available for image wrappers
WARNING:root:Unable to import pylibfreenect2. Python-only Kinect driver may not work properly.
WARNING:root:Failed to import ROS in Kinect2_sensor.py. Kinect will not be able to be used in bridged mode
WARNING:root:Unable to import openni2 driver. Python-only Primesense driver may not work properly
WARNING:root:Failed to import ROS in primesense_sensor.py. ROS functionality not available
WARNING:root:primesense_sensor.py not installed as catkin package. ROS functionality not available.
WARNING:root:Failed to import ROS in ensenso_sensor.py. ROS functionality not available
WARNING:root:Failed to import ROS in phoxi_sensor.py. PhoXiSensor functionality unavailable.
WARNING:root:Unable to import weight sensor modules!

When I try to render my scence, I got the error...Actually I already installed OpenGL. Thanks for your help.
97 # Render raw numpy arrays containing color and depth
---> 98 color_image_raw, depth_image_raw = scene.render(render_color=True)
99
100 # Alternatively, just render a depth image

C:\Users\liu\Anaconda3\lib\site-packages\meshrender\scene.py in render(self, render_color, front_and_back)
209 raise ValueError('scene.camera must be set before calling render()')
210 if self._renderer is None:
--> 211 self._renderer = OpenGLRenderer(self)
212 return self._renderer.render(render_color, front_and_back=front_and_back)
213

C:\Users\liu\Anaconda3\lib\site-packages\meshrender\render.py in init(self, scene)
52 self._window = pyglet.window.Window(config=conf, visible=False, resizable=False, width=1, height=1)
53 except:
---> 54 raise ValueError('Meshrender requires OpenGL 3+!')
55
56 # Bind the frame buffer for offscreen rendering

ValueError: Meshrender requires OpenGL 3+!

Shouyang

importing duration

First, thanks for this nice tool. I have a minor improvement request. The import times are currently quite long, on my system around 3 seconds. It seems like a lot of modules are being imported or checked in by autolab_core and perception. A dependency reduction would be nice.

Getting Material properties from a .mtl file

Hello Team,

Thank you very much for putting up this library and it was very helpful to learn about object rendering. But I have a task in which i must be able to get the material properties from a .mtl file which will be referred in the .obj file. In your library I find that, It is yet to be implemented. Can i get some ideas, as in how we can get the material properties from a .mtl file to make my scene look realistic.

Any kind of help would be greatly appreciated.

Thanking you,
Vinay

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.