@roman Just insert the code from my last post into the overall script.
Here, I did it for you this time:
import c4d
def visitLayer(layer):
while layer != None:
if layer.GetBit(c4d.BIT_ACTIVE) :
doc.AddUndo(c4d.UNDOTYPE_CHANGE, layer)
layer [c4d.ID_LAYER_VIEW] = not layer [c4d.ID_LAYER_VIEW]
visitLayer(layer.GetDown())
layer = layer.GetNext()
def main():
doc.StartUndo()
first = doc.GetLayerObjectRoot().GetDown()
visitLayer(first)
doc.EndUndo()
c4d.EventAdd()
if __name__=='__main__':
main()
This script toggles the editor visibility of the currently selected layers. Execute it twice to get the original visibility state back.
Note:
- Since Ferdinand didn't like the overall recursion, I replaced the next-recursion with a while loop. The depth recursion is still there but if you manage to break the Python stack with your nested layer setup, your amount of layers is sus.
- Layers do not have a method to get a list of selected elements last time I looked, so you need to traverse the whole layer tree and check the
BIT_ACTIVE
for each.
(If you are not happy about the structure of Maxon's API documentation, I do have many beginner-oriented lessons and examples in my course https://www.patreon.com/cairyn -- although Patreon is admittedly not ideal for searching; I really need to make this into a proper book.)
As for deleting a layer, a LayerObject
is a BaseList2D
which is a GeListNode
, so you can use the Remove
method. Like here:
import c4d
def visitLayer(layer):
while layer != None:
nextLayer = layer.GetNext()
if layer.GetBit(c4d.BIT_ACTIVE) :
doc.AddUndo(c4d.UNDOTYPE_DELETE, layer)
layer.Remove()
else:
visitLayer(layer.GetDown())
layer = nextLayer
def main():
doc.StartUndo()
first = doc.GetLayerObjectRoot().GetDown()
visitLayer(first)
doc.EndUndo()
c4d.EventAdd()
if __name__=='__main__':
main()
The infrastructure of C4D will remove the objects from that layer and throw the layer away once there are no more references.
Note: Since the layer tree is a linked structure, you will need to pay extra attention to the tree walking, be it recursion or iteration, when you remove layers out of the structure. That's why the visitLayer function looks a bit different here.