Advertisement
Dieton

Mesh_Bone_Animation_To_Shape_Keys_V2.py

Jul 20th, 2024 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. # this is trsh dont use this
  2.  
  3. import bpy
  4.  
  5. def read_names_from_file(file_path):
  6.     names = []
  7.     try:
  8.         with open(file_path, 'r') as file:
  9.             for line in file:
  10.                 names.append(line.strip())
  11.     except Exception as e:
  12.         print(f"Error reading file: {e}")
  13.     return names
  14.  
  15. def apply_shape_keys_from_file():
  16.     # Get the active object
  17.     active_object = bpy.context.active_object
  18.  
  19.     # Check if an object is selected and is in Object mode
  20.     if active_object and active_object.type == 'MESH' and bpy.context.mode == 'OBJECT':
  21.         # Ensure the mesh has an armature modifier
  22.         armature_modifier = active_object.modifiers.get("Armature")
  23.         if not armature_modifier:
  24.             print("Error: Please select a mesh with an Armature modifier.")
  25.             return
  26.  
  27.         # Set the file path to the text file containing names (replace with your file path)
  28.         file_path = r"D:\52_blendshapes_Names.txt"
  29.  
  30.         # Read names from the file
  31.         names = read_names_from_file(file_path)
  32.  
  33.         # Set the frame range based on your animation
  34.         start_frame = bpy.context.scene.frame_start
  35.         end_frame = bpy.context.scene.frame_end
  36.  
  37.         # Iterate over each frame
  38.         for frame in range(start_frame, end_frame + 1):
  39.             # Set the current frame
  40.             bpy.context.scene.frame_set(frame)
  41.  
  42.             # Apply the armature modifier as a shape key
  43.             bpy.ops.object.modifier_apply_as_shapekey(keep_modifier=True, modifier="Armature")
  44.  
  45.             # Assign a name from the list to the new shape key
  46.             shape_key_name = names[frame % len(names)]
  47.             shape_keys = active_object.data.shape_keys
  48.             if shape_keys:
  49.                 key_blocks = shape_keys.key_blocks
  50.                 if key_blocks:
  51.                     # Check if there are shape keys, if not, create a new one
  52.                     if len(key_blocks) == 0:
  53.                         bpy.ops.object.shape_key_add(from_mix=False)
  54.                     # Apply name to the latest shape key
  55.                     key_blocks[-1].name = shape_key_name
  56.                 else:
  57.                     print("Error: No shape keys found.")
  58.             else:
  59.                 print("Error: No shape key data found.")
  60.  
  61.         print("Shape keys applied and named from the file.")
  62.     else:
  63.         print("Error: Please select a mesh object in Object mode.")
  64.  
  65. # Call the function to apply shape keys and assign names
  66. apply_shape_keys_from_file()
  67.  
Tags: blender
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement