www.xbdev.net
xbdev - software development
Wednesday June 25, 2025
Home | Contact | Support | Blender (.py) Scripts... Automating Blender ..
     
 

Blender (.py) Scripts...

Automating Blender ..

 

Generating a Cloth (Simulation)


Cloth simulations are one of those things that add a bit of magic to a scene - anything from a flag flapping in the distance, to the cape that flaps behind Batman or Superman. With Blender scripting - you can create cloth simulations very easily.

In 20ish lines you can create a beautiful cloth simulation - that interacts with another rigid body (sphere) - we'll create a square cloth that is pinned on the two corners - and falls and covers the sphere.

import bpy

# Clear all objects
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)

# Create collision object (sphere)
bpy.ops.mesh.primitive_uv_sphere_add(radius=1location=(000))
collision_obj bpy.context.object
bpy
.ops.object.modifier_add(type='COLLISION')

# Create the cloth plane
bpy.ops.mesh.primitive_plane_add(size=4location=(002))
cloth bpy.context.object

# Subdivide the plane
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.subdivide(number_cuts=20)
bpy.ops.object.editmode_toggle()


# Create a vertex group for pinning two corners (top-left and top-right)
mesh cloth.data
pin_group 
cloth.vertex_groups.new(name="PinGroup")

for 
v in mesh.vertices:
    
# Top-left (approx: -2, 2), top-right (approx: 2, 2)
    
if (abs(v.co.2) < 0.1 and abs(v.co.2) < 0.1) or \
       (
abs(v.co.2) < 0.1 and abs(v.co.2) < 0.1):
        
pin_group.add([v.index], 1.0'ADD')


# Add cloth modifier
cloth_mod cloth.modifiers.new(name="ClothSim"type='CLOTH')
cloth_mod.settings.quality 5
cloth_mod
.settings.vertex_group_mass "PinGroup"

# Add a camera
bpy.ops.object.camera_add(location=(7, -76), rotation=(1.100.8))
camera bpy.context.object
bpy
.context.scene.camera camera

# Add a sun lamp for lighting
bpy.ops.object.light_add(type='SUN'location=(5, -510))
light bpy.context.object
light
.data.energy 3

# Set timeline range
bpy.context.scene.frame_start 1
bpy
.context.scene.frame_end 250

bpy
.context.scene.frame_set(1)


Tinker with the different parameters to get a feel for what is possible.


Things to Try


• Modify the material of the cloth (e.g., shiny or transparent)
• Make the cloth more slippery (modifying the friction)
• Add the cloth to the sphere as a 'cape' - then animate and move the sphere - so it moves around and the 'cape' flaps about and follows the sphere (Super-Sphere).
• Create a flagpoll with a flag on it
• Create a window with some curtains (also have them flapping - as the window is open)
• Create a cloth and have it twist and wrap around different objects (e.g., cubes and donuts)











 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2025 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.