Skip to content

Instantly share code, notes, and snippets.

@tcrowson
Last active October 2, 2015 19:39
Show Gist options
  • Save tcrowson/dbfed445f201fbcf2195 to your computer and use it in GitHub Desktop.
Save tcrowson/dbfed445f201fbcf2195 to your computer and use it in GitHub Desktop.
Softimage script for converting a selection to Redshift Proxy, and re-importing it into the scene with some customizations.
# ConvertToRedshiftProxy
#
# - Adds a new command to the context menu for objects in the Scene Explorer: 'Convert to Redshift Proxy'
#
# FEATURES
# - Exports the selected items to a Redshift Proxy, then re-imports the result back into the scene.
# - Optionally maintains parenting, but only to the parent of the first selected item.
# - Appends a suffix (defaults to '_RSPROXY') to the name of the proxy mesh, to distinguish it from other polymeshes.
# - Locks the Construction stack on the proxy mesh to prevent it being frozen by accident.
# - Optionally lets you specify a display mesh for GL previewing.
# - Options for handling the source items:
# 1. Group the source items in a group called '[proxyname]_SOURCE_ITEMS' and hides the group.
# 2. Delete the source items.
# 3. Do nothing with the source items.
import os
import win32com.client
from win32com.client import constants
version = "1.0"
def XSILoadPlugin( in_reg ):
'''
Boilerplate
'''
in_reg.Author = 'Tim Crowson'
in_reg.Name = 'ConvertToRedshiftProxy'
in_reg.Major = 1
in_reg.Minor = 0
in_reg.RegisterProperty('Convert_To_Redshift_Proxy')
in_reg.RegisterCommand("AddRSConversionProp","AddRSConversionProp")
in_reg.RegisterMenu(constants.siMenuSEObjectContextID,"AddRSConversionProp_Menu",False, False)
return True
def XSIUnloadPlugin( in_reg ):
'''
Boilerplate
'''
strPluginName = in_reg.Name
Application.LogMessage(str(strPluginName) + str(' has been unloaded.'),constants.siVerbose)
return True
def AddRSConversionProp_Init( in_ctxt ):
'''
Boilerplate
'''
oCmd = in_ctxt.Source
oCmd.Description = ""
oCmd.ReturnValue = True
return True
def AddRSConversionProp_Menu_Init( in_ctxt ):
'''
Boilerplate
'''
oMenu = in_ctxt.Source
oMenu.AddCommandItem("Convert to Redshift Proxy","AddRSConversionProp")
return True
def AddRSConversionProp_Execute( ):
'''
Executes when calling the AddRSConversionProp command
'''
if not 'Convert_To_Redshift_Proxy' in [x.name for x in Application.ActiveSceneRoot.Properties]:
Application.AddProp('Convert_To_Redshift_Proxy', 'Scene_Root')
Application.InspectObj('Convert_To_Redshift_Proxy')
return True
def Convert_To_Redshift_Proxy_Define( in_ctxt ):
'''
Define our custom property's parameters
'''
oProp = in_ctxt.Source
oProp.AddParameter3('sourceAction', constants.siInt2, 0, 0, 2, False, False)
oProp.AddParameter3('suffix', constants.siString, '_RSPROXY')
oProp.AddParameter3('parenting', constants.siInt2, 0, 0, 1, False, False)
oProp.AddParameter3('useDisplayMesh', constants.siBool, 0, 0, 1, False, False)
oProp.AddParameter3('meshName', constants.siString, '', '', '', False, True)
return True
def Convert_To_Redshift_Proxy_DefineLayout( in_ctxt ):
'''
Define our custom property's layout
'''
layout = in_ctxt.Source
layout.Clear()
sourceActCombo = layout.AddItem('sourceAction', 'Source Items', constants.siControlCombo)
sourceActCombo.UIItems = ['Group and Hide', 0, 'Delete Source Items', 1, 'Do Nothing', 2]
sourceActCombo.SetAttribute(constants.siUILabelMinPixels, 80)
parentingCombo = layout.AddItem('parenting', 'Parenting', constants.siControlCombo)
parentingCombo.UIItems = ['Same as First Selected', 0, 'Scene Root', 1]
parentingCombo.SetAttribute(constants.siUILabelMinPixels, 80)
suffix = layout.AddItem('suffix', 'Suffix')
suffix.SetAttribute(constants.siUILabelMinPixels, 80)
layout.AddGroup('Display Mesh')
layout.AddItem('useDisplayMesh', 'Use Custom Mesh for GL Preview')
layout.AddRow()
meshName = layout.AddItem('meshName', 'Mesh Name')
meshName.SetAttribute(constants.siUILabelMinPixels, 80)
pickBtn = layout.AddButton('pickMesh', 'Pick')
pickBtn.SetAttribute(constants.siUIButtonDisable, True)
layout.EndRow()
layout.EndGroup()
layout.AddSpacer(5, 10)
convertBtn = layout.AddButton('convert', 'Convert to Redshift Proxy')
convertBtn.SetAttribute(constants.siUICX, 315)
convertBtn.SetAttribute(constants.siUICY, 25)
return True
def Convert_To_Redshift_Proxy_OnInit( ):
'''
Boilerplate
'''
def Convert_To_Redshift_Proxy_OnClosed( ):
'''
Boilerplate
'''
def Convert_To_Redshift_Proxy_useDisplayMesh_OnChanged( ):
'''
Callback for the 'Use Display Mesh' Checkbox
'''
state = not PPG.useDisplayMesh.Value
PPG.meshName.ReadOnly = state
PPG.PPGLayout.Item('pickMesh').SetAttribute(constants.siUIButtonDisable, state)
PPG.Refresh()
def Convert_To_Redshift_Proxy_pickMesh_OnClicked( ):
'''
Callback for the 'Pick' Button
'''
mesh = Application.PickElement(constants.siPolyMeshFilter, 'Pick a mesh', 'Pick a mesh')[2]
PPG.meshName = mesh
PPG.Refresh()
def filebrowser():
'''
Open a file browser to specify the file name for the export.
'''
browser = XSIUIToolkit.FileBrowser
browser.DialogTitle = 'Select a location'
startDir = os.path.join(Application.ActiveProject2.Path, 'assets')
if not os.path.exists(startDir):
startDir = Application.ActiveProject2.Path
browser.InitialDirectory = startDir
browser.Filter = "Redshift Proxy (*.rs)|*.rs||"
browser.ShowSave()
return browser.FilePathName
def Convert_To_Redshift_Proxy_convert_OnClicked( ):
'''
Callback for the 'Convert' Button
'''
# store the current selection of items
selection = [x for x in Application.Selection]
proxyfile = filebrowser() # browse for a save location
if proxyfile != '':
proxyName = os.path.splitext( os.path.basename(proxyfile) )[0]
# Export the Proxy
start = Application.GetValue('PlayControl.Current')
end = Application.GetValue('PlayControl.Current')
Application.Redshift_ExportRedshiftProxy(proxyfile, True, start, end, 1)
# Load the Proxy
proxyOp = Application.Redshift_ApplyRedshiftProxyToMesh()
hostMesh = proxyOp.parent.parent
# Set suffix
if PPG.suffix.Value != '':
hostMesh.name = proxyName + PPG.suffix.Value
# Set parent
if PPG.parenting.Value == 0:
Application.ParentObj(selection[0].parent, hostMesh)
# Point to the file
proxyOp.RedshiftProxyFile = proxyfile
# Lock Construction History to prevent freezing
Application.Lock(proxyOp.parent, "siLockLevelConstruction")
# Group and Hide Source Items
if PPG.sourceAction.Value == 0:
inputObjs = [str(x) for x in selection]
grp = Application.CreateGroup('%s_SOURCE_ITEMS'%hostMesh.name, inputObjs)
grp.viewvis = 0
grp.rendvis = 0
# Delete Source Items
elif PPG.sourceAction.Value == 1:
for x in selection:
Application.DeleteObj(x)
# Link Display Mesh
if PPG.useDisplayMesh.Value == 1:
meshName = PPG.meshName.Value
if Application.Dictionary.GetObject(meshName):
Application.Redshift_LinkDisplayMeshToProxy(proxyOp, PPG.meshName.Value)
proxyOp.DisplayMode = 2
Application.SetValue("%s.visibility.viewvis" %meshName, False)
Application.SetValue("%s.visibility.rendvis" %meshName, False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment