def test_gltf(self): # split a multibody mesh into a scene scene = g.trimesh.scene.split_scene( g.get_mesh('cycloidal.ply')) # should be 117 geometries assert len(scene.geometry) >= 117 # a dict with {file name: str} export = scene.export('gltf') # load from just resolver r = g.trimesh.load(file_obj=None, file_type='gltf', resolver=export) # will assert round trip is roughly equal g.scene_equal(r, scene) # try loading from a ZIP archive zipped = g.trimesh.util.compress(export) r = g.trimesh.load( file_obj=g.trimesh.util.wrap_as_stream(zipped), file_type='zip') # try loading from a file name # will require a file path resolver with g.TemporaryDirectory() as d: for file_name, data in export.items(): with open(g.os.path.join(d, file_name), 'wb') as f: f.write(data) # load from file path of header GLTF rd = g.trimesh.load( g.os.path.join(d, 'model.gltf')) # will assert round trip is roughly equal g.scene_equal(rd, scene)
def test_export_path(self): m = g.get_mesh('fuze.obj') g.check_fuze(m) with g.TemporaryDirectory() as d: file_path = g.os.path.join(d, 'fz.obj') m.export(file_path) r = g.trimesh.load(file_path) g.check_fuze(r)
def test_obj_roundtrip(self): # get a zipped-DAE scene s = g.get_mesh('duck.zae', force='mesh') with g.TemporaryDirectory() as root: # export using a file path so it can auto-create # a FilePathResolver to write the stupid assets path = g.os.path.join(root, 'duck.obj') s.export(path) # bring it back from outer space rec = g.trimesh.load(path, force='mesh') assert rec.visual.uv.ptp(axis=0).ptp() > 1e-5 assert (s.visual.material.image.size == rec.visual.material.image.size)
def test_pbr_export(self): # try loading a textured box m = next(iter(g.get_mesh('BoxTextured.glb').geometry.values())) # make sure material copy doesn't crash m.visual.copy() with g.TemporaryDirectory() as d: # exports by path allow files to be written path = g.os.path.join(d, 'box.obj') m.export(path) # try reloading r = g.trimesh.load(path) # make sure material survived assert r.visual.material.image.size == (256, 256)
def test_merge_primitives_materials_roundtrip(self): # test to see if gltf loaded with `merge_primitives` # and then exported back # to gltf, produces a valid gltf. a = g.get_mesh('rgb_cube_with_primitives.gltf', merge_primitives=True) result = a.export(file_type='gltf', merge_buffers=True) with g.TemporaryDirectory() as d: for file_name, data in result.items(): with open(g.os.path.join(d, file_name), 'wb') as f: f.write(data) rd = g.trimesh.load(g.os.path.join(d, 'model.gltf'), merge_primitives=True) assert isinstance(rd, g.trimesh.Scene)
def test_write_dir(self): # try loading from a file name # will require a file path resolver original = g.get_mesh('fuze.obj') assert isinstance(original, g.trimesh.Trimesh) s = original.scene() with g.TemporaryDirectory() as d: path = g.os.path.join(d, 'heyy.gltf') s.export(file_obj=path) r = g.trimesh.load(path) assert isinstance(r, g.trimesh.Scene) assert len(r.geometry) == 1 m = next(iter(r.geometry.values())) assert g.np.isclose(original.area, m.area)
def test_export_concat(self): # Scenes exported in mesh formats should be # concatenating the meshes somewhere. original = g.trimesh.creation.icosphere(radius=0.123312) original_hash = original.identifier_md5 scene = g.trimesh.Scene() scene.add_geometry(original) with g.TemporaryDirectory() as d: for ext in ['stl', 'ply']: file_name = g.os.path.join(d, 'mesh.' + ext) scene.export(file_name) loaded = g.trimesh.load(file_name) assert g.np.isclose(loaded.volume, original.volume) # nothing should have changed assert original.identifier_md5 == original_hash