Stray_Tom

Auto-name materials in Blender

Dec 28th, 2022
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. import bpy
  2. import colorsys
  3.  
  4. # Define the category names
  5. hue_categories = ["Red", "Yellow", "Green", "Cyan", "Blue", "Purple"]
  6. saturation_categories = ["Pale", "Normal", "Vibrant"]
  7. value_categories = ["Dark", "Normal", "Light"] # <-- Reverse the order of the value categories
  8.  
  9. # Iterate over all selected objects
  10. for obj in bpy.context.selected_objects:
  11. # Iterate over all materials assigned to the object
  12. for mat in obj.data.materials:
  13. # Get the base color of the material
  14. base_color = mat.diffuse_color
  15.  
  16. # Convert the base color's RGB values to HSV values
  17. h, s, v = colorsys.rgb_to_hsv(base_color[0], base_color[1], base_color[2])
  18.  
  19. # Calculate the hue category
  20. hue_index = int((h+1/6)*5)
  21. hue_category = hue_categories[hue_index]
  22.  
  23. # Calculate the saturation category
  24. if s < 0.33:
  25. saturation_category = saturation_categories[0]
  26. elif s < 0.66:
  27. saturation_category = saturation_categories[1]
  28. else:
  29. saturation_category = saturation_categories[2]
  30.  
  31. # Calculate the value category
  32. if v < 0.33:
  33. value_category = value_categories[0]
  34. elif v < 0.66:
  35. value_category = value_categories[1]
  36. else:
  37. value_category = value_categories[2]
  38.  
  39. # Set the name of the material to the combination of categories
  40. mat.name = hue_category + " " + saturation_category + " " + value_category
  41.  
Advertisement
Add Comment
Please, Sign In to add comment