2
$\begingroup$

So I have no Idea what I am doing lol I have been experimenting with a little bit of code to try and select all objects in the scene if they have "x" amount of vertices. I have a scene with 158 objects in it. About 70 of those objects have 4 or less vertices but they are all mashed in with the other objects. Other than selecting each individual object one by one, I was hoping to be able to select all the objects that have 4 or less by simply clicking "Run Script" Is this code even remotely close? lol

import bpy
import bmesh

vertCount = 4

if True:
    
    meshes = set(o.data for i in context.selected_objects
        if o.type == 'MESH')

bm = bmesh.new()

for m in meshes:
        bpy.ops.object.mode_set( mode = 'EDIT' )
        bm = bmesh.from_edit_mesh( bpy.context.object.data )
        if len(m.vertices) == vertCount
            m.select = True
        bpy.ops.object.mode_set( mode = 'OBJECT' )
        bm.clear()
    
    bm.free()    
    bm.Select_Flush(True)
$\endgroup$

2 Answers 2

5
$\begingroup$

Select scene objects

Iterating over objects in the scene will keep the selection result is confined to only objects type mesh in the scene.

An object can be active but not selected.

Prior to blender 2.8

import bpy

context = bpy.context

for o in context.scene.objects:
    o.select = (o.type == 'MESH' and len(o.data.vertices) <= 4)

in > 2.8

o.select_set(o.type == 'MESH' and len(o.data.vertices) <= 4) 
$\endgroup$
3
  • $\begingroup$ Hey that worked perfectly! Thank you so much! $\endgroup$ Oct 5, 2020 at 11:51
  • $\begingroup$ I'm sorry to ask for more help but : I have a plane that's deformed like a heightmap. The one edge is offset by 32 BU and I have to go into Edit mode, Select the Edge and Subtract the edge by 32. It aligns perfectly where it should once I do that but I have like 50+ objects that need this done and I could use a quick solution. Is it possible to make a quick script that can just get all meshes that are greater than 30 BU in any axis direction and then reduce the edge by 32 BU per object? $\endgroup$ Oct 5, 2020 at 14:02
  • 1
    $\begingroup$ Ask a new question. $\endgroup$
    – batFINGER
    Oct 5, 2020 at 14:14
2
$\begingroup$

You can check object's data's vertices length:

len(obj.data.vertices) == count

One-line in interactive console

If you are familiar with python and don't need further manipulation for your object, use this snippet in your console.

tuple(map(lambda obj: obj.select_set(obj.type == 'MESH' and len(obj.data.vertices) <= 8), D.objects))
tuple(  # needed for map object to apply function
    map(  # apply the select based on vertices count
        lambda obj: obj.select_set(obj.type == 'MESH' and len(obj.data.vertices) <= 8),
        D.objects
    )
)

Code

import bpy

vertCount = 4
context = bpy.context

for obj in context.selected_objects:
    if obj.type != 'MESH':
        obj.select_set(False)  # deselect not mesh type
        continue
    assert obj.type == 'MESH'
    if obj.data and obj.data.vertices and len(obj.data.vertices) <= vertCount:
        pass
    else:
        obj.select_set(False)  # deselect obj

$\endgroup$
1
  • $\begingroup$ Hey thanks for the quick reply, when i run the script it pitches an error. The console reads: 'Object' object has no attribute Select_Set Not sure if it makes a difference but I am using blender 2.79 $\endgroup$ Oct 5, 2020 at 11:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .