How to group objects with Python?

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 17/03/2011 at 13:35, xxxxxxxx wrote:

Hi,

I am still new to C4D Python but not Python in general.

I was wondering how one would go about grouping some objects of a selection into a group, as in selecting some objects in the Object Manager and hitting Ctrl+G/Cmd+G.
It would be great if there is some sort of a command that takes a list of c4d objects and groups them.

However, I have been looking at the Python class hierarchy of the c4d module but I wasn't able to find something to do with grouping.

Thanks for reading!

Andre

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 17/03/2011 at 14:28, xxxxxxxx wrote:

def GetHNext(op) :   
  if not op: return   
  if op.GetDown() : return op.GetDown()   
  while not op.GetNext() and op.GetUp() :   
      op = op.GetUp()   
  return op.GetNext()   
  
def GetActiveObjects(doc) :   
  lst = list()   
  op = doc.GetFirstObject()   
  while op:   
      if op.GetBit(c4d.BIT_ACTIVE) == True: lst.append(op)   
      op = GetHNext(op)   
  return lst   
  
def Group(lst, copy) :   
  null = c4d.BaseObject(c4d.Onull)   
  if copy == True: lst = [i.GetClone() for i in lst]   
  [obj.InsertUnder(null) for obj in lst]   
  return null   
  
def main() :   
  lst = GetActiveObjects(doc)   
  op = Group(lst,True)   
  doc.InsertObject(op)   
  c4d.EventAdd()   

Is this what you are looking for ? :-)

PS: I'm sorry if there's any mistake in list comprehension etc. I am currently on a mobile device.

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 17/03/2011 at 14:42, xxxxxxxx wrote:

That Group function is exactly what I was looking for. Thanks a lot!

One question, if I may:

Why the

if op.GetDown() : return op.GetDown()

in

GetHNext()

?

If you want to return next then why go down and stop there?

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 17/03/2011 at 14:47, xxxxxxxx wrote:

No problem. :-)

You somewhere need to go down in the hierarchy.
And going down has the highest priority while walking the hierarchy.
Hard to explain, just test a bit with it.

Greets,
Niklas