I've been using SI since 7 years now, and i always have a stupid question but... there is a way to alphabetically reorganize materials in materials manager ?
thats always really suck, there appear by creation order
i know that stupid...
Code: Select all
# -----------------------------------------------------------------------------------------------------------------
# Creates a new material library and copies the items in the selected material library to the new one, but sorted.
#
# Select a Material library and run.
# NOTE: As it creates a new material library, it will be added in the last tab of the material manager.
# Also keep in mind it will skip any locked material library (eg: referenced)
# -----------------------------------------------------------------------------------------------------------------
from win32com.client import constants as C
import re
import functools
xsi = Application
log = xsi.LogMessage
sel = xsi.Selection
def tempSetValues(newVals):
def closure(func):
@functools.wraps(func)
def inner(*args, **kwargs):
# Save current values and set new ones
oldVals = {}
for k, v in newVals.iteritems():
oldVals[k] = xsi.GetValue(k)
xsi.SetValue(k, v, "")
# Run the wrapped function
try:
return func(*args, **kwargs)
finally:
# Restore the old values
for k, v in oldVals.iteritems():
xsi.SetValue(k, v, "")
return inner
return closure
def natural_sort(list):
""" Sort the given list in the way that humans expect.
"""
convert = lambda text: int(text) if text.isdigit() else text
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
list.sort(key=alphanum_key)
@tempSetValues({'preferences.scripting.cmdlog': False})
def sortMaterialLibrary(sel):
if sel.Count != 1:
log("Please select one single Material Library", C.siWarning)
else:
if "library_source" not in sel(0).Type:
log("Invalid Selection: must be a material library. Aborting.", C.siWarning)
else:
matLib = sel(0)
if not matLib.IsLocked():
original_name = matLib.Name
matLib.Name = "%s_old" % matLib.Name
mat_names = []
for i in range(matLib.Items.Count):
mat_names.append(matLib.Items(i).Name)
natural_sort(mat_names)
rtrn = xsi.CreateLibrary(original_name)
newLib = rtrn(0)
for mat in range(len(mat_names)):
xsi.CopyPaste("%s.%s" % (matLib.FullName, mat_names[mat]), "", newLib.FullName, 1)
xsi.DeleteObj(matLib)
else:
log("Material Library '%s' is locked, aborting." % matLib.Name, C.siWarning)
sortMaterialLibrary(sel)
Code: Select all
# Python
from AL.xsi import xsi, log, c, utils
try:
xsi.BeginUndo()
dMats = {}
for eachItem in [x for x in xsi.Selection(0).Items if x.Name != "Scene_Material"]:
dMats[eachItem.Name] = eachItem
oTmpMatLib = xsi.CreateLibrary("MyTmpLib", 1, "")(0)
for eachMat in sorted(dMats):
log(eachMat)
xsi.CopyPaste(dMats[eachMat].FullName, "", oTmpMatLib.FullName, 1)
for eachMat in sorted(dMats):
log(dMats[eachMat].FullName)
xsi.CopyPaste(dMats[eachMat].FullName, "", xsi.Selection(0).FullName, 1)
xsi.SetCurrentMaterialLibrary(xsi.Selection(0).FullName)
xsi.DeleteObj(oTmpMatLib)
finally:
xsi.EndUndo()