I have an object in my scene which is basically a point cloud, i.e. all vertices, no edges or faces. I'm trying to select a random fraction of this point cloud. Here is what the data looks like.

>>> bpy.data.objects[2].data.vertices[:]
[bpy.data.meshes['vertsMesh'].vertices[0], bpy.data.meshes['vertsMesh'].vertices[1], bpy.data.meshes['vertsMesh'].vertices[2], bpy.data.meshes['vertsMesh'].vertices[3], bpy.data.meshes['vertsMesh'].vertices[4], bpy.data.meshes['vertsMesh'].vertices[5], bpy.data.meshes['vertsMesh'].vertices[6], bpy.data.meshes['vertsMesh'].vertices[7], bpy.data.meshes['vertsMesh'].vertices[8], bpy.data.meshes['vertsMesh'].vertices[9], bpy.data.meshes['vertsMesh'].vertices[10], bpy.data.meshes['vertsMesh'].vertices[11], bpy.data.meshes['vertsMesh'].vertices[12], bpy.data.meshes['vertsMesh'].vertices[13], bpy.data.meshes['vertsMesh'].vertices[14], bpy.data.meshes['vertsMesh'].vertices[15], bpy.data.meshes['vertsMesh'].vertices[16], bpy.data.meshes['vertsMesh'].vertices[17], bpy.data.meshes['vertsMesh'].vertices[18], bpy.data.meshes['vertsMesh'].vertices[19], bpy.data.meshes['vertsMesh'].vertices[20], bpy.data.meshes['vertsMesh'].vertices[21], bpy.data.meshes['vertsMesh'].vertices[22], bpy.data.meshes['vertsMesh'].vertices[23], bpy.data.meshes['vertsMesh'].vertices[24], bpy.data.meshes['vertsMesh'].vertices[25], bpy.data.meshes['vertsMesh'].vertices[26], bpy.data.meshes['vertsMesh'].vertices[27], bpy.data.meshes['vertsMesh'].vertices[28], bpy.data.meshes['vertsMesh'].vertices[29], bpy.data.meshes['vertsMesh'].vertices[30], bpy.data.meshes['vertsMesh'].vertices[31]]

I'm thinking of a script that would resemble the following

count = int(vertices.length*fraction): # where fraction is a float generated by another function
for i in count:
    rand = random number generated % vertices.length
    delete vertices[rand]

I'm just not sure how this would be implemented in blender


enter image description here This script will remove random vertices on the active object in Object Mode.

import bpy, bmesh
from random import randint

bm = bmesh.new()
bm.from_mesh( bpy.context.object.data )

fraction = 0.5
count    = int( len( bm.verts ) * fraction )

removeIndices = []

# Add more unique indices until list is as big as $count
while len( removeIndices ) < count:
    randIdx = randint( 0, len( bm.verts ) - 1 )
    if randIdx not in removeIndices: 
        removeIndices.append( randIdx ) 

randVerts = [ bm.verts[ i ] for i in removeIndices ]
for v in randVerts:
    bm.verts.remove( v )

bm.to_mesh( bpy.context.object.data )
| improve this answer | |
import bpy

bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.mesh.select_random(percent=50, seed=2)
bpy.ops.object.mode_set(mode = 'OBJECT')
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.