Constructed Tag Path is Invalid

Hello there,

I am trying to generate a valid tag path for the tagPaths parameter of system.tag.readBlocking function. The tag path I want to use is [Default]AI_LT1.

The path does work If I generate the string in the following way

path = "[Default]AI_LT1"
result = system.tag.readBlocking(path)

However, if I tended to generate the same path by combining one variable that contains a pre-defined value with a string component, the path is invalid then. There is the example code:

tag_raw = self.props.selection.active.data[0]['displayPath']
tag_index = tag_raw.find('-')
tag_trimmed = tag_raw[:tag_index]

path = tag_trimmed + "AI_LT1"
result = system.tag.readBlocking(path)

In this case, tag_trimmed is equal to [Default] and will display [Default]AI_LT1 if print (path). However, it fails to read the proper tag content as what I type in the path = ''[Default]AI_LT1" directly.

I tried some initial troubleshooting by using str (path) or .encode('utf-8') to return a string or unicode datatype but no luck.

Any idea why it happens? Thanks for the help!

Can you show us the text contained in self.props.selection.active.data[0]['displayPath'] ? It is a little hard to follow what you are trying to achieve here.

Add print type(tag_trimmed) to see if it is actually the type that you are expecting. Also try print type(path) to confirm its a string.

system.tag.readBlocking expects a list of tag paths. It allows you to pass a single string to avoid breaking projects from older versions that got upgraded. It might not like something about how you are constructing the string, so try using result = system.tag.readBlocking([path])

Also consider using java's string formatting to generate the path, ie:
path = "%sAI_LT1" % tag_trimmed

Thanks for the rely!Tthe text contained in `self.props.selection.active.data[0]['displayPath'] is a PyUnicode with text [Default] - Alarm/Hi/Alarm:/alm:Alarm. After trimming, tag_trimmed is then equal to [Default].

Change this:
tag_index = tag_raw.find('-')

to this:

tag_index = tag_raw.find('-') -1

You are including a space between the [Default] and the tag path.

2 Likes

Hello Ryan, thanks for the reply! the data type of path was PyUnicode. I tried use str(path) which supposes to return a PyString data type but still doesn't work. I will try the rest methods you mentioned later the.

Safer method might be to call .strip() on the result of the original trim, I can foresee someone not including that space between [Default] and the -.

2 Likes

By applying either of your solutions @David_Stone @ryan.white and removing the space in between, the path is working properly now. Really appreciate the help!