-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloth_sim_on_2s.py
80 lines (60 loc) · 2.69 KB
/
cloth_sim_on_2s.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
bl_info = {
"name": "Stepped Cloth Sim Interpolation",
"author": "Joseph Hansen",
"version": (1, 1),
"blender": (3, 6, 5),
"location": "Properties > Physics > Cloth > Bake",
"description": "Convert a cloth simulation to stepped interpolation on 2s",
"category": "Physics",
}
import bpy
import os
class OBJECT_OT_interpolate_bake(bpy.types.Operator):
"""Convert cloth simulations to stepped interpolation on 2s"""
bl_idname = "object.interpolate_bake"
bl_label = "Interpolate All Bakes on 2s"
def execute(self, context):
blend_file_path = bpy.data.filepath
if not blend_file_path:
self.report({'ERROR'}, "Please save your blend file before running this script.")
return {'CANCELLED'}
directory = os.path.dirname(blend_file_path)
directory = os.path.join(directory, "blendcache_cloth")
print(directory)
if not os.path.exists(directory):
self.report({'ERROR'}, f"Directory '{directory}' not found.")
return {'CANCELLED'}
suffixes = []
for filename in os.listdir(directory):
if filename.endswith('.bphys'):
suffixes.append(filename.split('_')[2].split(".")[0])
print(suffixes)
for cache in suffixes:
for filename in os.listdir(directory):
if filename.endswith('.bphys') and filename.split('_')[2].split(".")[0] == cache:
file_number = int(filename.split('_')[1])
if file_number % 2 == 0:
print(file_number)
prev_file_number = file_number - 1
prev_filename = f"{filename.split('_')[0]}_{prev_file_number:06d}_{cache}.bphys"
with open(os.path.join(directory, prev_filename), 'rb') as prev_file:
content = prev_file.read()
with open(os.path.join(directory, filename), 'wb') as current_file:
current_file.write(content)
return {'FINISHED'}
def draw_func(self, context):
layout = self.layout
layout.operator("object.interpolate_bake", text="Interpolate All Bakes On 2s")
classes = (
OBJECT_OT_interpolate_bake,
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.PHYSICS_PT_cloth_cache.append(draw_func)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
bpy.types.PHYSICS_PT_cloth_cache.remove(draw_func)
if __name__ == "__main__":
register()