Skip to content

Commit e942516

Browse files
committed
STL scene loader
Now using trimesh
1 parent 86a07a3 commit e942516

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

demosys/conf/default.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
SCENE_LOADERS = (
7777
"demosys.loaders.scene.gltf.GLTF2",
7878
"demosys.loaders.scene.wavefront.ObjLoader",
79+
"demosys.loaders.scene.stl_loader.STLLoader",
7980
)
8081

8182
DATA_DIRS = ()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import gzip
2+
3+
import moderngl
4+
import numpy
5+
import trimesh
6+
7+
from demosys.loaders.scene.base import SceneLoader
8+
from demosys.opengl.vao import VAO
9+
from demosys.resources.meta import SceneDescription
10+
from demosys.scene import Material, MaterialTexture, Mesh, Node, Scene
11+
12+
13+
class STLLoader(SceneLoader):
14+
file_extensions = [
15+
['.stl'],
16+
['.stl', '.gz'],
17+
]
18+
19+
def load(self):
20+
path = self.find_scene(self.meta.path)
21+
if not path:
22+
raise ValueError("Scene '{}' not found".format(self.meta.path))
23+
24+
file_obj = str(path)
25+
if file_obj.endswith('.gz'):
26+
file_obj = gzip.GzipFile(file_obj)
27+
28+
stl_mesh = trimesh.load(file_obj, file_type='stl')
29+
scene = Scene(self.meta.resolved_path)
30+
scene_mesh = Mesh("mesh")
31+
scene_mesh.material = Material("default")
32+
33+
vao = VAO("mesh", mode=moderngl.TRIANGLES)
34+
vao.buffer(numpy.array(stl_mesh.vertices, dtype='f4'), '3f', ['in_position'])
35+
vao.buffer(numpy.array(stl_mesh.vertex_normals, dtype='f4'), '3f', ['in_normal'])
36+
vao.index_buffer(numpy.array(stl_mesh.faces, dtype='u4'))
37+
scene_mesh.vao = vao
38+
scene_mesh.add_attribute('POSITION', 'in_position', 3)
39+
scene_mesh.add_attribute('NORMAL', 'in_normal', 3)
40+
41+
scene.meshes.append(scene_mesh)
42+
scene.root_nodes.append(Node(mesh=scene_mesh))
43+
scene.prepare()
44+
45+
return scene

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
'pyrocket==0.2.8',
3434
'PyWavefront==1.0.2 ',
3535
'PyQt5==5.11.3',
36+
'trimesh==2.36.3',
3637
],
3738
extras_require={
3839
"glfw": ['glfw==1.7.0'],

0 commit comments

Comments
 (0)