pboucher
3/18/2011 - 3:02 PM

Plugin to run code in scene annotations when Softimage events occur.

Plugin to run code in scene annotations when Softimage events occur.

def main():
	if not Annotation.Parameters('flag1').Value:
		Application.LogMessage('Processing Post Import Script!')
		Annotation.Parameters('flag1').Value = True

main()
Annotation.Parent3DObject.Properties('runOnEndFileImport').Parameters('flag1').Value = True
Annotation.Parent3DObject.Properties('runOnEndFileImport').Parameters('flag1').Value = False
SI_GLOBALS = globals().copy()

import traceback

from win32com.client import constants as c
xsi = Application
log = xsi.LogMessage


ACTIVE_EVENTS = [
	'siOnBeginFileExport',
	'siOnBeginFileImport',
	'siOnBeginSequence',
	'siOnEndFileExport',
	'siOnEndFileImport',
	'siOnEndSceneSave',
	'siOnEndSequence',
]


def XSILoadPlugin(reg):
	reg.Author = "pboucher"
	reg.Name = "CodeRunner"
	reg.Major = 1
	reg.Minor = 0


	for eventName in ACTIVE_EVENTS:
		funcName = _makeFuncNameFromEventName(eventName)
		reg.registerEvent(funcName, getattr(c, eventName))

	#RegistrationInsertionPoint - do not remove this line

	return True


def processEvent(ctxt, eventName):
	annotationName = 'run' + eventName[2:]
	for note in _getAnnotations(annotationName):
		log('Processing code in %s.' % note.FullName)
		gbls = SI_GLOBALS.copy()
		gbls['Annotation'] = note
		code = note.Text.Value.replace('\r\n', '\n')
		try:
			exec code in gbls
		except Exception, err:
			msg = 'An error occured processing code in %s.\n\n' % note.FullName
			msg += traceback.format_exc()
			log(msg, c.siError)
			break


def _getAnnotations(name):
	"""Bottom up walk of models"""
	for model in _walkModel(xsi.ActiveSceneRoot):
		for note in model.Properties:
			if note.IsClassOf(c.siCustomPropertyID) and str(note.Name).startswith(name):
				yield note


def _walkModel(model):
	# Move yield to for top down
	for subModel in model.Models:
		for ret in _walkModel(subModel):
			yield ret
	yield model


# Create the event handling functions

def _makeFuncNameFromEventName(eventName):
	return 'codeRunner' + eventName[2:]


def _makeEventHandlerFunction(eventName):
	funcName = _makeFuncNameFromEventName(eventName) + '_OnEvent'
	def eventHandler(ctxt):
		processEvent(ctxt, eventName)
	eventHandler.__name__ = funcName
	return eventHandler


for eventName in ACTIVE_EVENTS:
	eventHandler = _makeEventHandlerFunction(eventName)
	globals()[eventHandler.__name__] = eventHandler