how to pin a vertex group in Cloth modifier with python API?
Asked
Active
Viewed 128 times
1 Answers
2
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:
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:
Unfortunately, it isn't always clear how vertex indices are determined. I would consult this question for further information.
ComputerScientist
- 526
- 7
- 21


gripped_groupabove, thengripped_group.namewill 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. – batFINGER Jun 03 '20 at 18:30