Sort by Object Name

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 08 August 2013   #16
enhanced V14 script request

Hello Lennart

Firstly I would like to thank you for all time I have saved with your super little sorting script. I am now using V14 at my workplace and would love to expand the functionality to group objects by object name.

All objects with the same name would be grouped under a null object which could be called for e.g.

Object nameX Group

is this a simple process? Would you or another CG talk person be willing to help.

Is it possible to also sort within existing grouped objects. At the moment I expand any preexisting groups that occur on import before I am able to run the sorting script.

Best regards

alawrence

Last edited by alawrence : 08 August 2013 at 09:34 AM.
 
Old 08 August 2013   #17
If the objects really have the exact same name, it is an easy task.


import c4d

def main():
    mapping = {}
    context = doc.GetActiveObject() or doc.GetFirstObject()

    if not context:
        return

    root = context.GetUp()

    # Go to the first object in the context.
    while context.GetPred():
        context = context.GetPred()

    # Fill the mapping.
    while context:
        mapping.setdefault(context.GetName(), []).append(context)
        context = context.GetNext()

    # Create Null-Objects.
    items = sorted(mapping.iteritems(), key=lambda x: x[0])
    prev = None
    for name, objects in items:
        null = c4d.BaseObject(c4d.Onull)
        null.SetName(name)
        for obj in objects:
            doc.AddUndo(c4d.UNDOTYPE_CHANGE, obj)
            obj.Remove()
            obj.InsertUnderLast(null)
        doc.AddUndo(c4d.UNDOTYPE_NEW, null)
        doc.InsertObject(null, root, prev)
        prev = null

    c4d.EventAdd()

main()


http://pastebin.com/m2GXy1hd
__________________
Cinema 4D Plugin Developer
niklasrosenstein.com/ | Freebies | Plugins
 
Old 08 August 2013   #18
Thank you

You Sir are a genius and a gentleman! That works like a dream and will save me hours of work in the future.

I hope other people can benefit from this script too.

Your help is what makes this forum so great.

Best regards

alawrence
 
Old 08 August 2013   #19
Thanks. Btw, if you not want to sort the top-level of the hierarchy but another part of the hierarchy, just select a single object in that hierarchy.

->
__________________
Cinema 4D Plugin Developer
niklasrosenstein.com/ | Freebies | Plugins
 
Old 08 August 2013   #20
The scripts are cool, and faster, but there's no reason it should take hours without a script. You can use the Object Manager's filter (magnifying glass icon) to make the OM show only objects with the text you enter and then it's just a matter of doing a select-all and Alt+G to group.
 
Old 08 August 2013   #21
Originally Posted by tcastudios: See if this helps.
For use in Python ScriptsManager.


# SORT OBJECTS ALPHABETICALLY IN OM
import c4d
from c4d import gui

def main():
    obj = doc.GetFirstObject()
    if not obj: return True
    doc.StartUndo()
    olist = []
    while obj:
        olist.append([obj.GetName(),obj])
        obj = obj.GetNext()
    
    olist.sort()
    olist.reverse()
    
    for i,o in enumerate(olist):
        doc.AddUndo(c4d.UNDOTYPE_CHANGE,o[1])
        o[1].Remove()
        doc.InsertObject(o[1],None,None)

    c4d.EventAdd()
    doc.EndUndo()

if __name__=='__main__':
    main()


Cheers
Lennart


Thanks Lennart. By me it works well in R14!
Do you think it would be possible (and useful) to sort objects by name in the all hierarchies? I think it might be quite useful. "Flat tree" sorts all objects but doesn´t show any hierarchies.
Jiri
 
Old 08 August 2013   #22
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright ©2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 12:40 PM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.