Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
XSI Example: Random Component or Object Selections
from win32com.client import constants as c
import random
xsi = Application
XSIDialog = XSIFactory.CreateObject("XSIDial.XSIDialog")
def get_random_percentage(iterable, percentage):
amount = int( float(len(iterable)) * (percentage/100.0) )
return random.sample(iterable, amount)
def selectRandomComponents(coll, percentage, cmpType = 'ask'):
allComponents = {
'edges':[x for obj in coll for x in obj.ActivePrimitive.Geometry.Edges],
'points':[x for obj in coll for x in obj.ActivePrimitive.Geometry.Points],
'faces':[x for obj in coll for x in obj.ActivePrimitive.Geometry.Facets]
cmpTypes = allComponents.keys()
if cmpType == 'ask':
cmpType = cmpTypes[ XSIDialog.ComboEx('Component type?',cmpTypes,2) ]
sample = get_random_percentage(allComponents[cmpType], percentage)
def selectRandomObjects(coll, percentage):
sample = get_random_percentage(coll, percentage)
def main():
selectComponents = (
"Select components?\n\nYES = components\nNO = objects",
"Random Selector" ) == c.siMsgYes
percentage = float(xsi.XSIInputBox(
"What percentage to select?",
"Selection percentage?",
50 # default %
if selectComponents:
selectRandomComponents(xsi.Selection, percentage)
selectRandomObjects(xsi.Selection, percentage)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.