0
$\begingroup$

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

$\endgroup$
2
$\begingroup$

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:
            bpy.data.meshes.remove(block)
    bpy.ops.object.mode_set(mode='OBJECT')
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete()

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))
    bpy.ops.object.editmode_toggle()
    bpy.ops.mesh.subdivide(number_cuts=20)
    bpy.ops.object.editmode_toggle()
    bpy.ops.object.modifier_add(type='CLOTH')

    # 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__':
    clear_scene()
    vertices = [0]
    make_cloth(vertices)

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.

$\endgroup$
2
  • $\begingroup$ Thanks @batFINGER I added some text there to explain. $\endgroup$ – ComputerScientist 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.