diff --git a/modules/zivid/presets.py b/modules/zivid/presets.py index e944058a..dabac734 100644 --- a/modules/zivid/presets.py +++ b/modules/zivid/presets.py @@ -4,10 +4,11 @@ from zivid.settings import _to_settings from zivid.camera_info import CameraInfo, _to_internal_camera_info +from zivid.settings2d import _to_settings2d class Preset: - """3D settings preset. + """Settings preset. Presets are pre-defined settings that are tuned for different camera models to perform optimally under different conditions and use cases. @@ -27,10 +28,21 @@ def __init__(self, impl): Raises: TypeError: If argument does not match the expected internal class. """ - if not isinstance(impl, _zivid.presets.Preset): + if not any( + [ + isinstance(impl, _zivid.presets.Preset), + isinstance(impl, _zivid.presets.Preset2D), + ] + ): raise TypeError( "Unsupported type for argument impl. Got {}, expected {}".format( - type(impl), _zivid.presets.Preset + type(impl), + " or ".join( + ( + _zivid.presets.Preset.__name__, + _zivid.presets.Preset2D.__name__, + ) + ), ) ) self.__impl = impl @@ -53,9 +65,16 @@ def settings(self): recommended to save them to a YML file and load as needed. Returns: - The settings of the preset. + The settings of the preset. The settings will be an instance of Settings if the preset is a 3D preset, + or an instance of Settings2D if the preset is a 2D preset. """ - return _to_settings(self.__impl.settings()) + + settings = self.__impl.settings() + + if isinstance(settings, _zivid.Settings2D): + return _to_settings2d(settings) + + return _to_settings(settings) def __str__(self): return str(self.__impl) @@ -64,8 +83,8 @@ def __str__(self): class Category: """Preset category. - This class cannot be initialized directly by the end-user. Use the categories function to - obtain a list of categories available for a camera. + This class cannot be initialized directly by the end-user. Use the categories or categories2d functions to obtain a + list of categories available for a camera. """ def __init__(self, impl): @@ -79,10 +98,21 @@ def __init__(self, impl): Raises: TypeError: If argument does not match the expected internal class. """ - if not isinstance(impl, _zivid.presets.Category): + if not any( + [ + isinstance(impl, _zivid.presets.Category), + isinstance(impl, _zivid.presets.Category2D), + ] + ): raise TypeError( "Unsupported type for argument impl. Got {}, expected {}".format( - type(impl), _zivid.presets.Category + type(impl), + " or ".join( + ( + _zivid.presets.Category.__name__, + _zivid.presets.Category2D.__name__, + ) + ), ) ) self.__impl = impl @@ -135,3 +165,23 @@ def categories(model): _to_internal_camera_info(CameraInfo(model=model)).model ) ] + + +def categories2d(model): + """Get available 2D preset categories for the specified camera model. + + See `categories` for more information. + + Args: + model: The model for the camera whose 2D preset categories should be returned. This value can + be obtained from CameraInfo.model. + + Returns: + The available 2D categories for the specified camera model. + """ + return [ + Category(c) + for c in _zivid.presets.categories2d( + _to_internal_camera_info(CameraInfo(model=model)).model + ) + ] diff --git a/src/Presets.cpp b/src/Presets.cpp index 338f3841..8ce1a11f 100644 --- a/src/Presets.cpp +++ b/src/Presets.cpp @@ -9,11 +9,21 @@ namespace ZividPython pyClass.def("name", &Zivid::Presets::Preset::name).def("settings", &Zivid::Presets::Preset::settings); } + void wrapClass(pybind11::class_ pyClass) + { + pyClass.def("name", &Zivid::Presets::Preset2D::name).def("settings", &Zivid::Presets::Preset2D::settings); + } + void wrapClass(pybind11::class_ pyClass) { pyClass.def("name", &Zivid::Presets::Category::name).def("presets", &Zivid::Presets::Category::presets); } + void wrapClass(pybind11::class_ pyClass) + { + pyClass.def("name", &Zivid::Presets::Category2D::name).def("presets", &Zivid::Presets::Category2D::presets); + } + namespace Presets { void wrapAsSubmodule(pybind11::module &dest) @@ -21,9 +31,12 @@ namespace ZividPython using namespace Zivid::Presets; ZIVID_PYTHON_WRAP_CLASS(dest, Preset); + ZIVID_PYTHON_WRAP_CLASS(dest, Preset2D); ZIVID_PYTHON_WRAP_CLASS(dest, Category); + ZIVID_PYTHON_WRAP_CLASS(dest, Category2D); dest.def("categories", &categories); + dest.def("categories2d", &categories2D); } } // namespace Presets diff --git a/test/test_presets.py b/test/test_presets.py index 10a0edd6..39fec367 100644 --- a/test/test_presets.py +++ b/test/test_presets.py @@ -1,7 +1,7 @@ import pytest -def test_presets(application): # pylint: disable=unused-argument +def test_3d_presets(application): # pylint: disable=unused-argument import zivid for model in zivid.CameraInfo.Model.valid_values(): @@ -26,3 +26,30 @@ def test_presets(application): # pylint: disable=unused-argument assert isinstance(preset, zivid.presets.Preset) assert preset.name != "" assert isinstance(preset.settings, zivid.Settings) + + +def test_2d_presets(application): # pylint: disable=unused-argument + import zivid + + for model in zivid.CameraInfo.Model.valid_values(): + if model in [ + zivid.CameraInfo.Model.zividOnePlusLarge, + zivid.CameraInfo.Model.zividOnePlusMedium, + zivid.CameraInfo.Model.zividOnePlusSmall, + ]: + with pytest.raises( + RuntimeError, + match=f"Internal error: The camera model '{model}' is not supported by this version of Zivid SDK.", + ): + zivid.presets.categories2d(model) + else: + categories = zivid.presets.categories2d(model) + assert categories + for category in categories: + assert isinstance(category, zivid.presets.Category) + assert category.name != "" + assert category.presets + for preset in category.presets: + assert isinstance(preset, zivid.presets.Preset) + assert preset.name != "" + assert isinstance(preset.settings, zivid.Settings2D)