Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Child Layers in layer manager?

    Scheduled Pinned Locked Moved PYTHON Development
    7 Posts 0 Posters 602 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      Helper
      last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • H Offline
        Helper
        last edited by

        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 😉

        1 Reply Last reply Reply Quote 0
        • H Offline
          Helper
          last edited by

          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()   
          
          1 Reply Last reply Reply Quote 0
          • H Offline
            Helper
            last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • H Offline
              Helper
              last edited by

              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.

              1 Reply Last reply Reply Quote 0
              • H Offline
                Helper
                last edited by

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

                Nope, that's iteration.

                Best,
                -Niklas

                1 Reply Last reply Reply Quote 0
                • H Offline
                  Helper
                  last edited by

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

                  Indeed, my mistake Tongue

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post