Moving the UDT Instance to the Another Folder Via Script,

HI Folks,
I want to move the Memory Tag "MYTag" from the UDT Definition "My Definition 1" to "My Definition 2". Once the tag is successfully added to "My Definition 2", it should be removed from "My Definition 1".
I’m executing the script in the Script Console, and I’ve written a script to get both UDT configurations, extract the tag, transfer it, and apply changes using system.tag.configure().

-->Script:
from com.inductiveautomation.ignition.common.tags.paths.parser import TagPathParser

try:
	fromPath = "[default]types/My Definition 1"
	toPath = "[default]types/My Definition 2"
 
	# Get UDT definitions
	fromDef = system.tag.getConfiguration(fromPath, True)[0]
	toDef = system.tag.getConfiguration(toPath, True)[0]
 
	# Extract internal tags
	fromTags = fromDef.get("tags", [])
	toTags = toDef.get("tags", [])
 
	# Find and remove MYTag
	tagToMove = None
	newFromTags = []
	for tag in fromTags:
		if tag.get("name") == "MYTag":
			tagToMove = tag
		else:
			newFromTags.append(tag)
 
	if tagToMove is None:
		print("MYTag not found.")
	else:
		toTags.append(tagToMove)
		fromDef["tags"] = newFromTags
		toDef["tags"] = toTags
 
		fromDef["path"] = fromPath
		toDef["path"] = toPath
 
		# Correct usage: convert string to TagPath
		basePath = TagPathParser.parse("[default]types")
		system.tag.configure(basePath, [fromDef, toDef], "m")
 
		print("MYTag successfully moved from My Definition 1 to My Definition 2.")
 
except Exception as e:
	import sys
	print("Error:", str(e))
	sys.printException(e)

--> i am getting the error message as well "MYTag successfully moved from My Definition 1 to My Definition 2. ", but nothing is moved.

-->this is the error message:

[Error_Configuration("java.lang.ClassCastException: class java.lang.String cannot be cast to class com.inductiveautomation.ignition.common.tags.model.TagPath (java.lang.String is in module java.base of loader 'bootstrap'; com.inductiveautomation.ignition.common.tags.model.TagPath is in unnamed module of loader java.net.URLClassLoader @3099f5ed)"), Error_Configuration("java.lang.ClassCastException: class java.lang.String cannot be cast to class com.inductiveautomation.ignition.common.tags.model.TagPath (java.lang.String is in module java.base of loader 'bootstrap'; com.inductiveautomation.ignition.common.tags.model.TagPath is in unnamed module of loader java.net.URLClassLoader @3099f5ed)")]
MYTag successfully moved from My Definition 1 to My Definition 2.

can u please look in to this issue and provide a fix.

Regards,
Adarsh R SPreformatted text

system.tag.configure(basePath, [fromDef, toDef], "m")

This should be

system.tag.configure(basePath, {'tags': [fromDef, toDef]} , "m")

There might be something else as I haven't tested it. You also don't need the tag parser, you can set the basePath to just "[default]_types_"

Actually, youre using "types" instead of "_types_"

Don't have a gateway to test with right now, but I think it would be something like this, might need some troubleshoot if I messed up any syntax.
Also:

  • You only need to read the config of the original UDT
  • basePath parameter for system.tag.configure can just be a string
  • backup your tags before testing :laughing:
fromPath = "[default]_types_/My Definition 1"
toPath = "[default]_types_/My Definition 2"

# get tags to move
fromDef = system.tag.getConfiguration(fromPath, True)[0]
tagsToMove = fromDef['tags'] 

# copy the tags to the new location
system.tag.configure(toPath, tagsToMove, 'm')

# check to make sure tags are there
good = True
for tag in tagsToMove:
  newPath = "%s/%s" % (toPath, tag['name'])
 # if any new path doesn't exis
  if not system.tag.exists(newPath):
    good = False
    break
# if all tags are moved, empty out the original UDT
if good:
  # empty out the tags from the original config
  fromDef['tags'] = []
  # overwrite original with empty tags def
  system.tag.configure("[default]_types_", [fromDef], "o")