Giter VIP home page Giter VIP logo

Comments (8)

wilbown avatar wilbown commented on May 31, 2024 2

Hi, thanks for the response! I looked through pyglet code and you are right. I tried what you mentioned, but I had to make these changes in viewer.py. I am sure there is a better way than this though.

        self._is_active = True

        # if self.run_in_thread:
        #     self._thread = Thread(target=self._init_and_start_app)
        #     self._thread.start()
        # else:
        self._init_and_start_app()
        self.switch_to()
        self.set_caption(self.viewer_flags['window_title'])
        # pyglet.app.run()

    def start_app(self):
        pyglet.app.run()

    def _compute_initial_camera_pose(self):

And here is my working test code.

import trimesh
import pyrender
import numpy
import threading
import time

fuze_trimesh = trimesh.load('examples/models/fuze.obj')
mesh = pyrender.Mesh.from_trimesh(fuze_trimesh)
scene = pyrender.Scene()
scene.add(mesh)

v = pyrender.Viewer(scene, run_in_thread=True, use_raymond_lighting=True)

def event_loop():
  global v

  i = 0
  while v.is_active:
    pose = numpy.eye(4)
    pose[:3,3] = [i, 0, 0]
    v.render_lock.acquire()
    for n in v.scene.mesh_nodes:
      v.scene.set_pose(n, pose)
    v.render_lock.release()
    i += 0.01
    time.sleep(0.1)

t = threading.Thread(target=event_loop)
t.start()

v.start_app()

from pyrender.

andreiburov avatar andreiburov commented on May 31, 2024 2

Another approach could be to add an animation thread to the viewer.

viewer.py:

def __init__(self, scene, viewport_size=None,
                 render_flags=None, viewer_flags=None,
                 registered_keys=None, run_in_thread=False, AnimationThread=None, **kwargs):
...
    if AnimationThread is not None:
        animation_thread = AnimationThread(self._render_lock)
        animation_thread.start()

    # add animation logic before starting the app
    if self.run_in_thread:

test.py:

import threading

class AnimationThread(threading.Thread):

    def __init__(self, render_lock):
        threading.Thread.__init__(self)
        self._render_lock = render_lock

    def run(self):
        y, z = 0, 0
        while True:
            pose = T.euler_matrix(0, y, z)
            self._render_lock.acquire()
            scene.set_pose(mesh_node, pose)
            self._render_lock.release()
            y += 0.1
            z += 0.01
            time.sleep(1./24)

viewer = pyrender.Viewer(scene, viewport_size=(width, height), AnimationThread=AnimationThread)

@mmatl I have seen that you use the lower level _thread module, so maybe you could define an abstract class for this AnimationThread and then just give the user possibility to define the logic. I could do a pull request if you like the concept.

from pyrender.

mmatl avatar mmatl commented on May 31, 2024

Hi @wilbown Thanks for the issue! I don't have a windows machine, so I haven't been able to test this behavior yet. It seems like Pyglet explicitly disallows multithreading on windows, so you might be out of luck for the moment until I replace the windowing system with something better than pyglet :( I'll look into why pyglet does that, though.

from pyrender.

mmatl avatar mmatl commented on May 31, 2024

Actually, come to think of it, there is another option -- instead of calling the viewer with run_in_thread set, in your application, start a new thread to run your scene update code and run the viewer in normal mode in the main thread. This should work around your problem, although it's not as easy or clean.

from pyrender.

wilbown avatar wilbown commented on May 31, 2024

Maybe the idea would be to make Viewer thread safe in general (by taking out check for render_lock functioning) and a switch to get the object back and manually start the window. This might be useful for anyone who might want to run a separate thread for control, not just making work on Windows. But I could probably subclass as well.

from pyrender.

lisa676 avatar lisa676 commented on May 31, 2024

@wilbown @andreiburov I have pyglet 1.5.0, where I can find viewer.py file to change some code?

from pyrender.

3sigma avatar 3sigma commented on May 31, 2024

Hello,

I have just tested the suggestion of @wilbown. In fact you just need to run the update code in a thread, you don't need to modify viewer.py.
In short, this code is running:

import trimesh
import pyrender
import numpy
import threading
import time

fuze_trimesh = trimesh.load('examples/models/fuze.obj')
mesh = pyrender.Mesh.from_trimesh(fuze_trimesh)
scene = pyrender.Scene()
scene.add(mesh)

v = pyrender.Viewer(scene, run_in_thread=True, use_raymond_lighting=True)

def event_loop():
  global v

  i = 0
  while v.is_active:
    pose = numpy.eye(4)
    pose[:3,3] = [i, 0, 0]
    v.render_lock.acquire()
    for n in v.scene.mesh_nodes:
      v.scene.set_pose(n, pose)
    v.render_lock.release()
    i += 0.01
    time.sleep(0.1)

t = threading.Thread(target=event_loop)
t.start()

Nicolas

from pyrender.

bakwc avatar bakwc commented on May 31, 2024

On MacOS it's the same error.
It's really some kind of trolling - creating a rendering library without ability to update scene.

from pyrender.

Related Issues (20)

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.