As I posted yesterday, an ICE compound with no category and no task will not show up in the Preset Manager. Here’s a Python script that checks .xsicompound files and reports any that are missing both the category and tasks attributes.
I use ElementTree to parse the .xsicompound XML, and get the category and tasks attributes from the xsi_file element, which looks something like this:
<xsi_file type="CompoundNode" name="abScatter" author="Andreas Bystrom" url="http://www.wurp.net" formatversion="1.4" compoundversion="1.0" constructionmode="Modeling" backgroundcolor="7765887">
Here’s the script.
from siutils import si # Application
from siutils import sidict # Dictionary
from siutils import sisel # Selection
from siutils import siuitk # XSIUIToolkit
from siutils import siut # XSIUtils
from siutils import log # LogMessage
from siutils import disp # win32com.client.Dispatch
from siutils import C # win32com.client.constants
from xml.etree import ElementTree as ET
import os, fnmatch
#
# Generator function for finding files
#
def find_files(directory, pattern):
for root, dirs, files in os.walk(directory):
for basename in files:
if fnmatch.fnmatch(basename, pattern):
filename = os.path.join(root, basename)
yield filename
#
# Check .xsicompound file for category and tasks attributes
#
def check_xsicompound( f ):
try:
tree = ET.parse( f )
except Exception, inst:
print "Unexpected error opening %s: %s" % (f, inst)
# Get the xsi_file element
xsi_file = tree.getroot()
# name = xsi_file.attrib['name']
# Check the category and task elements
cat = False
tasks = False
if 'category' in xsi_file.attrib and xsi_file.attrib['category'] != '':
cat = True
if 'tasks' in xsi_file.attrib and xsi_file.attrib['tasks'] != '':
tasks = True
# return False if both are blank
return cat or tasks
#
#
#
# list of compounds with no category and no tasks
compounds = []
# check all compounds in all workgroups
for wg in si.Workgroups:
d = siut.BuildPath( wg, "Data", "Compounds" );
for filename in find_files(d, '*.xsicompound'):
b = check_xsicompound( filename )
if not b:
compounds.append( filename )
log( "%d compounds found with no category and no tasks:" % (len(compounds)) )
for f in compounds:
log( f )
I can’t execute the script in 2011 SAP without getting this error:
[error on line 1.]
You need to replace the from siutils import statements with this:
from win32com.client import Dispatch as disp from win32com.client import constants as C si = disp('XSI.Application') log = si.LogMessage sidesk = si.Desktop sidict = si.Dictionary sifact = disp('XSI.Factory') simath = disp('XSI.Math') siproj = si.ActiveProject2 sisel = si.Selection siuitk = disp('XSI.UIToolkit') siut = disp('XSI.Utils')Works. Thank you. Nice Script π
# ERROR : Traceback (most recent call last):
# File “”, line 65, in
# b = check_xsicompound( filename )
# File “”, line 32, in check_xsicompound
# print “Unexpected error opening %s: %s” % (xml_file, inst)
# NameError: global name ‘xml_file’ is not defined
# – [line 32]
SI2012.5 Win7/64
On line 32, change xml_file to f:
print “Unexpected error opening %s: %s” % (f, inst)
brilliant!
works perfect now. thanks