Team Ai
Apppublic

OpenMotionLab/MotionGPT

sourceHugging Facemitupdated 1y agoView on Hugging Face
118likes
test_meshes.py134 linesDownload Raw Back to unit
1import numpy as np2import pytest3import trimesh4 5from pyrender import (Mesh, Primitive)6 7 8def test_meshes():9 10    with pytest.raises(TypeError):11        x = Mesh()12    with pytest.raises(TypeError):13        x = Primitive()14    with pytest.raises(ValueError):15        x = Primitive([], mode=10)16 17    # Basics18    x = Mesh([])19    assert x.name is None20    assert x.is_visible21    assert x.weights is None22 23    x.name = 'str'24 25    # From Trimesh26    x = Mesh.from_trimesh(trimesh.creation.box())27    assert isinstance(x, Mesh)28    assert len(x.primitives) == 129    assert x.is_visible30    assert np.allclose(x.bounds, np.array([31        [-0.5, -0.5, -0.5],32        [0.5, 0.5, 0.5]33    ]))34    assert np.allclose(x.centroid, np.zeros(3))35    assert np.allclose(x.extents, np.ones(3))36    assert np.allclose(x.scale, np.sqrt(3))37    assert not x.is_transparent38 39    # Test some primitive functions40    x = x.primitives[0]41    with pytest.raises(ValueError):42        x.normals = np.zeros(10)43    with pytest.raises(ValueError):44        x.tangents = np.zeros(10)45    with pytest.raises(ValueError):46        x.texcoord_0 = np.zeros(10)47    with pytest.raises(ValueError):48        x.texcoord_1 = np.zeros(10)49    with pytest.raises(TypeError):50        x.material = np.zeros(10)51    assert x.targets is None52    assert np.allclose(x.bounds, np.array([53        [-0.5, -0.5, -0.5],54        [0.5, 0.5, 0.5]55    ]))56    assert np.allclose(x.centroid, np.zeros(3))57    assert np.allclose(x.extents, np.ones(3))58    assert np.allclose(x.scale, np.sqrt(3))59    x.material.baseColorFactor = np.array([0.0, 0.0, 0.0, 0.0])60    assert x.is_transparent61 62    # From two trimeshes63    x = Mesh.from_trimesh([trimesh.creation.box(),64                          trimesh.creation.cylinder(radius=0.1, height=2.0)],65                          smooth=False)66    assert isinstance(x, Mesh)67    assert len(x.primitives) == 268    assert x.is_visible69    assert np.allclose(x.bounds, np.array([70        [-0.5, -0.5, -1.0],71        [0.5, 0.5, 1.0]72    ]))73    assert np.allclose(x.centroid, np.zeros(3))74    assert np.allclose(x.extents, [1.0, 1.0, 2.0])75    assert np.allclose(x.scale, np.sqrt(6))76    assert not x.is_transparent77 78    # From bad data79    with pytest.raises(TypeError):80        x = Mesh.from_trimesh(None)81 82    # With instancing83    poses = np.tile(np.eye(4), (5,1,1))84    poses[:,0,3] = np.array([0,1,2,3,4])85    x = Mesh.from_trimesh(trimesh.creation.box(), poses=poses)86    assert np.allclose(x.bounds, np.array([87        [-0.5, -0.5, -0.5],88        [4.5, 0.5, 0.5]89    ]))90    poses = np.eye(4)91    x = Mesh.from_trimesh(trimesh.creation.box(), poses=poses)92    poses = np.eye(3)93    with pytest.raises(ValueError):94        x = Mesh.from_trimesh(trimesh.creation.box(), poses=poses)95 96    # From textured meshes97    fm = trimesh.load('tests/data/fuze.obj')98    x = Mesh.from_trimesh(fm)99    assert isinstance(x, Mesh)100    assert len(x.primitives) == 1101    assert x.is_visible102    assert not x.is_transparent103    assert x.primitives[0].material.baseColorTexture is not None104 105    x = Mesh.from_trimesh(fm, smooth=False)106    fm.visual = fm.visual.to_color()107    fm.visual.face_colors = np.array([1.0, 0.0, 0.0, 1.0])108    x = Mesh.from_trimesh(fm, smooth=False)109    with pytest.raises(ValueError):110        x = Mesh.from_trimesh(fm, smooth=True)111 112    fm.visual.vertex_colors = np.array([1.0, 0.0, 0.0, 0.5])113    x = Mesh.from_trimesh(fm, smooth=False)114    x = Mesh.from_trimesh(fm, smooth=True)115    assert x.primitives[0].color_0 is not None116    assert x.is_transparent117 118    bm = trimesh.load('tests/data/WaterBottle.glb').dump()[0]119    x = Mesh.from_trimesh(bm)120    assert x.primitives[0].material.baseColorTexture is not None121    assert x.primitives[0].material.emissiveTexture is not None122    assert x.primitives[0].material.metallicRoughnessTexture is not None123 124    # From point cloud125    x = Mesh.from_points(fm.vertices)126 127# def test_duck():128#     bm = trimesh.load('tests/data/Duck.glb').dump()[0]129#     x = Mesh.from_trimesh(bm)130#     assert x.primitives[0].material.baseColorTexture is not None131#     pixel = x.primitives[0].material.baseColorTexture.source[100, 100]132#     yellowish = np.array([1.0, 0.7411765, 0.0, 1.0])133#     assert np.allclose(pixel, yellowish)134