Copy one UDT Tag to Another

Is there a way to copy one udt tag and values to another udt tag? Not the individual members but the whole thing at once.

I tried:

newUDT = system.tag.read(path to UDT to copy).value
system.tag.write(path to UDT to overwrite, newUDT)

But I’m getting an error that says that the tag doesn’t support writing.

Any nifty ways to do this? Or do I need to individually deep copy each member to the new spot?

Deep copy, sorry. And you need to skip any that are expressions or derived tags.

Not a problem. Thanks for getting back to me.

Sorry, I’m new to Ignition and Python. Can you please explain deep copy. I’m using Ignition 8 and have a UDT that I want to copy all of its members’ values (values only) to a different UDT. This seems impossible at the moment without going one by one.
This would make a huge script as some of the members of the UDT are another UDT type.

“Deep copy” refers to going one by one through the members. You may want to make a script that browses through the members and copies the values for all relevant members (skip expressions, etc.), rather than hard-coding all the members to copy. See system.tag.browse.

Thanks witman.
Do you have any examples of the system.tag.browse? The user manual isn’t very helpful on this. When I use it, it returns everything. How can I get it to only return a list of filtered criteria?

You’ll need to recursively browse tags that don’t meet your criteria to find the ones below them that do. Something like this should get you close to what you’re looking for (returns all tags in the specified path with valueSource type of opc or memory:

def browseForTags(path, valueSources):
	'''Return tag paths of the specified valueSource type(s).'''
	
	# Call the browse function.
	results = system.tag.browse(path, {})
	 
	# Loop through every item in the results.
	for result in results.getResults():
		# If result has children, call the function to repeat the process, starting at the child.
		if result['hasChildren']:
			browseForTags(result['fullPath'], valueSources)
		elif result['valueSource'] in valueSources:
			tagsToCopy.append(result['fullPath'])
         
# Create an empty list to be filled with tag paths.
tagsToCopy = []
# Set parameters & call function.
path = '[default]' # Replace with root path of UDT/folder to copy.
valueSources = ['opc', 'memory']
browseForTags(path, valueSources)
# Print results.
for tagPath in tagsToCopy:
	print tagPath

You can take the list of tag paths, read them, manipulate the names to match the paths you need to copy them to (hopefully just a different root path), and then write all the read values to the new list of tag paths.

1 Like