Child Layers in layer manager?

On 09/04/2013 at 09:52, xxxxxxxx wrote:

Hi,

Could someone help me get to the child layers in the layers manager? I'm getting stuck with why the hierarchy is not the same or I'm just being silly? If any one could help that would be great.

Heres a pic:

I'd be happy to just print all the layers including the children at this stage :/
Thanks guys.

On 09/04/2013 at 10:00, xxxxxxxx wrote:

a layerobject is derrived form a gelistnode, so you use just the standard methods to navigate
within a gelistnode tree (GetChildren, Next() and so on). use basedocument.getlayerobjectroot()
to get the root ;)

On 26/04/2013 at 15:37, xxxxxxxx wrote:

This ought to get you going...

  
import c4d   
  
def GetNextObject(op) :   
    """   
    Returns the next object in the hierarchy.   
    """   
    if op==None: return None   
  
    if op.GetDown() : return op.GetDown()   
  
    while not op.GetNext() and op.GetUp() :   
        op = op.GetUp()   
  
    return op.GetNext()   
  
def GetFirstLayer(doc) :   
    """   
    Returns the first layer, if   
    available, otherwise None   
    """   
  
    return doc.GetLayerObjectRoot().GetDown()   
  
def GetFirstActiveLayer(doc) :   
    """   
    Returns the first selected layer,   
    otherwise None   
    """   
       
    #Get the first_layer   
    first_layer = GetFirstLayer(doc)   
       
    #If there aren't any layers, return None   
    if first_layer is None: return None   
  
    #Loop through all layers until you find one that's active   
    cur_layer = first_layer   
       
    while cur_layer:   
        #Return the first active layer   
        if cur_layer.GetBit(c4d.BIT_ACTIVE) :   
            return cur_layer   
        cur_layer = GetNextObject(cur_layer)   
       
    #No active layers, return None   
    return None   
  
def GetLayers(doc) :   
    """   
    Returns a list containing all layers in the current document   
    """   
       
    #Get the first_layer   
    first_layer = GetFirstLayer(doc)   
       
    #If there aren't any layers, return None   
    if first_layer is None: return None   
       
    #Loop through all layers   
    layers = []   
    cur_layer = first_layer   
       
    while cur_layer:   
        layers.append(cur_layer)   
        cur_layer = GetNextObject(cur_layer)   
       
    return layers   
  
def PrintLayers(doc) :   
    """   
    Prints a list of all layers to the console.   
    """   
       
    for layer in GetLayers(doc) :   
        print layer.GetName()   
  
def main() :   
    PrintLayers(doc)   
  
if __name__=='__main__':   
    main()   

On 27/04/2013 at 03:41, xxxxxxxx wrote:

Or apply recursion:

def iter_hierarchy(obj, callback, depth=0) :
    if callback:
        callback(obj, depth)
  
    for child in obj.GetChildren() :
        iter_hierarchy(child, callback, depth + 1)
  
def print_obj(obj, depth) :
    print "    " * depth + obj.GetName()
 
def main() :
    layer = doc.GetLayerObjectRoot().GetDown()
    while layer:
        iter_hierarchy(layer, print_obj)
        layer = layer.GetNext()
  
main()

Untested, but should at least show you how it works.

Best,
Niklas

Sent from my mobile Phone.

Edit: Corrected one line in the code, works fine now.

On 29/04/2013 at 03:31, xxxxxxxx wrote:

Thanks for your replies.

I actually went down the recursion route like this:

import c4d
from c4d import gui
  
def getNextLayer(layer) :
    if layer.GetDown() :
         return layer.GetDown()
    while layer.GetUp() and not layer.GetNext() :
         layer = layer.GetUp()
    return layer.GetNext()
  
def main() :
    doc = 	c4d.documents.GetActiveDocument()
    root = 	doc.GetLayerObjectRoot()
    layer = root.GetDown()
  
    # loop through every layer in the layer list
    while layer:
        #Do something            
        layer = getNextLayer(layer)
    c4d.EventAdd()
  
if __name__=='__main__':
    main()

Thanks again guys.

On 29/04/2013 at 04:18, xxxxxxxx wrote:

Nope, that's iteration.

Best,
-Niklas

On 29/04/2013 at 08:22, xxxxxxxx wrote:

Indeed, my mistake