def test_reading_obj_with_mtl_from_sc_file(self, mock_sc, mock_s3_open): from baiji.pod.asset_cache import CacheFile sc_obj_with_texure = self.obj_with_texure.replace( "s3://bodylabs-korper-assets", '') sc_obj_with_texure_mtl = self.obj_with_texure_mtl.replace( "s3://bodylabs-korper-assets", '') sc_obj_with_texure_tex = self.obj_with_texure_tex.replace( "s3://bodylabs-korper-assets", '') bucket = "bodylabs-korper-assets" m = obj.load(sc(sc_obj_with_texure, bucket=bucket)) mock_sc.assert_has_calls([ mock.call(sc_obj_with_texure, bucket=bucket), # the one above mock.call( CacheFile(sc, sc_obj_with_texure_mtl, bucket=bucket).local), # in obj.load mock.call( CacheFile(sc, sc_obj_with_texure_tex, bucket=bucket).local), # in obj.load ]) mock_s3_open.assert_has_calls([ mock.call(sc(sc_obj_with_texure, bucket=bucket), 'rb'), mock.call(sc(sc_obj_with_texure_mtl, bucket=bucket), 'r'), ]) self.assertEqual(m.materials_filepath, sc(sc_obj_with_texure_mtl, bucket=bucket)) self.assertEqual(m.texture_filepath, sc(sc_obj_with_texure_tex, bucket=bucket))
def test_writing_obj_with_mtl(self): local_file = os.path.join(self.tmp_dir, "test_writing_obj_with_mtl.obj") m = obj.load(sc(self.obj_with_texure)) obj.dump(m, local_file) self.assertTrue(s3.exists(os.path.splitext(local_file)[0] + '.mtl')) self.assertTrue(s3.exists(os.path.splitext(local_file)[0] + '.png'))
def test_keep_segments(self): mesh = obj.load( vc('/templates/cached_model_templates/sm_2013_f_0005.obj')) expected_parts = [ 'rightCalf', 'head', 'rightHand', 'leftTorso', 'midsection', 'leftFoot', 'rightTorso', 'rightThigh', 'leftCalf', 'rightShoulder', 'leftShoulder', 'leftThigh', 'pelvis', 'leftForearm', 'rightFoot', 'leftHand', 'rightUpperArm', 'rightForearm', 'leftUpperArm' ] self.assertEqual(set(mesh.segm.keys()), set(expected_parts)) self.assertEqual(len(mesh.segm['rightFoot']), 3336) self.assertEqual(len(mesh.segm['leftFoot']), 3336) segments_to_keep = ['leftFoot', 'rightFoot'] mesh.keep_segments(segments_to_keep) self.assertEqual(len(mesh.f), 6672) self.assertEqual(len(mesh.segm['rightFoot']), 3336) self.assertEqual(len(mesh.segm['leftFoot']), 3336) self.assertEqual(set(mesh.segm.keys()), set(segments_to_keep)) max_f_index = np.max(mesh.segm.values()) self.assertEqual(max_f_index, mesh.f.shape[0] - 1)
def test_keep_vertices_with_empty_list_does_not_warn(self, warn): mesh = obj.load( vc('/templates/cached_model_templates/sm_2013_f_0005.obj')) mesh.keep_vertices([]) self.assertFalse(warn.called)
def test_loading_vertex_colors(self): # Mesh without vertex colors should not have vertex colors mesh_without_vertex_colors = obj.load(sc(self.test_obj_url)) self.assertIsNone(mesh_without_vertex_colors.vc) # Mesh with vertex colors should have vertex colors mesh_with_vertex_colors = obj.load( sc(self.test_obj_with_vertex_colors_url)) self.assertIsNotNone(mesh_with_vertex_colors.vc) # Check sizes vc_length, vc_size = mesh_with_vertex_colors.vc.shape v_length, _ = mesh_with_vertex_colors.v.shape self.assertEqual(vc_length, v_length) self.assertEqual(vc_size, 3) # Vertices should be the same self.assertTrue( (mesh_without_vertex_colors.v == mesh_with_vertex_colors.v).all())
def test_reading_obj_with_mtl_from_s3_url(self, mock_s3_open): skip_if_unavailable('s3') m = obj.load(self.obj_with_texure) mock_s3_open.assert_has_calls([ mock.call(self.obj_with_texure, 'rb'), mock.call(self.obj_with_texure_mtl, 'r'), ]) self.assertEqual(m.materials_filepath, self.obj_with_texure_mtl) self.assertEqual(m.texture_filepath, self.obj_with_texure_tex) self.assertIsNotNone(m.texture_image)
def test_loading_brings_in_normals_and_uvs(self): # This file is known to have vt, vn, and faces of the form 1/2/3 texture_template = 's3://bodylabs-korper-assets/is/ps/shared/data/body/template/texture_coordinates/textured_template_low_v2.obj' mesh_with_texture = obj.load(sc(texture_template)) self.assertIsNotNone(mesh_with_texture.vt) self.assertIsNotNone(mesh_with_texture.ft) self.assertEqual(mesh_with_texture.vt.shape[1], 2) self.assertEqual(mesh_with_texture.vt.shape[0], np.max(mesh_with_texture.ft) + 1) self.assertIsNotNone(mesh_with_texture.vn) self.assertIsNotNone(mesh_with_texture.fn) self.assertEqual(mesh_with_texture.vn.shape[1], 3) self.assertEqual(mesh_with_texture.vn.shape[0], np.max(mesh_with_texture.fn) + 1)
def test_keep_vertices_without_f(self): mesh = obj.load( vc('/templates/cached_model_templates/sm_2013_f_0005.obj')) mesh.segm = None mesh.f = None indices_to_keep = [1, 2, 3, 5, 8, 273, 302, 11808, 11847, 12031, 12045] expected_verts = mesh.v[indices_to_keep] mesh.keep_vertices(indices_to_keep) np.testing.assert_array_equal(mesh.v, expected_verts) self.assertIs(mesh.f, None)
def test_keep_vertices_without_segm(self): mesh = obj.load( vc('/templates/cached_model_templates/sm_2013_f_0005.obj')) mesh.segm = None indices_to_keep, expected_verts, expected_face_vertices = self.indicies_for_testing_keep_vertices( mesh) mesh.keep_vertices(indices_to_keep) np.testing.assert_array_equal(mesh.v, expected_verts) np.testing.assert_array_equal(mesh.v[mesh.f.flatten()], expected_face_vertices) max_v_index = np.max(mesh.f.flatten()) self.assertLessEqual(max_v_index, mesh.v.shape[0] - 1)
def test_reading_obj_with_mtl_from_local_file(self, mock_s3_open): local_obj_with_texure = os.path.join( self.tmp_dir, os.path.basename(self.obj_with_texure)) local_obj_with_texure_mtl = os.path.join( self.tmp_dir, os.path.basename(self.obj_with_texure_mtl)) local_obj_with_texure_tex = os.path.join( self.tmp_dir, os.path.basename(self.obj_with_texure_tex)) s3.cp(sc(self.obj_with_texure), local_obj_with_texure) s3.cp(sc(self.obj_with_texure_mtl), local_obj_with_texure_mtl) s3.cp(sc(self.obj_with_texure_tex), local_obj_with_texure_tex) m = obj.load(local_obj_with_texure) mock_s3_open.assert_has_calls([ mock.call(local_obj_with_texure, 'rb'), mock.call(local_obj_with_texure_mtl, 'r'), ]) self.assertEqual(m.materials_filepath, local_obj_with_texure_mtl) self.assertEqual(m.texture_filepath, local_obj_with_texure_tex)
def test_keep_vertices(self): mesh = obj.load( vc('/templates/cached_model_templates/sm_2013_f_0005.obj')) # set vc and vc for completeness mesh.set_vertex_colors("blue") mesh.reset_normals() indices_to_keep, expected_verts, expected_face_vertices = self.indicies_for_testing_keep_vertices( mesh) mesh.keep_vertices(indices_to_keep) np.testing.assert_array_equal(mesh.v, expected_verts) np.testing.assert_array_equal(mesh.v[mesh.f.flatten()], expected_face_vertices) max_v_index = np.max(mesh.f.flatten()) self.assertLessEqual(max_v_index, mesh.v.shape[0] - 1)
def test_texture_reads_Kd(self): obj_path = self.create_texture_test_files(include_Kd=True) m = obj.load(obj_path) self.assertEqual(m.texture_filepath, os.path.join(self.scratch_dir, 'diffuse_tex.png'))
def test_changing_texture_filepath(self): m = obj.load(self.obj_with_texure) self.assertEqual(m.texture_filepath, self.obj_with_texure_tex) self.assertIsNotNone(m.texture_image) m.texture_filepath = None self.assertIsNone(m.texture_image)