Skip to content

Instantly share code, notes, and snippets.

@pboucher
Created March 18, 2011 15:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pboucher/876213 to your computer and use it in GitHub Desktop.
Save pboucher/876213 to your computer and use it in GitHub Desktop.
Plugin to run code in scene annotations when Softimage events occur.
Annotation.Parent3DObject.Properties('runOnEndFileImport').Parameters('flag1').Value = False
Annotation.Parent3DObject.Properties('runOnEndFileImport').Parameters('flag1').Value = True
def main():
if not Annotation.Parameters('flag1').Value:
Application.LogMessage('Processing Post Import Script!')
Annotation.Parameters('flag1').Value = True
main()
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment