0
$\begingroup$

blender version==2.9.
I want to get vertex coordinates result of cloth simulation(with python script, if I could). Now, although I could get the vertex coordinates, they are before cloth simulation.

v_world = v.co @ obj.matrix_world
bpy.ops.object.empty_add(
                 type='ARROWS',
                 location=v_world)

cHPPU.png cHPPU.png

can anyone help me? Thanks in advance!!

$\endgroup$
1
$\begingroup$

You can use the dependendy graph to do that (supposing the simulation is already baked):

import bpy
import bmesh


# Set the frame considered as the result of the sim
bpy.context.scene.frame_current = 30

# Get the dependency graph state for this frame
dg = bpy.context.evaluated_depsgraph_get()

# The object
obj = bpy.context.object

# The mesh from the object and dependency graph
bm = bmesh.new()
bm.from_object(obj, dg)

# Take the coordinates
for v in bm.verts:
    print(v.co)

del bm
$\endgroup$
1
  • $\begingroup$ Thank you so much !! $\endgroup$ – ktro2828 Oct 3 '20 at 9:30
0
$\begingroup$

I could get an shape result of cloth simulation by following script,

(already cloth modifier is set)
bpy.ops.ptcache.bake_all(bake=True)
bpy.ops.object.modifier_apply_as_shapekey(keep_modifier=False, modifier='Cloth')
bpy.context.object.use_shape_key_edit_mode = True
bpy.data.shape_keys['Key'].key_blocks['Cloth'].value = 1

But, still I got same coordinates before result of cloth simulation. I can edit vertices of resulting cloth simulation, so, I think I could get vertex result of this as same...

$\endgroup$
0
$\begingroup$

Thank you @lemon , this is the best way I think.

I found another way to get vertex resulting cloth simulation, so share this.

Before running code, I've added 'Plane' and set vertex groups named 'Group' and 'Target' ('Group': to pin cloth, 'Target': target vertex)

def add_modifier(obj):
    """Add cloth modifier and apply as shapekey
    Args:
       obj(bpy.data.objects): object for cloth simulation
                              (In this case, 'Plane')
    
    Returns:
      shapekey
    """
    for ob in bpy.context.scene.objects:
        ob.select_set(False)
    obj.select_set(True)
    
    # add modifier and set some properties
    bpy.ops.object.modifier_add(type='CLOTH')
    modifier = obj.modifiers['Cloth']
    modifier.collision_settings.use_collision = True
    modifier.settings.bending_model = 'LINEAR'
    modifier.settings.vertex_group_mass = 'Group'
    
    # bake cloth simulation to get last frame
    bpy.ops.ptcache.bake_all(bake=True)
    bpy.context.scene.frame_set(250)

    # apply cloth modifier as shape-key
    #  and set value as '1'(last frame)
    bpy.ops.object.modifier_apply_as_shapekey(keep_modifier=False, modifier='Cloth')
    bpy.context.object.use_shape_key_edit_mode = True
    shapekey = bpy.data.shape_keys['Key'].key_blocks['Cloth']
    shapekey.value = 1.0
    return shapekey


# main
obj = bpy.data.objects['Plane']

# get vertices belong to vertex geoup named 'Target'
vg = obj.vertex_groups.get('Target')
vlist = []
for v in obj.data.vertices:
   for g in v.groups:
        if g.group == vg.index:
             vlist.append(v)

shapekey = add_modifier(obj)

for v in vlist:
    v_world = shapekey.data[v.index].co @ obj.matrix_world
    # visualize with ARROW
    bpy.ops.object.empty_add(type='ARROWS', location=v_world)

if someone know better ways or scripts, let me know please.

$\endgroup$

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.