Advertisement
salahzar

blender save resize pictures

Feb 10th, 2023 (edited)
1,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. import bpy
  2. import os
  3.  
  4. # Get the objects
  5. objects = bpy.context.selected_objects
  6.  
  7. # Set the base path
  8. base_path = "d:/export"
  9.  
  10. # Iterate over objects
  11. for obj in objects:
  12.     # Check if object has an active material
  13.     if obj.active_material:
  14.         # Get the material
  15.         mat = obj.active_material
  16.         # Check if the material has a node tree
  17.         if mat.node_tree:
  18.             # Iterate over the nodes in the node tree
  19.             for node in mat.node_tree.nodes:
  20.                 # Check if the node is a texture node and if its type is "TEX_IMAGE"
  21.                 if node.type == 'TEX_IMAGE':
  22.                     # Get the image
  23.                     img = node.image
  24.                     # Check if the image is not None
  25.                     if img:
  26.                         # Get the image size
  27.                         width = img.size[0]
  28.                         height = img.size[1]
  29.                         print(f"original : {width}x{height}")
  30.                         # Check if the image size is larger than 1024x1024
  31.                         if width > 1024 or height > 1024:
  32.                             print("shrinking")
  33.                             # Determine the new size, keeping aspect ratio
  34.                             aspect_ratio = width / height
  35.                             if aspect_ratio > 1:
  36.                                 new_width = 1024
  37.                                 new_height = int(1024 / aspect_ratio)
  38.                             else:
  39.                                 new_width = int(1024 * aspect_ratio)
  40.                                 new_height = 1024
  41.                             # Resize the image
  42.                             img.scale(new_width, new_height)
  43.                         # Set the new path
  44.                         new_path = os.path.join(base_path, img.name)
  45.                         # Save the resized image
  46.                         img.save_render(new_path)
  47.                         # Reload the image and assign it to the node
  48.                         img = bpy.data.images.load(new_path)
  49.                         node.image = img
  50.                         print("saving ",new_path)
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement