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
    • Recent
    • Tags
    • Users
    • Login

    Layer Manager Folding [SOLVED]

    Scheduled Pinned Locked Moved PYTHON Development
    9 Posts 0 Posters 858 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 17/05/2016 at 20:54, xxxxxxxx wrote:

      Hello Forum,

      Is it possible to fold/unfold layers in the Layer Manager with Python?

      Thank you,

      Joe Buck

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

        On 18/05/2016 at 02:03, xxxxxxxx wrote:

        Hello,

        a layer uses the bit BIT_OFOLD to control the folding status.

          
        if layer.GetBit(c4d.BIT_OFOLD) == False:  
          layer.SetBit(c4d.BIT_OFOLD)  
          c4d.EventAdd()  
        

        Best wishes,
        Sebastian

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

          On 18/05/2016 at 09:26, xxxxxxxx wrote:

          Hi Sebastian,

          BaseList2D.GetBit() is not working as I expected in the Layer Manager or the Object Manager.  No matter if the layer/object is folded, GetBit(c4d.BIT_OFOLD) always returns True.

          However GeListNode.GetNBit(c4d.NBIT_OM1_FOLD) and GeListNode.ChangeNBit(c4d.NBIT_OM1_FOLD, c4d.NBITCONTROL_TOGGLE) work as expected in the Object Manager.

          Do I need to somehow get the layer as a BaseList2D?

          Thank you,

          Joe Buck

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

            On 19/05/2016 at 01:40, xxxxxxxx wrote:

            Hello,

            what Cinema 4D version do you use? The above code works fine with R17 SP2. How do you aquire the layer object?

            Best wishes,
            Sebastian

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

              On 19/05/2016 at 09:13, xxxxxxxx wrote:

              Hi Sebastian,

              I'm using R17.032(Build RB139719).

              Here is the code:

              import c4d
              import c4d.documents
                
                
              def GetNextObject(obj, **kwargs) :
                  if obj is None:
                      return None
                  # using stop_obj will only check a branch of the hierarchy.
                  # It will not check siblings below/above stop_obj
                  # if stop_obj is None, all objects below the starting
                  # object will be checked.  Objects
                  # above start object will not be checked.
                  if "stop_obj" in kwargs:
                      stop_obj = kwargs['stop_obj']
                  else:
                      stop_obj = None
                  if obj.GetDown() :
                      return obj.GetDown()
                  while not obj.GetNext() and obj.GetUp() :
                      obj = obj.GetUp()
                      # quit searching if recursed up to stop_obj.
                      if obj == stop_obj:
                          return None
                  # do not get sibling below if stop_obj.
                  if obj == stop_obj:
                          return None
                  obj = obj.GetNext()
                  return obj
                
                
              def get_layer_by_name(doc, layer_name) :
                  root = doc.GetLayerObjectRoot()
                  layer = root.GetDown()
                  while layer is not None:
                      if layer.GetName() == layer_name:
                          return layer
                      layer = GetNextObject(layer)
                  return None
                
                
              def create_insert_layer(doc, layer_name, parent_layer=None) :
                  layer = c4d.documents.LayerObject()
                  if not layer:
                      return None
                  layer.SetName(layer_name)
                  if parent_layer is not None:
                      layer.InsertUnder(parent_layer)
                  else:
                      root = doc.GetLayerObjectRoot()
                      layer.InsertUnder(root)
                  return layer
                
                
              def main() :
                  doc = c4d.documents.GetActiveDocument()
                  parent_layer = get_layer_by_name(doc, 'parent_layer')
                  if not parent_layer:
                      return
                  doc.StartUndo()
                  new_layer = create_insert_layer(doc, 'new_layer', parent_layer)
                  if new_layer:
                      doc.AddUndo(c4d.UNDOTYPE_NEW, new_layer)
                  if parent_layer.GetBit(c4d.BIT_OFOLD) == True:
                      parent_layer.ToggleBit(c4d.BIT_OFOLD)
                  doc.EndUndo()
                  c4d.EventAdd()
                
                
              if __name__=='__main__':
                  main()
              

              This issue is not a big deal for my script.  If it is taking up too much of your time, we can just let it go.
               
              Again, thanks for your time and help.

              Joe Buck

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

                On 20/05/2016 at 00:48, xxxxxxxx wrote:

                Hello,

                your script works perfectly fine if you use the code I posted above.

                Best wishes,
                Sebastian

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

                  On 20/05/2016 at 06:54, xxxxxxxx wrote:

                  Sebastian,

                  Yes your code works fine.  My bad.

                  if layer.GetBit(c4d.BIT_OFOLD) == True:
                  	# layer is unfolded
                    
                  if layer.GetBit(c4d.BIT_OFOLD) == False:
                  	# layer is folded
                  

                  I thought if the bit was set, the layer would be in a folded state.  I did not test code with the layer unfolded.

                  Thank you,

                  Joe Buck

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

                    On 20/05/2016 at 09:22, xxxxxxxx wrote:

                    Here's an example that hopefully makes it clearer.
                    Make two layers named "L1" & "L2"
                    Drag L2 as a child of L1 in the Layer Manager. Then run this script.
                    It will make L1 fold & unfold each time the script is run.

                    import c4d  
                    def main() :  
                            
                      root = doc.GetLayerObjectRoot()  #Gets the layer manager  
                      
                      LayersList = root.GetChildren()  
                      for layer in LayersList:  
                          if layer.GetName() == "L1":  
                              if layer.GetBit(c4d.BIT_OFOLD) == False:  
                                  print "Currently Folded"  
                                  layer.SetBit(c4d.BIT_OFOLD)     #Unfolds just the direct children of L1  
                                  #c4d.CallCommand(100004740)     #Unfold All Children if desired  
                                    
                              elif layer.GetBit(c4d.BIT_OFOLD) == True:  
                                  print "Currently Unfolded"  
                                  layer.ToggleBit(c4d.BIT_OFOLD)  #Folds just the direct children of L1  
                                  #c4d.CallCommand(100004741)     #Fold All Children if desired  
                            
                      c4d.EventAdd()  
                      
                    if __name__=='__main__':  
                      main()
                    

                    -ScottA

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

                      On 23/05/2016 at 12:48, xxxxxxxx wrote:

                      Thanks Scott!  Your examples in the forum are always very helpful to me.  I have read and used many.

                      Joe Buck

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