diff --git a/doc/python/magnum.trade.rst b/doc/python/magnum.trade.rst index 30d3a86..b512cbe 100644 --- a/doc/python/magnum.trade.rst +++ b/doc/python/magnum.trade.rst @@ -95,6 +95,30 @@ .. py:function:: magnum.trade.AbstractImporter.open_file :raise RuntimeError: If file opening fails +.. py:property:: magnum.trade.AbstractImporter.mesh2d_count + :raise RuntimeError: If no file is opened +.. py:property:: magnum.trade.AbstractImporter.mesh3d_count + :raise RuntimeError: If no file is opened + +.. py:function:: magnum.trade.AbstractImporter.mesh2d_for_name + :raise RuntimeError: If no file is opened +.. py:function:: magnum.trade.AbstractImporter.mesh3d_for_name + :raise RuntimeError: If no file is opened + +.. py:function:: magnum.trade.AbstractImporter.mesh2d_name + :raise RuntimeError: If no file is opened + :raise ValueError: If :p:`id` is negative or not less than `mesh2d_count` +.. py:function:: magnum.trade.AbstractImporter.mesh3d_name + :raise RuntimeError: If no file is opened + :raise ValueError: If :p:`id` is negative or not less than `mesh3d_count` + +.. py:function:: magnum.trade.AbstractImporter.mesh2d + :raise RuntimeError: If no file is opened + :raise ValueError: If :p:`id` is negative or not less than `mesh2d_count` +.. py:function:: magnum.trade.AbstractImporter.mesh3d + :raise RuntimeError: If no file is opened + :raise ValueError: If :p:`id` is negative or not less than `mesh3d_count` + .. py:property:: magnum.trade.AbstractImporter.image1d_count :raise RuntimeError: If no file is opened .. py:property:: magnum.trade.AbstractImporter.image2d_count diff --git a/src/python/magnum/test/mesh.glb b/src/python/magnum/test/mesh.glb new file mode 100644 index 0000000..3a71a0c Binary files /dev/null and b/src/python/magnum/test/mesh.glb differ diff --git a/src/python/magnum/test/test_trade.py b/src/python/magnum/test/test_trade.py index 6360c1f..c05ffad 100644 --- a/src/python/magnum/test/test_trade.py +++ b/src/python/magnum/test/test_trade.py @@ -89,9 +89,14 @@ class ImageData(unittest.TestCase): mutable_view = MutableImageView2D(image) class MeshData(unittest.TestCase): - def test_init(self): - # Well this doesn't do much but well - a = trade.MeshData2D + def test(self): + # The only way to get a mesh instance is through a manager + importer = trade.ImporterManager().load_and_instantiate('TinyGltfImporter') + importer.open_file(os.path.join(os.path.dirname(__file__), 'mesh.glb')) + + mesh = importer.mesh3d(0) + self.assertEqual(mesh.primitive, MeshPrimitive.TRIANGLES) + # TODO: test more, once it's exposed class Importer(unittest.TestCase): def test(self): @@ -111,6 +116,26 @@ class Importer(unittest.TestCase): importer = trade.ImporterManager().load_and_instantiate('StbImageImporter') self.assertFalse(importer.is_opened) + with self.assertRaisesRegex(RuntimeError, "no file opened"): + importer.mesh2d_count + with self.assertRaisesRegex(RuntimeError, "no file opened"): + importer.mesh3d_count + + with self.assertRaisesRegex(RuntimeError, "no file opened"): + importer.mesh2d_for_name('') + with self.assertRaisesRegex(RuntimeError, "no file opened"): + importer.mesh3d_for_name('') + + with self.assertRaisesRegex(RuntimeError, "no file opened"): + importer.mesh2d_name(0) + with self.assertRaisesRegex(RuntimeError, "no file opened"): + importer.mesh3d_name(0) + + with self.assertRaisesRegex(RuntimeError, "no file opened"): + importer.mesh2d(0) + with self.assertRaisesRegex(RuntimeError, "no file opened"): + importer.mesh3d(0) + with self.assertRaisesRegex(RuntimeError, "no file opened"): importer.image1d_count with self.assertRaisesRegex(RuntimeError, "no file opened"): @@ -143,6 +168,16 @@ class Importer(unittest.TestCase): importer = trade.ImporterManager().load_and_instantiate('StbImageImporter') importer.open_file(os.path.join(os.path.dirname(__file__), 'rgb.png')) + with self.assertRaises(IndexError): + importer.mesh2d_name(0) + with self.assertRaises(IndexError): + importer.mesh3d_name(0) + + with self.assertRaises(IndexError): + importer.mesh2d(0) + with self.assertRaises(IndexError): + importer.mesh3d(0) + with self.assertRaises(IndexError): importer.image1d_name(0) with self.assertRaises(IndexError): @@ -165,6 +200,17 @@ class Importer(unittest.TestCase): with self.assertRaisesRegex(RuntimeError, "opening data failed"): importer.open_data(b'') + def test_mesh3d(self): + # importer refcounting tested in image2d + importer = trade.ImporterManager().load_and_instantiate('TinyGltfImporter') + importer.open_file(os.path.join(os.path.dirname(__file__), 'mesh.glb')) + self.assertEqual(importer.mesh3d_count, 3) + self.assertEqual(importer.mesh3d_name(0), 'Non-indexed mesh') + self.assertEqual(importer.mesh3d_for_name('Non-indexed mesh'), 0) + + mesh = importer.mesh3d(0) + self.assertEqual(mesh.primitive, MeshPrimitive.TRIANGLES) + def test_image2d(self): manager = trade.ImporterManager() manager_refcount = sys.getrefcount(manager) diff --git a/src/python/magnum/trade.cpp b/src/python/magnum/trade.cpp index b36551e..e4790c1 100644 --- a/src/python/magnum/trade.cpp +++ b/src/python/magnum/trade.cpp @@ -245,6 +245,15 @@ void trade(py::module& m) { .def("close", &Trade::AbstractImporter::close, "Close currently opened file") /** @todo all other data types */ + .def_property_readonly("mesh2d_count", checkOpened, "Two-dimensional mesh count") + .def_property_readonly("mesh3d_count", checkOpened, "Three-dimensional mesh count") + .def("mesh2d_for_name", checkOpened, "Two-dimensional mesh ID for given name") + .def("mesh3d_for_name", checkOpened, "Three-dimensional mesh ID for given name") + .def("mesh2d_name", checkOpenedBounds, "Two-dimensional mesh name", py::arg("id")) + .def("mesh3d_name", checkOpenedBounds, "Three-dimensional mesh name", py::arg("id")) + .def("mesh2d", checkOpenedBoundsResult, "Two-dimensional mesh", py::arg("id")) + .def("mesh3d", checkOpenedBoundsResult, "Three-dimensional mesh", py::arg("id")) + .def_property_readonly("image1d_count", checkOpened, "One-dimensional image count") .def_property_readonly("image2d_count", checkOpened, "Two-dimensional image count") .def_property_readonly("image3d_count", checkOpened, "Three-dimensional image count")