Skip to content

Instantly share code, notes, and snippets.

@asus4
Created July 13, 2024 21:29
Show Gist options
  • Save asus4/79712d79712dfd7d147dd12f0511d3cc to your computer and use it in GitHub Desktop.
Save asus4/79712d79712dfd7d147dd12f0511d3cc to your computer and use it in GitHub Desktop.
A Blender script that converts all shape keys in a Curve object to a mesh with shape keys
'''
A Blender script that
1. Convert all shape keys in a curve object to mesh
2. Join all shape keys to a new mesh object
'''
import bpy
def resetAllShapeKeys(target):
for key in target.data.shape_keys.key_blocks:
key.value = 0
def convertToMeshWithShapeKeys(sourceObj):
# Assertions
assert(sourceObj is not None)
assert(sourceObj.type == 'CURVE')
assert(sourceObj.data.shape_keys is not None)
# Convert all shape keys to mesh
baseObject = None
for key in sourceObj.data.shape_keys.key_blocks:
print(f"Converting {key.name}")
# Set this shape key to 1
resetAllShapeKeys(sourceObj)
key.value = 1
# Note: Need to select the object before converting
bpy.ops.object.select_all(action='DESELECT')
sourceObj.select_set(True)
bpy.context.view_layer.objects.active = sourceObj
bpy.ops.object.convert(target='MESH', keep_original=True, merge_customdata=True)
# Get the converted mesh
newMesh = bpy.context.active_object
newMesh.select_set(False)
newMesh.name = key.name
if baseObject is None:
baseObject = newMesh
else:
# Join the mesh to the base mesh
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = baseObject
baseObject.select_set(True)
newMesh.select_set(True)
bpy.ops.object.join_shapes()
bpy.data.objects.remove(newMesh)
baseObject.name = f"{sourceObj.name}_mesh"
resetAllShapeKeys(sourceObj)
sourceObj.select_set(True)
bpy.context.view_layer.objects.active = sourceObj
convertToMeshWithShapeKeys(bpy.context.active_object)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment