Hello,
I’m wondering if it is possible to use an instance of a class as a view custom property. Two reasons I have for wanting to do this:
- Accessing data members within the class
- Calling class methods from view components without having to instantiate the class each time. Faster and cleaner.
I’ve tried creating a object type custom prop and then using a script transform to get a class instance like so:
This appears to return a class object, but within designer or a session, Perspective treats this property as a unicode object. Trying to access any of the class members or methods return errors, and a print of the property type returns
09:14:12.481 [Browser Thread: 59281] INFO Perspective.Designer.Workspace - class org.python.core.PyUnicode
Is what I’m trying to do possible, or do I have to resort to calling class methods and members ad hoc?
Perspective properties are expected to be convertible to/from json, so I doubt this will ever work. You might consider creating an architecture that holds such objects in a persistent dictionary. Perhaps using a weak-key dictionary keyed by session object. Tricky. You might find these discusions helpful, particularly to avoid memory leaks:
https://forum.inductiveautomation.com/search?q=system.util.persistent
1 Like
Yeah, as Phil said, this isn’t going to work as-is.
We could in theory implement something like this (it is convenient in Vision to be able to get/put
arbitrary objects at runtime), but I wouldn’t expect it anytime soon, so you’ll have to work around it somehow.
The biggest gotcha is the need to replace object instances wherever you’ve tucked them whenever the defining script module is restarted. Object instances tucked into Ignition’s infrastructure will retain the old class methods, which will hold references to the old interpreter, which will leak memory in huge chunks. ):
1 Like