OpenMotionLab/MotionGPT
118
1import numpy as np2import pytest3from trimesh import transformations4 5from pyrender import (DirectionalLight, PerspectiveCamera, Mesh, Node)6 7 8def test_nodes():9 10 x = Node()11 assert x.name is None12 assert x.camera is None13 assert x.children == []14 assert x.skin is None15 assert np.allclose(x.matrix, np.eye(4))16 assert x.mesh is None17 assert np.allclose(x.rotation, [0,0,0,1])18 assert np.allclose(x.scale, np.ones(3))19 assert np.allclose(x.translation, np.zeros(3))20 assert x.weights is None21 assert x.light is None22 23 x.name = 'node'24 25 # Test node light/camera/mesh tests26 c = PerspectiveCamera(yfov=2.0)27 m = Mesh([])28 d = DirectionalLight()29 x.camera = c30 assert x.camera == c31 with pytest.raises(TypeError):32 x.camera = m33 x.camera = d34 x.camera = None35 x.mesh = m36 assert x.mesh == m37 with pytest.raises(TypeError):38 x.mesh = c39 x.mesh = d40 x.light = d41 assert x.light == d42 with pytest.raises(TypeError):43 x.light = m44 x.light = c45 46 # Test transformations getters/setters/etc...47 # Set up test values48 x = np.array([1.0, 0.0, 0.0])49 y = np.array([0.0, 1.0, 0.0])50 t = np.array([1.0, 2.0, 3.0])51 s = np.array([0.5, 2.0, 1.0])52 53 Mx = transformations.rotation_matrix(np.pi / 2.0, x)54 qx = np.roll(transformations.quaternion_about_axis(np.pi / 2.0, x), -1)55 Mxt = Mx.copy()56 Mxt[:3,3] = t57 S = np.eye(4)58 S[:3,:3] = np.diag(s)59 Mxts = Mxt.dot(S)60 61 My = transformations.rotation_matrix(np.pi / 2.0, y)62 qy = np.roll(transformations.quaternion_about_axis(np.pi / 2.0, y), -1)63 Myt = My.copy()64 Myt[:3,3] = t65 66 x = Node(matrix=Mx)67 assert np.allclose(x.matrix, Mx)68 assert np.allclose(x.rotation, qx)69 assert np.allclose(x.translation, np.zeros(3))70 assert np.allclose(x.scale, np.ones(3))71 72 x.matrix = My73 assert np.allclose(x.matrix, My)74 assert np.allclose(x.rotation, qy)75 assert np.allclose(x.translation, np.zeros(3))76 assert np.allclose(x.scale, np.ones(3))77 x.translation = t78 assert np.allclose(x.matrix, Myt)79 assert np.allclose(x.rotation, qy)80 x.rotation = qx81 assert np.allclose(x.matrix, Mxt)82 x.scale = s83 assert np.allclose(x.matrix, Mxts)84 85 x = Node(matrix=Mxt)86 assert np.allclose(x.matrix, Mxt)87 assert np.allclose(x.rotation, qx)88 assert np.allclose(x.translation, t)89 assert np.allclose(x.scale, np.ones(3))90 91 x = Node(matrix=Mxts)92 assert np.allclose(x.matrix, Mxts)93 assert np.allclose(x.rotation, qx)94 assert np.allclose(x.translation, t)95 assert np.allclose(x.scale, s)96 97 # Individual element getters98 x.scale[0] = 099 assert np.allclose(x.scale[0], 0)100 101 x.translation[0] = 0102 assert np.allclose(x.translation[0], 0)103 104 x.matrix = np.eye(4)105 x.matrix[0,0] = 500106 assert x.matrix[0,0] == 1.0107 108 # Failures109 with pytest.raises(ValueError):110 x.matrix = 5 * np.eye(4)111 with pytest.raises(ValueError):112 x.matrix = np.eye(5)113 with pytest.raises(ValueError):114 x.matrix = np.eye(4).dot([5,1,1,1])115 with pytest.raises(ValueError):116 x.rotation = np.array([1,2])117 with pytest.raises(ValueError):118 x.rotation = np.array([1,2,3])119 with pytest.raises(ValueError):120 x.rotation = np.array([1,2,3,4])121 with pytest.raises(ValueError):122 x.translation = np.array([1,2,3,4])123 with pytest.raises(ValueError):124 x.scale = np.array([1,2,3,4])125 