Skip to content

Instantly share code, notes, and snippets.

@NiklasRosenstein
Last active December 25, 2015 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NiklasRosenstein/6913874 to your computer and use it in GitHub Desktop.
Save NiklasRosenstein/6913874 to your computer and use it in GitHub Desktop.
@rosensteinn #c4dpytask #medium Sort the selected objects in the #cinema4d object manager? #python #script @RagingClaw @PGoski Hah! For instance :) Let's choose the name of the object, case insensitive
# @rosensteinn #c4dpytask #medium Sort the selected objects in the
# #cinema4d object manager? #python #script
#
# @RagingClaw @Pgoski Hah! For instance :) Let's choose the name of
# the object, case insensitive
import c4d
sort_key = lambda x: x.GetName().lower()
def sort_and_insert(objects, key):
if not objects:
return
parent = objects[0].GetUp()
prev = objects[0].GetPred()
for obj in sorted(objects, key=key):
doc.AddUndo(c4d.UNDOTYPE_CHANGE, obj)
obj.Remove()
doc.InsertObject(obj, parent, prev)
prev = obj
def process(obj, key):
children = obj.GetChildren()
sort_children = filter(lambda x: x.GetBit(c4d.BIT_ACTIVE), children)
sort_and_insert(sort_children, key)
for child in children:
process(child, key)
def main():
objects = doc.GetObjects()
sort_objects = filter(lambda x: x.GetBit(c4d.BIT_ACTIVE), objects)
sort_and_insert(sort_objects, sort_key)
for op in objects:
process(op, sort_key)
c4d.EventAdd()
doc.StartUndo()
main()
doc.EndUndo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment