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 Tools Display Modes
  08 August 2013
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


Last edited by alawrence : 08 August 2013 at 09:34 AM.
  08 August 2013
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:

    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)
        for obj in objects:
            doc.AddUndo(c4d.UNDOTYPE_CHANGE, obj)
        doc.AddUndo(c4d.UNDOTYPE_NEW, null)
        doc.InsertObject(null, root, prev)
        prev = null


Cinema 4D Plugin Developer | Freebies | Plugins
  08 August 2013
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

  08 August 2013
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 | Freebies | Plugins
  08 August 2013
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.
  08 August 2013
Originally Posted by tcastudios: See if this helps.
For use in Python ScriptsManager.

import c4d
from c4d import gui

def main():
    obj = doc.GetFirstObject()
    if not obj: return True
    olist = []
    while obj:
        obj = obj.GetNext()
    for i,o in enumerate(olist):


if __name__=='__main__':


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.
  08 August 2013
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.
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
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
Society of Digital Artists

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

All times are GMT. The time now is 02:04 PM.

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