# Copyright 2009 Autodesk, Inc.  All rights reserved.
# Use of this software is subject to the terms of the Autodesk license agreement 
# provided at the time of installation or download, or which otherwise accompanies
# this software in either electronic or hard copy form.
#
# Script description:
# Show how to register callback to FBSystem about global connections and data notification.
# IMPORTANT: we need to remove these callbacks at the end of MoBu execution.
#
# Topic: FBSystem, FBApplication, FBSystem.OnConnectionNotify
#

from pyfbsdk import *

def OnUIIdle(control, event):
    print "UIIdle", control, event

def OnConnectionNotify(control, event):
    """
    FBEventConnectionNotify
    FBConnectionAction Action : Connection's action performed.
    int SrcIndex : Index of the source in the destination component.
    FBConnectionType ConnectionType: Connection's type.
    object SrcPlug : The source plug involved in the action.
    object DstPlug : The destination plug involved in the action.
    object NewPlug : New plug created by the action. (Mostly used by merge/replace)
    """
    print "OnConnectionNotify", event.Action, event.SrcIndex, event.ConnectionType, event.SrcPlug, event.DstPlug, event.NewPlug

def OnConnectionDataNotify(control, event):
    """
    FBEventConnectionDataNotify
    FBConnectionAction Action : Connection's action performed.
    object Plug : The plug involved in the action.
    """
    print "OnConnectionDataNotify", event.Action, event.Plug

def OnConnectionStateNotify(control, event):
    """
    FBEventConnectionStateNotify
    FBConnectionAction Action : Connection's action performed.
    object Plug : The plug involved in the action.
    """
    print "OnConnectionStateNotify", event.Action, event.Plug


# This notification broadcasts a LOT of events!
def Register():
    #sys.OnUIIdle.Add(OnUIIdle)
    sys.OnConnectionNotify.Add(OnConnectionNotify)
    sys.OnConnectionDataNotify.Add(OnConnectionDataNotify)
    sys.OnConnectionStateNotify.Add(OnConnectionStateNotify)

    # IMPORTANT: we need to remove all callbacks connected to FBSystem and FBApplication
    # At file exit to avoid notification while the python environement is unitialized.
    app.OnFileExit.Add(Unregister)

def Unregister(control=None, event=None):
    sys.OnConnectionNotify.Remove(OnConnectionNotify)
    sys.OnConnectionDataNotify.Remove(OnConnectionDataNotify)
    sys.OnConnectionStateNotify.Remove(OnConnectionStateNotify)
    app.OnFileExit.Remove(Unregister)

sys = FBSystem()
app = FBApplication()
Register()