Part 4 – On Event Plugins

If you have worked much with any programming language you should be familiar with Triggers/Events/Callbacks or one of the other many names for them. If not, don’t worry; Events in Xsi are really easy. Basically an On Event plugin is run when a certain event takes place. So if we create a “siOnSelectionChange” Event, the plugin will be run every time change what is selected i.e. by selecting something else.

So to illustrate this I’m going to create two cubes, a Polygon Mesh cube and a Surface cube. If I set my Viewport to dislpay Shaded, the two cubes look exactly the same. What I want is a quick way to tell what “Type” the object I have selected is so I can easily tell if it’s a Polygon Mesh, Surface or whatever else. For this we want our plugin to check what Type the selection is every time we change what we have selected, so the “siOnSelectionChange” Event will be perfect for this.

To create an On Event plugin you start the same way as every other plugin.

Click File >> Plug-in Manager >> File >> New >> Event

Then we setup the plugin. I’ve set the name to “SelectedType_SelectionEvent” so that just by looking at the plugin name I get an idea of what it does.

After that click on the “Event Definition” tab to select the event you want. Near the bottom you’ll find the “siOnSelectionChange” Event.

Once you’ve checked that, scroll to the top and hit the “Generate Code” button.

To get the selected object’s Type we can do something like:

# Get a the selected objects and store in a list called "selected_lst"
selected_lst = Application.Selection

# If no objects are selected
if len(selected_lst) == 0:
    Application.LogMessage("No Selection")

# If there is more than one object selected
elif len(selected_lst) > 1:
    Application.LogMessage("Multi Selection")

# Otherwise there must only be one object selected
else:
    # Note if I want to get the first item in a list I use my_list[0] and not my_list[1]
    # A list's order actually goes [0, 1, 2, 3]
    # So if I have a list ["banana", "apple", "pear", "grape"], "pear" is at position [2]
    selected_obj = selected_lst[0]
    # Log the type of the selected object
    Application.LogMessage(selected_obj.Type)

 

And just like with the commands, all we need to do is add our code to the plugin’s “_Execute” function and then save the plugin.

So now when I select an object, the plugin tells me what I’ve got selected:

And as soon as I select something else or even if I select nothing the plugin will detect the selection has changed and Log what ever we told it to.
The entire plugin looks like this:

# SelectedType_SelectedEvent
# Initial code generated by Softimage SDK Wizard
# Executed Wed Aug 1 17:57:30 UTC+0200 2012 by jared.glass
#
# Tip: To add a command to this plug-in, right-click in the
# script editor and choose Tools > Add Command.
import win32com.client
from win32com.client import constants

null = None
false = 0
true = 1

def XSILoadPlugin( in_reg ):
    in_reg.Author = "jared.glass"
    in_reg.Name = "SelectedType_SelectionEvent"
    in_reg.Major = 1
    in_reg.Minor = 0

    in_reg.RegisterEvent("siOnSelectionChangeEvent",constants.siOnSelectionChange)
    #RegistrationInsertionPoint - do not remove this line

    return true

def XSIUnloadPlugin( in_reg ):
    strPluginName = in_reg.Name
    Application.LogMessage(str(strPluginName) + str(" has been unloaded."),constants.siVerbose)
    return true
    # Callback for the siOnSelectionChangeEvent event.

def siOnSelectionChangeEvent_OnEvent( in_ctxt ):
    Application.LogMessage("siOnSelectionChangeEvent_OnEvent called",constants.siVerbose)

    Application.LogMessage("ChangeType: " + str(in_ctxt.GetAttribute("ChangeType")),constants.siVerbose)

    # Get a the selected objects and store in a list called "selected_lst"
    selected_lst = Application.Selection

    # If no objects are selected
    if len(selected_lst) == 0:
        Application.LogMessage("No Selection")

    # If there is more than one object selected
    elif len(selected_lst) > 1:
        Application.LogMessage("Multi Selection")

    # Otherwise there must only be one object selected
    else:
        # Note if I want to get the first item in a list I use my_list[0] and not my_list[1]
        # A list's order actually goes [0, 1, 2, 3]
        # So if I have a list ["banana", "apple", "pear", "grape"], "pear" is at position [2]
        selected_obj = selected_lst[0]
        # Log the type of the selected object
        Application.LogMessage(selected_obj.Type)

    # Return value is ignored as this event can not be aborted.
    return true

 

2 thoughts on “Part 4 – On Event Plugins

  1. Pingback: Start Here!!! « Conquering the Code in Softimage Xsi

  2. Pingback: Start Here!!! | Conquering the Code in Softimage Xsi

Leave a comment