Post #2533: ![](https://blenderartists.org/user_avatar/blenderartists.org/raphaelbarros/48/1

๐Ÿ“‹ Metadata

๐Ÿท๏ธ Tags

python-scripting intermediate solved

  • Python Scripting
  • Macro Editor

๐Ÿ’ฌ Content

RaphaelBarros:

Iโ€™m having some trouble converting some code to a single line.
How can I convert โ€œforsโ€ and โ€œifsโ€ to this single line codes?

In some cases itโ€™s impossible. But in your case you can use list comprehensions:

for ob in scene.objects:
    if ob.type == 'MESH':
        scene.objects.active = ob;
        break;



mehses = [ob for ob in scene.objects if ob.type == 'MESH']; scene.objects.active = meshes[0] if meshes else scene.objects.active



for ob in scene.objects:
    if ob.type == 'EMPTY':
        scene.objects.unlink(ob)
        bpy.data.objects.remove(ob) 



[(scene.objects.unlink(ob), bpy.data.objects.remove(ob)) for ob in scene.objects if ob.type == 'EMPTY']

โค๏ธ 1 likes


๐Ÿ”— View on Blender Artists