Advertisement
Dieton

Mesh Bone Rig Animation To Shape Keys.py

Mar 5th, 2024 (edited)
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. """
  2. Copyright (c) [2/25/24] [Dieton]
  3.  
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10.  
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13.  
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. SOFTWARE.
  21.  
  22. Instructions
  23. 1. Make sure to add "Shapekey_Name_List.txt" to code
  24. 2. Make sure Armature Modifier Name is Armature
  25. 3. Select object mesh in object mode
  26. 4. press play
  27. 5. done
  28. """
  29. import bpy
  30.  
  31. def read_names_from_file(file_path):
  32.     names = []
  33.     with open(file_path, 'r') as file:
  34.         for line in file:
  35.             names.append(line.strip())
  36.     return names
  37.  
  38. def apply_shape_keys_from_file():
  39.     # Get the active object
  40.     active_object = bpy.context.active_object
  41.  
  42.     # Check if an object is selected and is in Object mode
  43.     if active_object and active_object.type == 'MESH' and bpy.context.mode == 'OBJECT':
  44.         # Ensure the mesh has an armature modifier
  45.         armature_modifier = active_object.modifiers.get("Armature")
  46.         if not armature_modifier:
  47.             print("Error: Please select a mesh with an Armature modifier.")
  48.             return
  49.  
  50.         # Set the file path to the text file containing names (replace with your file path)
  51.         file_path = "Shapekey_Name_List.txt"
  52.  
  53.         # Read names from the file
  54.         names = read_names_from_file(file_path)
  55.  
  56.         # Set the frame range based on your animation
  57.         start_frame = bpy.context.scene.frame_start
  58.         end_frame = bpy.context.scene.frame_end
  59.  
  60.         # Iterate over each frame
  61.         for frame in range(start_frame, end_frame + 1):
  62.             # Set the current frame
  63.             bpy.context.scene.frame_set(frame)
  64.  
  65.             # Apply the armature modifier as a shape key
  66.             bpy.ops.object.modifier_apply_as_shapekey(keep_modifier=True, modifier="Armature")
  67.  
  68.             # Assign a name from the list to the new shape key
  69.             shape_key_name = names[frame % len(names)]
  70.             active_object.data.shape_keys.key_blocks[-1].name = shape_key_name
  71.  
  72.         print("Shape keys applied and named from the file.")
  73.     else:
  74.         print("Error: Please select a mesh object in Object mode.")
  75.  
  76. # Call the function to apply shape keys and assign names
  77. apply_shape_keys_from_file()
Tags: blender
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement