Results 1 to 3 of 3

Thread: For loop help

  1. #1

    For loop help

    What am I doing wrong lol, I make a list of selected objects and want to .remove them from the list if they are not a mesh or a curve, but if I make 3 suns and run the script it doesn't remove them all :S

    Code:
    import bpy 
    
    
    selected_objects = bpy.context.selected_objects
    
    
    print("-----------------------------")
    print("List len before: " + str(len(selected_objects)))
    
    
    for obj in selected_objects:
          
        if obj.type not in ['MESH','CURVE']:
            selected_objects.remove(obj)
    
    
    print("List len after: " + str(len(selected_objects)))
    Last edited by Way2Close; 14-Feb-18 at 08:38.



  2. #2
    Member Secrop's Avatar
    Join Date
    Dec 2011
    Location
    Berlin
    Posts
    2,009
    If you remove the items from the beginning of a list, the index of the next item will be now the index of the removed item... The for loop just keeps the track of the index it's iterating, and it will jump to the index+1, thought the next item is now at index0.

    you can try do change it to this instead:
    Code:
    my_filtered_objects=[obj for obj in selected_objects if obj.type in ['MESH', 'CURVE']]



  3. #3
    Thank you very much, I was just trying to be optimized and use one variable. Guess I need to read up on arrays haha



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •