jupyter-widgets / pythreejs

A Jupyter - Three.js bridge
https://pythreejs.readthedocs.io
Other
942 stars 188 forks source link

How to get the device PixelRatio from the renderer? #357

Open nvaytet opened 3 years ago

nvaytet commented 3 years ago

I can see in the threejs docs that the renderer has the getPixelRatio() method (https://threejs.org/docs/#api/en/renderers/WebGLRenderer.getPixelRatio)

How can I access this property from the pythreejs renderer? It has no such method, but maybe there is a method that allows getting the properties from the underlying js object? By giving it the property name as a string maybe? For example renderer.get_property("PixelRatio") or renderer.call("getPixelRatio")?

Many thanks for any help.

vidartf commented 2 years ago

Sorry for the lack of response here, but the official answer is that it isn't directly supported. However, you can potentially get this by calling renderer.exec_three_obj_method('getPixelRatio). It is however not so easy to get the return value. You can either patch the renderer._on_ret_val method, or subclass the renderer class and override that method, but in both cases the reply will be asynchronous (i.e. it will arrive at a random time after the request was sent, typically after the cell has finished executing).

This is an inherent issue in ipywidgets, where it isn't possible to block the execution while waiting for a reply, since the message loop needs to continue running in order to process the incoming response. There are some packages that try to solve this, but they are not very robust.

But if you are ok with that code being "event based" then this patter could work well.