I have a script in perspective (Message Handler located in the root of a view) that takes variables passed through the message and assigns two custom properties the values.
after this code, I have a logger event that logs the value of passedData as an array. Every time the script runs, the logger outputs the correct array with the correct keys and values. Although, my second script (an event script under a button in the same view) is set to retrieve the custom properties the prior script set using the following code:
data = self.parent.custom.passedData
logger.info("data array from root: " + repr(data))
direction = data["direction"]
ID = data["ID"]
logger.info("Direction: " + direction + ". ID: " + ID)
Every time this script is run (despite the log of the prior script showing the array as assigned values) returns the values as null when reading directly from the custom property, and from the local variables. I've also tried variations of the code above such as:
direction = self.parent.custom.passedData.direction
ID = self.parent.custom.passedData.ID
logger.info("Direction: " + direction + ". ID: " + ID)
and get the same results. Is there something I'm doing wrong when assigning values to the Custom Properties making them unreadable/null to other scripts?
Are you sure self in the first script refers to the same node as self.parent in the second script? I recommend putting all custom properties on the view itself rather than individual components, as references to those can break easily as soon as you start renaming or moving components.
Are you sure self in the first script refers to the same node as self.parent in the second script?
Yes, I will probably move the properties to the view after I've solved this problem. But to continue the conversation, the second script is reading the custom property without issue, its just returning that the array has all the keys it should, but all the values I assigned are showing as null.
The logger from the first script is set to read and log the value of the custom array at the very end of the script for debugging purposes, here is the output of that log:
{u'ID': u'WR-A1-A1', u'direction': u'out'}
The ID and Direction keys seem to be set and assigned values properly. Moving to the second script, the logger is set to output the value of the same array at the very beginning of the script. Here is the output of the second script (keep in mind, no other scripts execute between the two scripts I've been referring to):
{u'ID': u'', u'direction': u''}
See, the script has found the custom properties and can read the array, but its acting as if the first script never assigned any values to the keys. Any idea why that may be?
Assignments to Perspective properties are asynchronous. You cannot read back from them in the same script and expect them to have the new values. (Yes, this is poorly documented.)
Your scripts should use jython local variables for any intermediate work. Read from Perspective properties to gather necessary information, compute with it, then write results back to Perspective properties as needed.
Treat as you would tag reads and writes, and you will not be disappointed.
Thank you, @pturmel! With properties being asynchronous, how would I go about writing the data I need to move between scripts into a perspective property? I think that's what I'm attempting to do now but seem to be unable to actually write to the property, unless there's been a miscommunication.
News to me! But in this case he has two separate scripts. Unless I'm misunderstanding, after the first has executed and printed the correctly written values to a logger, he presses a button that executes the second script. Surely by that time the new value should be available, no?
The second script is triggered by a button press, the time between the execution of the first and second script can vary depending on how long the user takes to proceed, so I don't necessarily have a set of function arguments to utilize in this instance, any other suggestions as to how I can communicate this data between the scripts?
Not too sure that'd make much of a difference, same thing with the time it takes to populate the array not being an issue, the custom properties are written to as soon as the view opens, and the button press typically doesn't happen for about 10+ seconds after the array has been written to as there's an (unrelated) input in the view that needs to be filled before pressing the button. So timing may not be the culprit here
I'm gonna go ahead and mark this comment as the solution as its brought the most information forward in regards to the issue, I've yet to fix the bug but I now have enough information to get out of the blockade I was at, Ill update the thread when I find a solution