<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Layer Selection Python]]></title><description><![CDATA[<p dir="auto">Hi for everyone!  I have an easy code which show the hidden layer but i don't understand how change it for do the same for:</p>
<ol>
<li>selected layers</li>
<li>all layers(not only for children).</li>
</ol>
<p dir="auto"><strong>Code</strong></p>
<p dir="auto">import c4d</p>
<p dir="auto">def main():</p>
<pre><code>doc.StartUndo()
root = doc.GetLayerObjectRoot() 

LayersList = root.GetChildren()
for layer in LayersList:
   doc.AddUndo(c4d.UNDOTYPE_CHANGE,layer)
   layer [c4d.ID_LAYER_VIEW] = 1


doc.EndUndo()
c4d.EventAdd()
</code></pre>
<p dir="auto">if <strong>name</strong>=='<strong>main</strong>':<br />
main()</p>
]]></description><link>http://developers.maxon.net/forum/topic/13386/layer-selection-python</link><generator>RSS for Node</generator><lastBuildDate>Tue, 13 Oct 2026 22:53:24 GMT</lastBuildDate><atom:link href="http://developers.maxon.net/forum/topic/13386.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 25 May 2021 10:45:02 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Layer Selection Python on Mon, 21 Jun 2021 10:24:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/cairyn">@<bdi>cairyn</bdi></a> Thank you again. I will check your patreon</p>
]]></description><link>http://developers.maxon.net/forum/post/66950</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/66950</guid><dc:creator><![CDATA[ROMAN]]></dc:creator><pubDate>Mon, 21 Jun 2021 10:24:19 GMT</pubDate></item><item><title><![CDATA[Reply to Layer Selection Python on Thu, 17 Jun 2021 21:26:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/roman">@<bdi>roman</bdi></a> Just insert the code from my last post into the overall script.</p>
<p dir="auto">Here, I did it for you this time:</p>
<pre><code>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()
</code></pre>
<p dir="auto">This script toggles the editor visibility of the currently selected layers. Execute it twice to get the original visibility state back.</p>
<p dir="auto">Note:</p>
<ol>
<li>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.</li>
<li>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 <code>BIT_ACTIVE</code> for each.</li>
</ol>
<p dir="auto">(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 <a href="https://www.patreon.com/cairyn" target="_blank" rel="noopener noreferrer nofollow ugc">https://www.patreon.com/cairyn</a> -- although Patreon is admittedly not ideal for searching; I really need to make this into a proper book.)</p>
<p dir="auto">As for deleting a layer, a <code>LayerObject</code> is a <code>BaseList2D</code> which is a <code>GeListNode</code>, so you can use the <code>Remove</code> method. Like here:</p>
<pre><code>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()
</code></pre>
<p dir="auto">The infrastructure of C4D will remove the objects from that layer and throw the layer away once there are no more references.</p>
<p dir="auto">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.</p>
]]></description><link>http://developers.maxon.net/forum/post/66941</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/66941</guid><dc:creator><![CDATA[Cairyn]]></dc:creator><pubDate>Thu, 17 Jun 2021 21:26:18 GMT</pubDate></item><item><title><![CDATA[Reply to Layer Selection Python on Thu, 17 Jun 2021 18:51:07 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/cairyn">@<bdi>cairyn</bdi></a><br />
For example, we have 20 layers and i need to change visibility just for 2 layers in this list. That's what i meant when i talked about selected layers.</p>
<p dir="auto">I tried to use same code but it showed to me first layer in list....while i want to work with active layers which i have selected.</p>
<p dir="auto">And i wanted to ask which command delete layer?</p>
<p dir="auto">Sorry for stupid questions, but SDK a little bit unobvious.</p>
]]></description><link>http://developers.maxon.net/forum/post/66940</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/66940</guid><dc:creator><![CDATA[ROMAN]]></dc:creator><pubDate>Thu, 17 Jun 2021 18:51:07 GMT</pubDate></item><item><title><![CDATA[Reply to Layer Selection Python on Thu, 17 Jun 2021 14:38:58 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/roman">@<bdi>roman</bdi></a> If you mean handle only the selected <em>layers</em> then you just need to insert a check for the active bit:</p>
<pre><code>print (layer.GetName())
if layer.GetBit(c4d.BIT_ACTIVE) :
    doc.AddUndo(c4d.UNDOTYPE_CHANGE, layer)
    layer [c4d.ID_LAYER_VIEW] = 1
</code></pre>
<p dir="auto">If you really mean <em>selected objects</em>, I'm not sure what you mean as your code doesn't refer to any objects?</p>
]]></description><link>http://developers.maxon.net/forum/post/66939</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/66939</guid><dc:creator><![CDATA[Cairyn]]></dc:creator><pubDate>Thu, 17 Jun 2021 14:38:58 GMT</pubDate></item><item><title><![CDATA[Reply to Layer Selection Python on Thu, 17 Jun 2021 12:48:38 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/cairyn">@<bdi>cairyn</bdi></a>, i don't understand just one thing how should i change a code if i want to do it with selected objects not for all.</p>
<p dir="auto">import c4d</p>
<p dir="auto">def visitLayer(layer):<br />
if layer == None: return<br />
print (layer.GetName())<br />
doc.AddUndo(c4d.UNDOTYPE_CHANGE, layer)<br />
layer [c4d.ID_LAYER_VIEW] = 1<br />
visitLayer(layer.GetDown())<br />
visitLayer(layer.GetNext())</p>
<p dir="auto">def main():<br />
doc.StartUndo()<br />
first = doc.GetLayerObjectRoot().GetDown()<br />
visitLayer (first)</p>
<pre><code>doc.EndUndo()
c4d.EventAdd()
</code></pre>
<p dir="auto">if <strong>name</strong>=='<strong>main</strong>':<br />
main()</p>
]]></description><link>http://developers.maxon.net/forum/post/66938</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/66938</guid><dc:creator><![CDATA[ROMAN]]></dc:creator><pubDate>Thu, 17 Jun 2021 12:48:38 GMT</pubDate></item><item><title><![CDATA[Reply to Layer Selection Python on Mon, 31 May 2021 13:26:23 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/cairyn">@<bdi>cairyn</bdi></a> said in <a href="https://developers.maxon.net/forum/post/66723" target="_blank" rel="noopener noreferrer nofollow ugc">Layer Selection Python</a>:</p>
<blockquote>
<p dir="auto">Also, you are not looking at "only the children". The object you get from GetLayerObjectRoot() is a GeListHead, not a LayerObject, so you need to go from root either to the first layer in the list by GetDown(), or you get the full layer list on the top level(!) by GetChildren().</p>
<p dir="auto">Each of these layers in the returned list can have children, though, since layers form a full tree structure. So if you want to visit all layers, you need to recurse into the child lists as well. E.g.:</p>
<pre><code>import c4d

def visitLayer(layer):
    if layer == None: return
    print (layer.GetName(), "Selected" if layer.GetBit(c4d.BIT_ACTIVE) else "Unselected")
    visitLayer(layer.GetDown())
    visitLayer(layer.GetNext())
    
def main():
    first = doc.GetLayerObjectRoot().GetDown()
    visitLayer(first)

if __name__=='__main__':
    main()
</code></pre>
</blockquote>
<p dir="auto">Thank you a lot Cairyn!!!</p>
]]></description><link>http://developers.maxon.net/forum/post/66774</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/66774</guid><dc:creator><![CDATA[ROMAN]]></dc:creator><pubDate>Mon, 31 May 2021 13:26:23 GMT</pubDate></item><item><title><![CDATA[Reply to Layer Selection Python on Wed, 26 May 2021 08:33:22 GMT]]></title><description><![CDATA[<p dir="auto">Hello <a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/roman">@<bdi>roman</bdi></a>,</p>
<p dir="auto">thank you for reaching out to us. And thank you <a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/cairyn">@<bdi>Cairyn</bdi></a> for providing an answer. A minor problem with the answer provided by <a class="plugin-mentions-user plugin-mentions-a" href="/forum/user/cairyn">@<bdi>Cairyn</bdi></a> is that it is recursive. We recommend walking node graphs iteratively and not recursively, because of the vastness of node graphs in Cinema 4D and danger of producing stack overflows when walking them recusively. In this case, the case of layers, it is unlikely that one will hit the recursion limit of Python (one thousand recursions by default on most systems¹) or produce a stack overflow in C++ (depends on many things).</p>
<p dir="auto">Cheers,<br />
Ferdinand</p>
<p dir="auto">¹ The recursion limit of Python is defined and set in <code>sys</code> via <code>sys.get/setrecursionlimit</code>. One can raise the limit of one thousand which is rather conservative, but if it is set to high, it will then fail to prevent what it is intended for: to prevent stack overflows and crashing the Python interpereter.</p>
]]></description><link>http://developers.maxon.net/forum/post/66728</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/66728</guid><dc:creator><![CDATA[ferdinand]]></dc:creator><pubDate>Wed, 26 May 2021 08:33:22 GMT</pubDate></item><item><title><![CDATA[Reply to Layer Selection Python on Tue, 25 May 2021 19:08:46 GMT]]></title><description><![CDATA[<p dir="auto">Also, you are not looking at "only the children". The object you get from GetLayerObjectRoot() is a GeListHead, not a LayerObject, so you need to go from root either to the first layer in the list by GetDown(), or you get the full layer list on the top level(!) by GetChildren().</p>
<p dir="auto">Each of these layers in the returned list can have children, though, since layers form a full tree structure. So if you want to visit all layers, you need to recurse into the child lists as well. E.g.:</p>
<pre><code>import c4d

def visitLayer(layer):
    if layer == None: return
    print (layer.GetName(), "Selected" if layer.GetBit(c4d.BIT_ACTIVE) else "Unselected")
    visitLayer(layer.GetDown())
    visitLayer(layer.GetNext())
    
def main():
    first = doc.GetLayerObjectRoot().GetDown()
    visitLayer(first)

if __name__=='__main__':
    main()
</code></pre>
]]></description><link>http://developers.maxon.net/forum/post/66723</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/66723</guid><dc:creator><![CDATA[Cairyn]]></dc:creator><pubDate>Tue, 25 May 2021 19:08:46 GMT</pubDate></item><item><title><![CDATA[Reply to Layer Selection Python on Tue, 25 May 2021 18:56:20 GMT]]></title><description><![CDATA[<p dir="auto">Check for the active bit by <code>GetBit(c4d.BIT_ACTIVE)</code> to test whether a layer is selected.<br />
Layers are <code>GeListNode</code>s, so you can get from one layer object to the other with the usual functions <code>GetNext()</code>, <code>GetDown()</code> etc.</p>
]]></description><link>http://developers.maxon.net/forum/post/66722</link><guid isPermaLink="true">http://developers.maxon.net/forum/post/66722</guid><dc:creator><![CDATA[Cairyn]]></dc:creator><pubDate>Tue, 25 May 2021 18:56:20 GMT</pubDate></item></channel></rss>