Create a gist now

Instantly share code, notes, and snippets.

A small python plug-in for (X)SI. Let's you set RGB or HSV colors. This applies the colors to the first vertex color cluster it finds for every model in the current selection. If it doesn't find a cluster it can create one if you check the Create Cluster option. I created a very basic script for a friend who needed to eliminate baked AO from the…
# Changes Vertex Color Values.
# This should go to <USER>\\Autodesk\\<Softimage Version>\\Application\\Plugins.
# It's not important how the file is named.
#
# Code Copyright (C) Benedikt Schatz http://schlechtwetterfront.github.com
#
# GNU General Public License http://www.gnu.org/licenses/.
#
# The command will be added to the Get>Property menu.
import colorsys
import os
from win32com.client import constants as const
xsi = Application
def XSILoadPlugin(in_reg):
in_reg.Author = 'Ande'
in_reg.Name = 'VertColChanger'
in_reg.Email = 'schlchtwtrfrnt@gmail.com'
in_reg.URL = 'https://sites.google.com/site/andescp/'
in_reg.Major = 1
in_reg.Minor = 1
in_reg.RegisterCommand('VertColChanger', 'VertColChanger')
in_reg.RegisterMenu(const.siMenuTbGetPropertyID, 'Edit Vertex Colors', True)
return True
def XSIUnloadPlugin(in_reg):
return True
def EditVertexColors_Init(in_ctxt):
menu = in_ctxt.Source
menu.AddCommandItem('Set Vertex Colors', 'VertColChanger')
def VertColChanger_Init(in_ctxt):
oCmd = in_ctxt.Source
oCmd.Description = ''
oCmd.ReturnValue = True
return True
def VertColChanger_Execute():
for plugin in xsi.Plugins:
if plugin.Name == 'VertColChanger':
path = plugin.OriginPath
for prop in xsi.ActiveSceneRoot.Properties:
if prop.Name == 'VertColChanger':
xsi.DeleteObj('VertColChanger')
pset = xsi.ActiveSceneRoot.AddProperty('CustomProperty', False, 'VertColChanger')
pset.AddParameter3('create', const.siBool, False, '', '', 0, 0)
pset.AddParameter3('colortype', const.siString, 'RGB')
pset.AddParameter3('red', const.siBool)
pset.AddParameter3('green', const.siBool)
pset.AddParameter3('blue', const.siBool)
pset.AddParameter3('hue', const.siBool, False, '', '', 0, 1)
pset.AddParameter3('saturation', const.siBool, False, '', '', 0, 1)
pset.AddParameter3('value', const.siBool, False, '', '', 0, 1)
pset.AddParameter3('alpha', const.siBool)
pset.AddParameter3('redv', const.siDouble, 0.0, 0.0, 1.0)
pset.AddParameter3('greenv', const.siDouble, 0.0, 0.0, 1.0)
pset.AddParameter3('bluev', const.siDouble, 0.0, 0.0, 1.0)
pset.AddParameter3('huev', const.siDouble, 0.0, 0.0, 1.0, 0, 1)
pset.AddParameter3('saturationv', const.siDouble, 0.0, 0.0, 1.0, 0, 1)
pset.AddParameter3('valuev', const.siDouble, 0.0, 0.0, 1.0, 0, 1)
pset.AddParameter3('alphav', const.siDouble, 0.0, 0.0, 1.0)
mlay = pset.PPGLayout
mlay.SetAttribute(const.siUILogicFile, '{0}\\vertcolchanger.py'.format(path))
mlay.Language = 'pythonscript'
agr = mlay.AddGroup
egr = mlay.EndGroup
text = mlay.AddStaticText
item = mlay.AddItem
arow = mlay.AddRow
erow = mlay.EndRow
agr('Vertex Color Changer', 1)
arow()
item('create', 'Create Cluster')
mlay.AddEnumControl('colortype', ('RGB', 'RGB', 'HSV', 'HSV'), 'Color Space', const.siControlRadio)
btn = mlay.AddButton('change', 'CHANGE!')
btn.SetAttribute(const.siUICX, 60)
btn.SetAttribute(const.siUICY, 43)
erow()
agr('Colors')
arow()
agr('', 0) # color labels
text('Red')
text('Green')
text('Blue')
text('Hue')
text('Saturation')
text('Value')
text('Alpha')
egr()
ctrlgr = agr('', 0)
arow()
red = item('red', '')
red.SetAttribute('ValueOnly', True)
redv = item('redv', '')
redv.SetAttribute('ValueOnly', True)
erow()
arow()
green = item('green', '')
green.SetAttribute('ValueOnly', True)
greenv = item('greenv', '')
greenv.SetAttribute('ValueOnly', True)
erow()
arow()
blue = item('blue', '')
blue.SetAttribute('ValueOnly', True)
bluev = item('bluev', '')
bluev.SetAttribute('ValueOnly', True)
erow()
arow()
hue = item('hue', '')
hue.SetAttribute('ValueOnly', True)
huev = item('huev', '')
huev.SetAttribute('ValueOnly', True)
erow()
arow()
saturation = item('saturation', '')
saturation.SetAttribute('ValueOnly', True)
saturationv = item('saturationv', '')
saturationv.SetAttribute('ValueOnly', True)
erow()
arow()
value = item('value', '')
value.SetAttribute('ValueOnly', True)
valuev = item('valuev', '')
valuev.SetAttribute('ValueOnly', True)
erow()
arow()
alpha = item('alpha', '')
alpha.SetAttribute('ValueOnly', True)
alphav = item('alphav', '')
alphav.SetAttribute('ValueOnly', True)
erow()
egr()
ctrlgr.SetAttribute('WidthPercentage', 90)
erow()
egr()
egr()
xsi.InspectObj(pset, '', '', 'siLock')
return True
def colortype_OnChanged():
ppg = PPG.Inspected(0)
if ppg.Parameters('colortype').Value == 'RGB':
ppg.Parameters('red').ReadOnly = False
ppg.Parameters('redv').ReadOnly = False
ppg.Parameters('green').ReadOnly = False
ppg.Parameters('greenv').ReadOnly = False
ppg.Parameters('blue').ReadOnly = False
ppg.Parameters('bluev').ReadOnly = False
ppg.Parameters('hue').ReadOnly = True
ppg.Parameters('huev').ReadOnly = True
ppg.Parameters('saturation').ReadOnly = True
ppg.Parameters('saturationv').ReadOnly = True
ppg.Parameters('value').ReadOnly = True
ppg.Parameters('valuev').ReadOnly = True
else:
ppg.Parameters('red').ReadOnly = True
ppg.Parameters('redv').ReadOnly = True
ppg.Parameters('green').ReadOnly = True
ppg.Parameters('greenv').ReadOnly = True
ppg.Parameters('blue').ReadOnly = True
ppg.Parameters('bluev').ReadOnly = True
ppg.Parameters('hue').ReadOnly = False
ppg.Parameters('huev').ReadOnly = False
ppg.Parameters('saturation').ReadOnly = False
ppg.Parameters('saturationv').ReadOnly = False
ppg.Parameters('value').ReadOnly = False
ppg.Parameters('valuev').ReadOnly = False
def change_OnClicked():
ppg = PPG.Inspected(0)
if ppg.Parameters('colortype').Value == 'RGB':
change_rgb(ppg)
else:
change_hsv(ppg)
def change_rgb(ppg):
pb = XSIUIToolkit.ProgressBar
pb.Value = 0
pb.Maximum = 1
pb.Caption = 'Preparing...'
pb.Visible = True
param = ppg.Parameters
dored = param('red').Value
dogreen = param('green').Value
doblue = param('blue').Value
doalpha = param('alpha').Value
if dored:
red = param('redv').Value
if dogreen:
green = param('greenv').Value
if doblue:
blue = param('bluev').Value
if doalpha:
alpha = param('alphav').Value
pb.Increment()
sel = xsi.Selection
everything = [mdl for mdl in sel]
pb.Value = 0
pb.Maximum = len(everything)
for mdl in everything:
pb.Caption = 'Changing Colors for {0}...'.format(mdl.Name)
vertcol = get_cluster(mdl)
if not vertcol:
if param('create').Value:
xsi.CreateVertexColorSupport()
vertcol = get_cluster(mdl)
else:
XSIUIToolkit.MsgBox('Couldnt find vertex colors for {0}. Aborting.'.format(mdl.Name), 0, 'Vertex Color Changer')
return
els = vertcol.Elements.Array
numverts = len(els[0])
if dored and dogreen and doblue and doalpha:
newels = [[red] * numverts, [green] * numverts, [blue] * numverts, [alpha] * numverts]
vertcol.Elements.Array = newels
return
newels = [[], [], [], []]
for n in xrange(numverts):
r, g, b, a = els[0][n], els[1][n], els[2][n], els[3][n]
if dored:
r = red
if dogreen:
g = green
if doblue:
b = blue
if doalpha:
a = alpha
newels[0].append(r)
newels[1].append(g)
newels[2].append(b)
newels[3].append(a)
vertcol.Elements.Array = newels
pb.Increment()
def change_hsv(ppg):
pb = XSIUIToolkit.ProgressBar
pb.Value = 0
pb.Maximum = 1
pb.Caption = 'Preparing...'
pb.Visible = True
param = ppg.Parameters
dohue = param('hue').Value
dosaturation = param('saturation').Value
dovalue = param('value').Value
doalpha = param('alpha').Value
if dohue:
hue = param('huev').Value
if dosaturation:
saturation = param('saturationv').Value
if dovalue:
value = param('valuev').Value
if doalpha:
alpha = param('alphav').Value
pb.Increment()
sel = xsi.Selection
everything = [mdl for mdl in sel]
pb.Value = 0
pb.Maximum = len(everything)
for mdl in everything:
pb.Caption = 'Changing Colors for {0}...'.format(mdl.Name)
vertcol = get_cluster(mdl)
if not vertcol:
if param('create').Value:
xsi.CreateVertexColorSupport()
vertcol = get_cluster(mdl)
else:
XSIUIToolkit.MsgBox('Couldnt find vertex colors for {0}. Aborting.'.format(mdl.Name), 0, 'Vertex Color Changer')
return
els = vertcol.Elements.Array
numverts = len(els[0])
newels = [[], [], [], []]
for n in xrange(numverts):
r, g, b, a = els[0][n], els[1][n], els[2][n], els[3][n]
h, s, v = colorsys.rgb_to_hsv(r, g, b)
if dohue:
h = hue
if dosaturation:
s = saturation
if dovalue:
v = value
if doalpha:
a = alpha
r, g, b = colorsys.hsv_to_rgb(h, s, v)
newels[0].append(r)
newels[1].append(g)
newels[2].append(b)
newels[3].append(a)
vertcol.Elements.Array = newels
pb.Increment()
def do_stuff():
sel = xsi.Selection(0)
everything = get_all_children(sel)
for mdl in everything:
vertcol = get_cluster(mdl)
els = vertcol.Elements.Array
# print els
numverts = len(els[0])
newels = [[], [], [], [1.0] * numverts]
for n in xrange(numverts):
# print 'rgb xsi', els[0][n], els[1][n], els[2][n]
h, s, v = colorsys.rgb_to_hsv(els[0][n], els[1][n], els[2][n])
# print 'hsv', h, s, v
v = .5
r, g, b = colorsys.hsv_to_rgb(h, s, v)
# print 'rgb', r, g, b
newels[0].append(r)
newels[1].append(g)
newels[2].append(b)
vertcol.Elements.Array = newels
# print newels
return True
def get_all_children(root):
'''Recursively adds children to a list.'''
children = []
children.append(root)
if root.Children(0):
chldrn = list(root.Children)
for child in chldrn:
children.extend(get_all_children(child))
return children
def get_cluster(model):
geo = model.ActivePrimitive.GetGeometry2(0)
sample_clusters = geo.Clusters.Filter('sample')
for cls in sample_clusters:
for prop in cls.Properties:
if prop.Type == 'vertexcolor':
return prop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment