Create ZenExperiment instance in imported class

Everything OAD-related that won't fit in the other categories: share general aspects of macro programming and discuss the OAD environment
Post Reply
Marco Raffaele Cosenza
Posts: 1
Joined: Wed Nov 23, 2022 3:40 pm

Create ZenExperiment instance in imported class

Post by Marco Raffaele Cosenza »

Hello,
I have the need to load a ZenExperiment from a class described in a module imported from the macro editor.
The ZenExperiment type is not available, of course, but I can pass it directly to the class.
Just to be clear, in the main macro code, I have

Code: Select all

from module.test import test_function

exp = test_function(ZenExperiment)
print dir(exp)
While in module.test I have

Code: Select all

def test_function(ZenExperiment):
	new_exp = ZenExperiment()
	new_exp.Load('zen_exp_name')
	print dir(new_exp)
	return new_exp
Unfortunately, this works only partially as only part of the methods are then available in the ZenExperiment instance.
Indeed by comparing the output of the two dir() calls, all methods concerning with getting/adding areas and positions are missing, raising a missing attribute error when called.
On the other hand, I can save the experiment file and manually check the xml, confirming that the right experiment was 'loaded'.

I tried to load the experiment also via

Code: Select all

new_exp = Zen.ObjectFactory.Create("Zeiss.Micro.Scripting.ZenExperiment") 
new_exp.Load('zen_exp_name')
and also via

Code: Select all

Zen.Acquisition.Experiments.GetByName("zen_exp_name") 
But the result is the same.

Would there be a way to allow the addition/modification of positions in an experiment created/loaded in a module?
Thank you very much for helping!
Martin Stas
Posts: 11
Joined: Mon Jun 19, 2023 12:04 pm

Re: Create ZenExperiment instance in imported class

Post by Martin Stas »

Hey Marco,

I can definitely understand your frustration with this issue. It seems like you've tried a few different approaches, but you're still running into problems with missing methods in the 'ZenExperiment' instance when it's loaded from a module.

From what you've described, it appears that the issue might be related to how the 'ZenExperiment' instance is instantiated or loaded. You've made a good attempt with 'Zen.ObjectFactory.Create' and 'Zen.Acquisition.Experiments.GetByName', but it's unfortunate that the methods related to areas and positions are still missing.

One potential solution to this problem could involve defining and initializing the 'ZenExperiment' instance within the module itself rather than passing it as a parameter. This way, you have more control over its creation and initialization. You can then expose the necessary methods to interact with areas and positions.

Here's a modified version of your code in 'module.test':

Code: Select all

# module.test

import Zen  # Make sure to import Zen here

def initialize_experiment():
    new_exp = Zen.Micro.Scripting.ZenExperiment()  # Initialize the ZenExperiment instance
    new_exp.Load('zen_exp_name')
    return new_exp

def add_position(exp, position_name):
    # Add your logic for adding positions here
    pass
Then, in your main macro code:

Code: Select all

# Main macro code

from module.test import initialize_experiment, add_position

exp = initialize_experiment()
print dir(exp)

# Now, you can call add_position to modify positions in the experiment
add_position(exp, 'new_position')
By handling the initialization and manipulation of the 'ZenExperiment' instance within your module, you might have better control over the available methods and attributes. Make sure to adapt the 'add_position' function to your specific use case to effectively modify positions within the experiment.

I hope this helps you resolve the issue you're facing.
Post Reply