Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # this is trsh dont use this
- import bpy
- def read_names_from_file(file_path):
- names = []
- try:
- with open(file_path, 'r') as file:
- for line in file:
- names.append(line.strip())
- except Exception as e:
- print(f"Error reading file: {e}")
- return names
- def apply_shape_keys_from_file():
- # Get the active object
- active_object = bpy.context.active_object
- # Check if an object is selected and is in Object mode
- if active_object and active_object.type == 'MESH' and bpy.context.mode == 'OBJECT':
- # Ensure the mesh has an armature modifier
- armature_modifier = active_object.modifiers.get("Armature")
- if not armature_modifier:
- print("Error: Please select a mesh with an Armature modifier.")
- return
- # Set the file path to the text file containing names (replace with your file path)
- file_path = r"D:\52_blendshapes_Names.txt"
- # Read names from the file
- names = read_names_from_file(file_path)
- # Set the frame range based on your animation
- start_frame = bpy.context.scene.frame_start
- end_frame = bpy.context.scene.frame_end
- # Iterate over each frame
- for frame in range(start_frame, end_frame + 1):
- # Set the current frame
- bpy.context.scene.frame_set(frame)
- # Apply the armature modifier as a shape key
- bpy.ops.object.modifier_apply_as_shapekey(keep_modifier=True, modifier="Armature")
- # Assign a name from the list to the new shape key
- shape_key_name = names[frame % len(names)]
- shape_keys = active_object.data.shape_keys
- if shape_keys:
- key_blocks = shape_keys.key_blocks
- if key_blocks:
- # Check if there are shape keys, if not, create a new one
- if len(key_blocks) == 0:
- bpy.ops.object.shape_key_add(from_mix=False)
- # Apply name to the latest shape key
- key_blocks[-1].name = shape_key_name
- else:
- print("Error: No shape keys found.")
- else:
- print("Error: No shape key data found.")
- print("Shape keys applied and named from the file.")
- else:
- print("Error: Please select a mesh object in Object mode.")
- # Call the function to apply shape keys and assign names
- apply_shape_keys_from_file()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement