Quantcast
Channel: Scripting - McNeel Forum
Viewing all articles
Browse latest Browse all 5938

Deleting layers not working if objects moved to another layer before

$
0
0

Hi all,

here is a script to flatten all child layers so that no more nested layers are present.
Everything works well except that scriptcontext.doc.ActiveDoc.Layers.Delete(lay.Id,True) does nothing if a object is moved to another layer before.

I guess i miss something simple maybe commit changes to the layer.Table…dont know.
I descriped the problem in big letters on the line wehre nothing happens.

import scriptcontext as sc

act_doc = sc.doc.ActiveDoc

# iterate over layers
for lay in act_doc.Layers:
    # get all child layers
    if len(str(lay).split('::')) > 1:
        # check if objects on this layer
        if len(act_doc.Objects.FindByLayer(lay)) > 0:
            # move objects to the first parent layer
            for obj in act_doc.Objects.FindByLayer(lay):
                lay_first = act_doc.Layers.FindName(str(lay).split('::')[0])
                obj.Attributes.LayerIndex = lay_first.LayerIndex
                obj.CommitChanges()
            # delete the child layer
            # HERE IS THE PROBLEM THE LAYER WILL NOT BE DELETED
            # ONLY IF THE SCRIPT RUN A SECOND TIME
            # BUT THAN ITS THE ELSE STATEMENT
            act_doc.Layers.Delete(lay.Id,True)
            
        else:
            act_doc.Layers.Delete(lay.Id,True)

Maybe someone had the same issue and a solution for it.
Thanks in advanced

2 posts - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 5938

Trending Articles