Perspective object not updating within script

I am having an issue with an object not being written to within a message handler script in perspective.

Here is my script:

note = payload['note']
downtime = payload['plannedDowntime']
payloadLine = payload['line']

lines = self.view.custom.selectedLines
lineNames = []
	
#Get a list of the selected line names
for lineName in lines:
	lineNames.append(lineName['line'])

#Get the index of the payload line
index = lineNames.index(payloadLine)
	
lineUserInputData = self.view.custom.lineUserInputData
	
system.perspective.print('TESTING--------------------')
system.perspective.print(lineUserInputData)
system.perspective.print(downtime)
	
#Change the values based on the message handler response
lineUserInputData[index]["Downtime_Goal"] = downtime
lineUserInputData[index]["Notes"] = note
	
system.perspective.print(lineUserInputData)

#Set the objects as the changed values
self.view.custom.lineUserInputData = lineUserInputData

Here is the object I am trying to update:

Screenshot 2021-08-24 110250

In my code I am able to pull in the object successfully. I can print it out correctly. I have checked that the payload coming into the handler script is correct as well as the index variable. The “downtime” variable is also correct when I print it. Yet, the object does not update. I have also tried copying the object into the script console and running the same code structure and it updates just fine. I also receive no error messages when this runs.

What if you change this line to lineUserInputData = list(self.view.custom.lineUserInputData)?

In some edge cases, the objects that represent objects and arrays in our backing model don’t synchronize writes back to the underlying property tree correctly. By calling list, you should convert from our internal object to a plain Python list, which shouldn’t have the same issues - and then your assignment at the end of the script will be ‘atomic’.

Still nothing after converting to a list.

Hm, you might need to do it recursively; you could try this:

from com.inductiveautomation.ignition.common import TypeUtilities

lineUserInputData = TypeUtilities.pyToGson(self.view.custom.lineUserInputData)

I got it to work by just copying the original object structure into a new object which I think is the same thing as the recursive option you suggested. This copy method got it working:

def copy(data):
	output = []
	for row in data:
		rowData = {}
		for key in row:
			value = row[key]
			rowData[key] = value
		output.append(rowData)
	return output
1 Like

Could you use:

import copy
new Dict = copy.deepcopy(dict)

This will copy the dict and any nested dicts into a new independent object

1 Like