Stupid question in material manager

General discussion about 3D DCC and other topics
fabilabo
Posts: 69
Joined: 28 Jan 2010, 12:13

Stupid question in material manager

Post by fabilabo » 23 Nov 2012, 09:42

Hi,

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...

User avatar
Rork
Posts: 1359
Joined: 09 Jul 2009, 06:59
Location: Close to The Hague, Netherlands

Re: Stupid question in material manager

Post by Rork » 23 Nov 2012, 09:50

No,

You can't. I ran into that a long time ago.
The only thing that kinda works, is exporting the library, and importing it back again. Than SI will arrange all shaders accorfing to name.
Yes, it's a silly workflow.

I had a script from someone that did that automagically, I'll see if I can find it for you.

rob
Last edited by Rork on 23 Nov 2012, 10:27, edited 3 times in total.
SI UI tutorials: Toolbar http://goo.gl/iYOL0l | Custom Layout http://goo.gl/6iP5xQ | RenderManager View http://goo.gl/b4ZkjQ
So long, and thanks for all the Fish!!

fabilabo
Posts: 69
Joined: 28 Jan 2010, 12:13

Re: Stupid question in material manager

Post by fabilabo » 23 Nov 2012, 10:04

ok thank you very much, that what i thought... hope they can read that and add it to the next version, i guess its not long to add a little fonction "by ordercreation" or by "alphabetical"

User avatar
Rork
Posts: 1359
Joined: 09 Jul 2009, 06:59
Location: Close to The Hague, Netherlands

Re: Stupid question in material manager

Post by Rork » 23 Nov 2012, 10:27

Here ya go, save as python script (.py). It was created by Orlando Esponda :-)

Workflow:
- Select the material library in the explorer view, and run the script in the script editor. Or make a button somewhere for this.
- A new material library is created with all materials nicely sorted by name. The new library is put at the end of the tabs in the material library with a new name.

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)
Moderator Edit: USE CODE TAGS! Please. :) - Eric T.
SI UI tutorials: Toolbar http://goo.gl/iYOL0l | Custom Layout http://goo.gl/6iP5xQ | RenderManager View http://goo.gl/b4ZkjQ
So long, and thanks for all the Fish!!

fabilabo
Posts: 69
Joined: 28 Jan 2010, 12:13

Re: Stupid question in material manager

Post by fabilabo » 23 Nov 2012, 16:10

very nice, thank you very much !

EricTRocks
Moderator
Posts: 754
Joined: 25 Nov 2009, 00:41

Re: Stupid question in material manager

Post by EricTRocks » 26 Nov 2012, 06:39

A more concise way though it may not handle the proper ordering:

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()
Eric Thivierge
Lead Kraken Developer, Fabric Engine
http://fabric-engine.github.io/Kraken

fabilabo
Posts: 69
Joined: 28 Jan 2010, 12:13

Re: Stupid question in material manager

Post by fabilabo » 26 Nov 2012, 08:33

should i have to install pyton ?

i have reinstall all my system (win 8 x64 + SI 2013 SP1 64)
Because it dont works.

I always wonder if i have to install python because there is a option preferences "use python installed with softimage (windows only)" checked

thanks

EricTRocks
Moderator
Posts: 754
Joined: 25 Nov 2009, 00:41

Re: Stupid question in material manager

Post by EricTRocks » 26 Nov 2012, 08:44

You shouldn't have to if that is checked. You do have to have the Material Library selected in the Scene Explorer though. Also after running the script you may need to change tabs in the material manager to see the results.
Eric Thivierge
Lead Kraken Developer, Fabric Engine
http://fabric-engine.github.io/Kraken