Saturday Snippet: Getting a list of properties and methods with Python introspection


If you’ve ever wanted to get a list of properties and methods support by an object, here’s how.
Copied from the Softimage wiki

def GetFunctions( dynDisp ):
	"""returns a sorted and unique list of all functions defined in a dynamic dispatch"""
	dict = {}
	try:
		for iTI in xrange(0,dynDisp._oleobj_.GetTypeInfoCount()):
			typeInfo = dynDisp._oleobj_.GetTypeInfo(iTI)
			typeAttr = typeInfo.GetTypeAttr()
			for iFun in xrange(0,typeAttr.cFuncs):
				funDesc = typeInfo.GetFuncDesc(iFun)
				name = typeInfo.GetNames(funDesc.memid)[0]
				dict[name] = 1
	except:
		pass # Object is not the dynamic dispatch I knew
	ret = dict.keys()
	ret.sort()
	return ret

import pprint

funcs = GetFunctions(Application)
Application.LogMessage(pprint.pformat(funcs))

funcs = GetFunctions(Application.ActiveSceneRoot)
Application.LogMessage(pprint.pformat(funcs))

1 thought on “Saturday Snippet: Getting a list of properties and methods with Python introspection

  1. My god that’s useful. Thanks. I’ve always wondered how you interrogate the dispatch interface to find this stuff out.

    Also.. I can’t quite believe how hard it is to do that! The COM stuff always seems almost intentionally obfuscated.

    BTW, you’re using a dictionary like a Set. You might want to just use a Set. (Not to mention that dict is an object type and mustn’t be used as a variable name. You’ll break any code that instantiates the dictionary type, e.g. myDictionaryInstance = dict() )

    Thanks for the post though, dead useful.

    Andy

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 )

Facebook photo

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

Connecting to %s