Advertisement
Dieton

Shape_Key_And_Name_Matcher.py

Feb 22nd, 2024 (edited)
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | None | 0 0
  1. """
  2. Copyright (c) [2/22/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.  
  23. import bpy
  24.  
  25. def create_matching_shape_keys(obj_list):
  26.     source_object = None
  27.     target_object = None
  28.  
  29.     # Iterate through the selected objects
  30.     for obj in obj_list:
  31.         if obj.data.shape_keys:
  32.             # If the object has shape keys, assign it as the source
  33.             source_object = obj
  34.         else:
  35.             # If the object does not have shape keys, assign it as the target
  36.             target_object = obj
  37.  
  38.     # Check if both source and target objects are found
  39.     if source_object and target_object:
  40.         print("Source Object: {}".format(source_object.name))
  41.         print("Target Object: {}".format(target_object.name))
  42.  
  43.         # Check if source_object has shape keys
  44.         if source_object.data.shape_keys is None:
  45.             print("Error: The source object should have shape keys.")
  46.             return
  47.  
  48.         # Create matching shape keys in the target object
  49.         for shape_key_source in source_object.data.shape_keys.key_blocks:
  50.             shape_key_target = target_object.shape_key_add(name=shape_key_source.name, from_mix=False)
  51.  
  52.         print("Shape keys created on '{0}' matching '{1}'.".format(target_object.name, source_object.name))
  53.     else:
  54.         print("Error: One object should have shape keys, and the other should not.")
  55.  
  56. # Get the selected objects
  57. selected_objects = bpy.context.selected_objects
  58.  
  59. # Ensure two objects are selected
  60. if len(selected_objects) == 2:
  61.     # Call the function to create matching shape keys
  62.     create_matching_shape_keys(selected_objects)
  63. else:
  64.     print("Error: Please select exactly two objects.")
  65.  
Tags: blender
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement