<?php
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=1, location=(0, 0, 0))
collision_obj = bpy.context.object
bpy.ops.object.modifier_add(type='COLLISION')
# Create the cloth plane
bpy.ops.mesh.primitive_plane_add(size=4, location=(0, 0, 2))
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.x + 2) < 0.1 and abs(v.co.y - 2) < 0.1) or \
(abs(v.co.x - 2) < 0.1 and abs(v.co.y - 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, -7, 6), rotation=(1.1, 0, 0.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, -5, 10))
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)