Jump to content

Working with Class Tags


danm01

Recommended Posts

Hello all,

 

I struggled with this a bit, so I figured I'd share in hopes of saving someone else some pain and anguish. 

 

The key learning is that the result of vs.GetObjecTags(class_handle) is a 2-element tuple that contains TRUE in index 0 and either None or a nested tuple with all attached tags in index 1. Once I figured that out it was easy to work with the list. I have not experimented, but I imagine Layer Tags work the same way.

 

Example below creates a worksheet with a list of Class Tags:

import vs

classes_total = vs.ClassNum()
taglist = []

# make a list of tag names, skipping classes without a tag

for x in range(classes_total):
	ClassName = vs.ClassList(x + 1)
	hClass = vs.GetObject(ClassName)
	tagTuple = vs.GetObjectTags(hClass)
	if tagTuple[1] == None: pass #skip classes with no tag
	else:
		tagArray = tagTuple[1]
    
		for i in range(len(tagArray)):
			tag = tagArray[i]
			if tag in taglist: pass
			else: taglist.append(tag)
         
# check for the Class Tag List worksheet. If it doesn't exist, create it.

ws_handle = vs.GetObject("Class Tag List")

#if no worksheet found, create it
if vs.GetTypeN(ws_handle) != 18:
	ws_handle = vs.CreateWS("Class Tag List",len(taglist)+1,1)
	vs.SetWSCellFormulaN(ws_handle, 1, 1, 1, 1, "Class Tags")
	vs.SetWSCellTextFormat(ws_handle,1,1,1,1,0,0,1) #make the header bold

#if the worksheet exists, easist way to handle is delete everything except the header and start from scratch
else:
	rowcount = vs.GetWSRowColumnCount(ws_handle) 
	vs.DeleteWSRows(ws_handle, 2, rowcount[0]-1)
	vs.InsertWSRows(ws_handle, 2, len(taglist)+1)
	vs.DeleteWSRows(ws_handle, len(taglist)+1, 1)

# add elements from the tag list to the worksheet. 
for i in range(len(taglist)):
    vs.SetWSCellFormulaN(ws_handle, i+2, 1, i+2, 1, taglist[i])
    vs.SetWSCellTextFormat(ws_handle,i+2,1,i+2,1,0,0,0) #make the content rows regular text

vs.ShowWS(ws_handle,True) 

 

This example shows or hides classes based on tag value:

import vs

TagStr = 'Audio'
ShowMe = 1

def ViewByTag(TagName,ShowHide):

	classes_total = vs.ClassNum()

	for x in range(classes_total):
		ClassName = vs.ClassList(x + 1)
		hClass = vs.GetObject(ClassName)
		tagTuple = vs.GetObjectTags(hClass)
	
		if tagTuple[1] == None: pass
		else: 
			tagArray = tagTuple[1]
		
			if TagName in str(tagArray): 
			
				if ShowHide == 0: vs.HideClass(ClassName)
				else: vs.ShowClass(ClassName)
				
ViewByTag(TagStr,ShowMe)

 

 

Enjoy! Real Python coders, please feel free to weigh in if you see an issue.

 

Dan

 

  • Like 1
Link to comment
  • 2 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...