public
Last active

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

  • Download Gist
SI_CodeRunner.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
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
runOnBeginFileExport
1
Annotation.Parent3DObject.Properties('runOnEndFileImport').Parameters('flag1').Value = False
runOnEndFileExport
1
Annotation.Parent3DObject.Properties('runOnEndFileImport').Parameters('flag1').Value = True
runOnEndFileImport
1 2 3 4 5 6
def main():
if not Annotation.Parameters('flag1').Value:
Application.LogMessage('Processing Post Import Script!')
Annotation.Parameters('flag1').Value = True
 
main()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.