4
$\begingroup$

I am very new to blender. I have a scene like this,

enter image description here

The top plane is a cloth, the two cylinders and rectangle are collision objects. I click play on the timeline and get this after a few seconds,

enter image description here

I have this code that generates a point cloud for each center of a face on a mesh. This is the code.

import bpy

def point_cloud(ob_name, coords):
    """Create point cloud object based on given coordinates and name."""
    me = bpy.data.meshes.new(ob_name + "Mesh")
    ob = bpy.data.objects.new(ob_name, me)
    me.from_pydata(coords, [], [])
    ob.show_name = True
    me.update()
    return ob

def face_centers(obj):
    """Returns median center coordinates for each face of given mesh object."""
    if obj.type == 'MESH':
        import bmesh
        bm = bmesh.new()
        bm.from_mesh(obj.data)
        return [obj.matrix_world @ f.calc_center_median() for f in bm.faces]
    else:
        return [(0.0, 0.0, 0.0)]


ob = bpy.context.active_object
pc = point_cloud(ob.name + "-pointcloud", face_centers(ob))

# Link object to the active collection
bpy.context.collection.objects.link(pc)

My problem is when I run it, the mesh locations are not updated after the simulation.

enter image description here

You can see the locations of the point cloud is in the original location of the cloth and not in the new updated location after the collisions. How can I get the new updated mesh face coordinates after the simulation has played? The code works as intended its just the mesh face locations are not correct and "outdated".

$\endgroup$

1 Answer 1

3
$\begingroup$

just try this:

if obj.type == 'MESH':
    import bmesh
    bm = bmesh.new()
    d = bpy.context.evaluated_depsgraph_get()
    bm.from_object(obj, d)

instead of:

if obj.type == 'MESH':
    import bmesh
    bm = bmesh.new()
    bm.from_mesh(obj.data)

good luck!

$\endgroup$

You must log in to answer this question.

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