how to pin a vertex group in Cloth modifier with python API?


Here's a quick minimum working example with Blender 2.82a and Python:

import bpy

def clear_scene():
    for block in bpy.data.meshes:
        if block.users == 0:

def make_cloth(vertices):
    """Make a plane at height 0.2m and subdivide. Then, pin vertices."""
    bpy.ops.mesh.primitive_plane_add(location=(0, 0, 0.2))

    # Pin the designated vertices.
    gripped_group = bpy.context.object.vertex_groups.new(name='Pinned')
    gripped_group.add(vertices, 1.0, 'ADD')
    bpy.context.object.modifiers["Cloth"].settings.vertex_group_mass = 'Pinned'

if __name__ == '__main__':
    vertices = [0]

Note: the clear_scene method is not necessary to get pinned vertices. I just had it there to remove the starting items so that the only thing left would be the cloth.

If this file is called test.py, then run blender -P test.py. I get this when I open the GUI:

enter image description here

Notice the vertex groups shown to the right in the screenshot above.

Clicking the "play"' button to animate the scene, I get this at frame 85:

enter image description here

Unfortunately, it isn't always clear how vertex indices are determined. I would consult this question for further information.

  • $\begingroup$ Thanks @batFINGER I added some text there to explain. $\endgroup$ Jun 3 '20 at 18:21
  • $\begingroup$ Cheers, a little warning like This will clear all your objects and meshes from the file too. lol. Oh and recommend with modifiers and most blender objects, if you have a reference eg gripped_group above, then gripped_group.name will always be that refs name. eg if for example by chance there is an existing group named "Pinned". Realise it can't happen here, .. simply think it's a good habit to get into with rt blender's naming convention as it is. A grid primitive will remove the need to mode swap subdiv. $\endgroup$
    – batFINGER
    Jun 3 '20 at 18:30

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.