Browse Source

python: expose mesh import.

pull/8/head
Vladimír Vondruš 7 years ago
parent
commit
59a2773808
  1. 24
      doc/python/magnum.trade.rst
  2. BIN
      src/python/magnum/test/mesh.glb
  3. 52
      src/python/magnum/test/test_trade.py
  4. 9
      src/python/magnum/trade.cpp

24
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

BIN
src/python/magnum/test/mesh.glb

Binary file not shown.

52
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)

9
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<UnsignedInt, &Trade::AbstractImporter::mesh2DCount>, "Two-dimensional mesh count")
.def_property_readonly("mesh3d_count", checkOpened<UnsignedInt, &Trade::AbstractImporter::mesh3DCount>, "Three-dimensional mesh count")
.def("mesh2d_for_name", checkOpened<Int, const std::string&, &Trade::AbstractImporter::mesh2DForName>, "Two-dimensional mesh ID for given name")
.def("mesh3d_for_name", checkOpened<Int, const std::string&, &Trade::AbstractImporter::mesh3DForName>, "Three-dimensional mesh ID for given name")
.def("mesh2d_name", checkOpenedBounds<std::string, &Trade::AbstractImporter::mesh2DName, &Trade::AbstractImporter::mesh2DCount>, "Two-dimensional mesh name", py::arg("id"))
.def("mesh3d_name", checkOpenedBounds<std::string, &Trade::AbstractImporter::mesh3DName, &Trade::AbstractImporter::mesh3DCount>, "Three-dimensional mesh name", py::arg("id"))
.def("mesh2d", checkOpenedBoundsResult<Trade::MeshData2D, &Trade::AbstractImporter::mesh2D, &Trade::AbstractImporter::mesh2DCount>, "Two-dimensional mesh", py::arg("id"))
.def("mesh3d", checkOpenedBoundsResult<Trade::MeshData3D, &Trade::AbstractImporter::mesh3D, &Trade::AbstractImporter::mesh3DCount>, "Three-dimensional mesh", py::arg("id"))
.def_property_readonly("image1d_count", checkOpened<UnsignedInt, &Trade::AbstractImporter::image1DCount>, "One-dimensional image count")
.def_property_readonly("image2d_count", checkOpened<UnsignedInt, &Trade::AbstractImporter::image2DCount>, "Two-dimensional image count")
.def_property_readonly("image3d_count", checkOpened<UnsignedInt, &Trade::AbstractImporter::image3DCount>, "Three-dimensional image count")

Loading…
Cancel
Save