# 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:
# This script shows how and why you can receive UnbindExceptionError.
#


from pyfbsdk import *
from pyfbsdk_additions import *
from unbind import *

# Tool creation will serve as the hub for all other controls
t = CreateUniqueTool("Label Example")

l = FBLabel()
l.Caption = "This is a label!!!!"

t.StartSizeX = 200
t.StartSizeY = 100

x = FBAddRegionParam(10,FBAttachType.kFBAttachLeft,"")
y = FBAddRegionParam(10,FBAttachType.kFBAttachTop,"")
w = FBAddRegionParam(100,FBAttachType.kFBAttachNone,"")
h = FBAddRegionParam(25,FBAttachType.kFBAttachNone,"")

t.AddRegion("Label","Label", x, y, w, h)

t.SetControl("Label",l)

def OnUnbind(control, event):
    print "***** ", control, " has been Unbound *****"

l.OnUnbind.Add(OnUnbind)

# At that point, the tool is destroy, the internal Label are also destroy.
# Python is notify about its internal Undinbing.
DestroyTool(t)

# if you make this call, you get an exception because this Python wrapper is not linked
# to a MotionBuilder label anymore.

print "Trying to access an unbound Ui Control... this will raise an exception"
l.Caption