Browse Source

python: raise Python exceptions for shader API usage failures.

pull/8/head
Vladimír Vondruš 7 years ago
parent
commit
40891858d6
  1. 7
      doc/python/magnum.shaders.rst
  2. 20
      src/python/magnum/shaders.cpp
  3. 16
      src/python/magnum/test/test_shaders_gl.py

7
doc/python/magnum.shaders.rst

@ -37,3 +37,10 @@
:data POSITION: Vertex position :data POSITION: Vertex position
:data NORMAL: Normal direction :data NORMAL: Normal direction
:data TEXTURE_COORDINATES: 2D texture coordinates :data TEXTURE_COORDINATES: 2D texture coordinates
.. py:property:: magnum.shaders.Phong.alpha_mask
:raise AttributeError: If the shader was not created with `Flags.ALPHA_MASK`
.. py:property:: magnum.shaders.Phong.light_positions
:raise ValueError: If list length is different from `light_count`
.. py:property:: magnum.shaders.Phong.light_colors
:raise ValueError: If list length is different from `light_count`

20
src/python/magnum/shaders.cpp

@ -112,8 +112,14 @@ void shaders(py::module& m) {
// TODO: textures, once exposed // TODO: textures, once exposed
.def_property("shininess", nullptr, .def_property("shininess", nullptr,
&Shaders::Phong::setShininess, "Shininess") &Shaders::Phong::setShininess, "Shininess")
.def_property("alpha_mask", nullptr, .def_property("alpha_mask", nullptr, [](Shaders::Phong& self, Float mask) {
&Shaders::Phong::setAlphaMask, "Alpha mask") if(!(self.flags() & Shaders::Phong::Flag::AlphaMask)) {
PyErr_SetString(PyExc_AttributeError, "the shader was not created with alpha mask enabled");
throw py::error_already_set{};
}
self.setAlphaMask(mask);
}, "Alpha mask")
.def_property("transformation_matrix", nullptr, .def_property("transformation_matrix", nullptr,
&Shaders::Phong::setTransformationMatrix, "Set transformation matrix") &Shaders::Phong::setTransformationMatrix, "Set transformation matrix")
.def_property("normal_matrix", nullptr, .def_property("normal_matrix", nullptr,
@ -121,9 +127,19 @@ void shaders(py::module& m) {
.def_property("projection_matrix", nullptr, .def_property("projection_matrix", nullptr,
&Shaders::Phong::setProjectionMatrix, "Set projection matrix") &Shaders::Phong::setProjectionMatrix, "Set projection matrix")
.def_property("light_positions", nullptr, [](Shaders::Phong& self, const std::vector<Vector3>& positions) { .def_property("light_positions", nullptr, [](Shaders::Phong& self, const std::vector<Vector3>& positions) {
if(positions.size() != self.lightCount()) {
PyErr_Format(PyExc_ValueError, "expected %u items but got %u", self.lightCount(), UnsignedInt(positions.size()));
throw py::error_already_set{};
}
self.setLightPositions(positions); self.setLightPositions(positions);
}, "Light positions") }, "Light positions")
.def_property("light_colors", nullptr, [](Shaders::Phong& self, const std::vector<Color4>& colors) { .def_property("light_colors", nullptr, [](Shaders::Phong& self, const std::vector<Color4>& colors) {
if(colors.size() != self.lightCount()) {
PyErr_Format(PyExc_ValueError, "expected %u items but got %u", self.lightCount(), UnsignedInt(colors.size()));
throw py::error_already_set{};
}
self.setLightColors(colors); self.setLightColors(colors);
}, "Light colors"); }, "Light colors");
} }

16
src/python/magnum/test/test_shaders_gl.py

@ -58,9 +58,19 @@ class Phong(GLTestCase):
self.assertEqual(c.light_count, 3) self.assertEqual(c.light_count, 3)
def test_uniforms(self): def test_uniforms(self):
a = shaders.Phong() a = shaders.Phong(shaders.Phong.Flags.ALPHA_MASK, 2)
a.diffuse_color = (0.5, 1.0, 0.9) a.diffuse_color = (0.5, 1.0, 0.9)
a.transformation_matrix = Matrix4.translation(Vector3.x_axis()) a.transformation_matrix = Matrix4.translation(Vector3.x_axis())
a.projection_matrix = Matrix4.zero_init() a.projection_matrix = Matrix4.zero_init()
a.light_positions = [(0.5, 1.0, 0.3)] a.light_positions = [(0.5, 1.0, 0.3), Vector3()]
a.light_colors = [Color4()] a.light_colors = [Color4(), Color4()]
a.alpha_mask = 0.3
def test_uniforms_errors(self):
a = shaders.Phong()
with self.assertRaisesRegex(AttributeError, "the shader was not created with alpha mask enabled"):
a.alpha_mask = 0.3
with self.assertRaisesRegex(ValueError, "expected 1 items but got 0"):
a.light_positions = []
with self.assertRaisesRegex(ValueError, "expected 1 items but got 0"):
a.light_colors = []

Loading…
Cancel
Save