Saturday snippet: Converting strings to objects


XSICollections know how to handle (aka parse) string expressions like ‘cube.pnt[2,4,LAST]’

si = Application
import win32com.client
c = win32com.client.Dispatch( "XSI.Collection" )

#c.SetAsText( 'cube.pnt[2,4,LAST]' )
c.Items = 'cube.pnt[2,4,LAST]'

print c.Count
print si.ClassName( c(0) )
print c(0).SubComponent.ComponentCollection.Count
print si.ClassName( c(0).SubComponent.ComponentCollection(0) )
print c(0).SubComponent.ComponentCollection(2).Index
# 1
# CollectionItem
# 3
# Vertex
# 7

Back in 1999, this code looked something like this:

CreatePrim "Cube", "MeshSurface"
set list = GetCollection( "cube.pnt[2,3,6,LAST]" )

if not typename(list) = "Nothing" then
	logmessage list
end if

function GetCollection( in_str )
	Dim l_myList 

	set GetCollection = CreateObject( "Sumatra.Collection" )

	On Error Resume Next
	GetCollection.items = in_str

	if GetCollection.Count = 0 then
		set GetCollection = Nothing
	end if

end function

One thought on “Saturday snippet: Converting strings to objects

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s