#LookThroughSpot...

#normal includes...
import win32com.client
from win32com.client import constants as xsiconst

#xsi specific globals...
app = Application

###############################################################################
#def            XSILoadPlugin
#description    registers commands and menus for the plugin with XSI
###############################################################################
def XSILoadPlugin( in_reg ):
    in_reg.Author = "stevec"
    in_reg.Name = "LookThroughSpot"
    in_reg.Email = "steven@steven-caron.com"
    in_reg.URL = "http://www.steven-caron.com/lookthroughspot.htm"
    in_reg.Major = 1
    in_reg.Minor = 0
    
    #add commands to the plugin...
    in_reg.RegisterCommand("LookThroughSpot","LookThroughSpot")
    
    #add menus to the plugin...
    in_reg.RegisterMenu(xsiconst.siMenuVMViewTypeID,"LookThroughSpotMenu",False)
    in_reg.RegisterMenu(xsiconst.siMenuSEObjectContextID,"LookThroughMenu",True)
    in_reg.RegisterMenu(xsiconst.siMenu3DViewObjectContextID,"LookThroughMenu",True)

    
    return True

###############################################################################
#def            XSIUnloadPlugin
#description    any plugin cleanup goes in this function
###############################################################################
def XSIUnloadPlugin( in_reg ):
    strPluginName = in_reg.Name
    
    #unloading plugin...
    app.LogMessage(str(strPluginName) + str(" has been unloaded."))
    
    return True


###############################################################################
#def             LookThroughSpot_Init
#description     assigns command return value and description and adds all 
#                necessary arguments to the command handler
###############################################################################
def LookThroughSpot_Init( ctxt ):
    oCmd = ctxt.Source
    oCmd.Name = "LookThroughSpot"
    oCmd.ReturnValue = True
    
    #handlers...
    return True

###############################################################################
#def             LookThroughSpot_Execute
#description     function to look through a selected light
###############################################################################
def LookThroughSpot_Execute( ):
    
    #get first object in selection...
    sel = app.Selection(0)
    
    #allow only spots to be selected
    #the menu filter is filtering lights, but not spotlights, if you try to look
    #through any other type xsi crashes
    if sel.OGLLight.Type != xsiconst.siLightSpot:
        app.logmessage("[ERROR] Please select a spot light!", xsiconst.siError)
        return False
    
    #dictionary to map 'GetFocusedViewport' return value to an integer
    views = {"A":0, "B":1, "C":2, "D":3}
    
    #get the active view
    activeView = app.GetFocusedViewport()
    
    #set that viewport's spotcamera to the selected light
    app.SetViewCamera(sel.FullName, views[activeView])
    
    return True

###############################################################################
#def             LookThroughSpotMenu_Init
#description     defines menu callback items and names the menu
###############################################################################
def LookThroughSpotMenu_Init( ctxt ):
    menu = ctxt.Source
    menu.Filter = xsiconst.siLightPrimType
    #menu.Name = "Look Through Light"
    
    menu.AddCommandItem("Look Through Selected Light", "LookThroughSpot")

    return True

###############################################################################
#def             LookThroughMenu_Init
#description     defines menu callback items and names the menu
###############################################################################
def LookThroughMenu_Init( ctxt ):
    menu = ctxt.Source
    menu.Filter = xsiconst.siLightPrimType
    menu.Name = "Look Through..."
        
    menu.AddCommandItem("Active Viewport", "LookThroughSpot")
    menu.AddSeparatorItem()
    menu.AddCallbackItem("Viewport A","LookThroughSpot_OnClick")
    menu.AddCallbackItem("Viewport B","LookThroughSpot_OnClick")
    menu.AddCallbackItem("Viewport C","LookThroughSpot_OnClick")
    menu.AddCallbackItem("Viewport D","LookThroughSpot_OnClick")

    return True

###############################################################################
#def             LookThroughSpot_OnClick
#description     callback function for when menu button is clicked
###############################################################################
def LookThroughSpot_OnClick( ctxt ):
    menu = ctxt.Source
    
    if menu.Name == "Viewport A":
        app.SetFocusedViewport("A")
        app.LookThroughSpot()
        return True
    if menu.Name == "Viewport B":
        app.SetFocusedViewport("B")
        app.LookThroughSpot()
        return True
    if menu.Name == "Viewport C":
        app.SetFocusedViewport("C")
        app.LookThroughSpot()
        return True
    if menu.Name == "Viewport D":
        app.SetFocusedViewport("D")
        app.LookThroughSpot()
        return True

    return True
