From 712b3789a1d961661e4076846a2d33a2e830a41e Mon Sep 17 00:00:00 2001 From: morefigs Date: Thu, 21 Feb 2019 12:10:55 +1100 Subject: calling an object command feature type as an attribute returns a callable --- CHANGELOG.md | 3 +++ examples/camera/set_feature_value.py | 22 ++++++++++++++++++++++ examples/camera/write_feature_value.py | 22 ---------------------- examples/interface/set_feature_value.py | 22 ++++++++++++++++++++++ examples/interface/write_feature_value.py | 22 ---------------------- pymba/camera.py | 12 ++++++------ pymba/vimba.py | 8 +++++++- pymba/vimba_object.py | 14 ++++++++++++-- 8 files changed, 72 insertions(+), 53 deletions(-) create mode 100644 examples/camera/set_feature_value.py delete mode 100644 examples/camera/write_feature_value.py create mode 100644 examples/interface/set_feature_value.py delete mode 100644 examples/interface/write_feature_value.py diff --git a/CHANGELOG.md b/CHANGELOG.md index ad0f512..3a9f1f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ ## [0.3.1] - 2019/02/21 ### Added - Auto adjust packet size upon opening camera. +- Command type features can now be called directly as an object attribute. +### Changed +- Increased default frame buffer size from 3 to 10. ## [0.3] - 2019/02/11 ### Added diff --git a/examples/camera/set_feature_value.py b/examples/camera/set_feature_value.py new file mode 100644 index 0000000..77f8ce5 --- /dev/null +++ b/examples/camera/set_feature_value.py @@ -0,0 +1,22 @@ +from pymba import Vimba + + +if __name__ == '__main__': + + with Vimba() as vimba: + camera = vimba.camera(0) + camera.open() + + # read a feature value + feature = camera.feature('ExposureAuto') + value = feature.value + + # set the feature value + feature.value = value + + print(feature.name, '=', feature.value) + + # alternatively the feature value can be set as an object attribute + camera.ExposureAuto = feature.value + + camera.close() diff --git a/examples/camera/write_feature_value.py b/examples/camera/write_feature_value.py deleted file mode 100644 index 77f8ce5..0000000 --- a/examples/camera/write_feature_value.py +++ /dev/null @@ -1,22 +0,0 @@ -from pymba import Vimba - - -if __name__ == '__main__': - - with Vimba() as vimba: - camera = vimba.camera(0) - camera.open() - - # read a feature value - feature = camera.feature('ExposureAuto') - value = feature.value - - # set the feature value - feature.value = value - - print(feature.name, '=', feature.value) - - # alternatively the feature value can be set as an object attribute - camera.ExposureAuto = feature.value - - camera.close() diff --git a/examples/interface/set_feature_value.py b/examples/interface/set_feature_value.py new file mode 100644 index 0000000..0c71da2 --- /dev/null +++ b/examples/interface/set_feature_value.py @@ -0,0 +1,22 @@ +from pymba import Vimba + + +if __name__ == '__main__': + + with Vimba() as vimba: + interface = vimba.interface(0) + interface.open() + + # set a feature value by feature name + feature = interface.feature('InterfacePingPace') + value = feature.value + + # set the feature value + feature.value = value + + print(feature.name, '=', feature.value) + + # alternatively the feature value can be set as an object attribute + interface.InterfacePingPace = 3 + + interface.close() diff --git a/examples/interface/write_feature_value.py b/examples/interface/write_feature_value.py deleted file mode 100644 index 0c71da2..0000000 --- a/examples/interface/write_feature_value.py +++ /dev/null @@ -1,22 +0,0 @@ -from pymba import Vimba - - -if __name__ == '__main__': - - with Vimba() as vimba: - interface = vimba.interface(0) - interface.open() - - # set a feature value by feature name - feature = interface.feature('InterfacePingPace') - value = feature.value - - # set the feature value - feature.value = value - - print(feature.name, '=', feature.value) - - # alternatively the feature value can be set as an object attribute - interface.InterfacePingPace = 3 - - interface.close() diff --git a/pymba/camera.py b/pymba/camera.py index 4cda105..5a8b75b 100644 --- a/pymba/camera.py +++ b/pymba/camera.py @@ -133,7 +133,7 @@ class Camera(VimbaObject): # may experience issues with camera comms if not called if adjust_packet_size: - self.run_feature_command('GVSPAdjustPacketSize') + self.GVSPAdjustPacketSize() def close(self): """ @@ -181,7 +181,7 @@ class Camera(VimbaObject): """ return Frame(self) - def arm(self, mode: str, callback: Optional[Callable] = None, frame_buffer_size: Optional[int] = 3) -> None: + def arm(self, mode: str, callback: Optional[Callable] = None, frame_buffer_size: Optional[int] = 10) -> None: """ Arm the camera by starting the capture engine and creating frames. :param mode: Either 'SingleFrame' to acquire a single frame or 'Continuous' for streaming frames. @@ -229,9 +229,9 @@ class Camera(VimbaObject): # capture a single frame self._single_frame.queue_for_capture() - self.run_feature_command('AcquisitionStart') + self.AcquisitionStart() self._single_frame.wait_for_capture() - self.run_feature_command('AcquisitionStop') + self.AcquisitionStop() return self._single_frame @@ -259,7 +259,7 @@ class Camera(VimbaObject): raise VimbaException(VimbaException.ERR_INVALID_CAMERA_MODE) # safe to call multiple times - self.run_feature_command('AcquisitionStart') + self.AcquisitionStart() self._is_acquiring = True def _streaming_callback(self, frame: Frame) -> None: @@ -279,7 +279,7 @@ class Camera(VimbaObject): # implies both is armed and in continuous mode if self._is_acquiring: self._is_acquiring = False - self.run_feature_command('AcquisitionStop') + self.AcquisitionStop() def disarm(self) -> None: """ diff --git a/pymba/vimba.py b/pymba/vimba.py index 66329ab..1079b12 100644 --- a/pymba/vimba.py +++ b/pymba/vimba.py @@ -59,7 +59,7 @@ class Vimba: # automatically check for the presence of a GigE transport layer if self.system().GeVTLIsPresent: self.system().GeVDiscoveryAllDuration = 250 - self.system().run_feature_command('GeVDiscoveryAllOnce') + self.system().GeVDiscoveryAllOnce() @staticmethod def shutdown(): @@ -89,8 +89,11 @@ class Vimba: if interface_id in self._interfaces: return self._interfaces[interface_id] + + # cache interface instances interface = Interface(interface_id) self._interfaces[interface_id] = interface + return interface @staticmethod @@ -109,6 +112,9 @@ class Vimba: if camera_id in self._cameras: return self._cameras[camera_id] + + # cache camera instance camera = Camera(camera_id) self._cameras[camera_id] = camera + return camera diff --git a/pymba/vimba_object.py b/pymba/vimba_object.py index 174fe9a..fbc6b05 100644 --- a/pymba/vimba_object.py +++ b/pymba/vimba_object.py @@ -2,7 +2,7 @@ from ctypes import byref, sizeof, c_void_p, c_uint32, c_uint64, c_bool from typing import List, Optional from .vimba_exception import VimbaException -from .feature import Feature +from .feature import Feature, _FEATURE_DATA_COMMAND from . import vimba_c @@ -25,7 +25,14 @@ class VimbaObject: def __getattr__(self, item: str): # allow direct access to feature values as an attribute if item in self.feature_names(): - return self.feature(item).value + feature = self.feature(item) + + # command feature types are a special case, return a callable + if feature.info.featureDataType == _FEATURE_DATA_COMMAND: + return lambda: self.run_feature_command(item) + + # otherwise attempt to get their value + return feature.value raise AttributeError(f'{self.__class__.__name__} object has no attribute {item}') @@ -94,8 +101,11 @@ class VimbaObject: """ if feature_name in self._features: return self._features[feature_name] + + # cache feature feature = Feature(feature_name, self._handle) self._features[feature_name] = feature + return feature def run_feature_command(self, feature_name: str) -> None: -- cgit v1.2.3