Updated the post to also include how to use it in a published app
It is possible to export the geometry as image using trimesh. Try it here.
In order to do so add the following dependencies to your requirements.txt
file:
trimesh
pyglet<2.0.0
scipy
The following snippet can be used to export a TransformableObject
to a trimesh scene, rotate the camera and save as image. Please note that you can also use it on a Group
if you want to export a larger assembly.
def download_screenshot(self, params, **kwargs):
beam = RectangularExtrusion(width=10,
height=10,
line=Line(Point(0, 0, 0), Point(50, 0, 0)),
material=Material(color=Color.green()))
scene = beam._to_trimesh()
corners = np.concatenate(list(scene.bounds_corners.values()))
r_e = trimesh.transformations.euler_matrix(
math.radians(45),
math.radians(-45),
math.radians(0),
"ryxz",
)
transform = scene.camera.look_at(points=corners, distance=100, rotation=r_e)
scene.camera_transform = transform
png = scene.save_image(resolution=[1920, 1080], visible=False)
return DownloadResult(File.from_data(png), "screenshot.png")
You will need to do some additional modifications to be able to run this snippet in a published application. Add the following lines to the top of your imports to force pyglet to run in headless mode.
import pyglet
pyglet.options["headless"] = True
Additionally, you need the following dependencies to be installed during the publishing process by adding the following line to viktor.config.toml
packages = ["libglu1-mesa", "libegl1-mesa-dev"]
Please note that the _to_trimesh()
method is private, which means that it might change in a new SDK version without notice. Since this is an often requested feature, I didn’t want to wait sharing it with you. I am curious to hear if and how you are going to use it!
Regards,
Raoul