3
$\begingroup$

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

$\endgroup$

2 Answers 2

4
$\begingroup$

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 ) 

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

bm.to_mesh( bpy.context.object.data )
$\endgroup$
0
$\begingroup$
import bpy

bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.mesh.select_random(percent=50, seed=2)
bpy.ops.mesh.delete(type='VERT')
bpy.ops.object.mode_set(mode = 'OBJECT')
$\endgroup$

You must log in to answer this question.

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