import bpy
import os
# Get the objects
objects = bpy.context.selected_objects
# Set the base path
base_path = "d:/export"
# Iterate over objects
for obj in objects:
# Check if object has an active material
if obj.active_material:
# Get the material
mat = obj.active_material
# Check if the material has a node tree
if mat.node_tree:
# Iterate over the nodes in the node tree
for node in mat.node_tree.nodes:
# Check if the node is a texture node and if its type is "TEX_IMAGE"
if node.type == 'TEX_IMAGE':
# Get the image
img = node.image
# Check if the image is not None
if img:
# Get the image size
width = img.size[0]
height = img.size[1]
print(f"original : {width}x{height}")
# Check if the image size is larger than 1024x1024
if width > 1024 or height > 1024:
print("shrinking")
# Determine the new size, keeping aspect ratio
aspect_ratio = width / height
if aspect_ratio > 1:
new_width = 1024
new_height = int(1024 / aspect_ratio)
else:
new_width = int(1024 * aspect_ratio)
new_height = 1024
# Resize the image
img.scale(new_width, new_height)
# Set the new path
new_path = os.path.join(base_path, img.name)
# Save the resized image
img.save_render(new_path)
# Reload the image and assign it to the node
img = bpy.data.images.load(new_path)
node.image = img
print("saving ",new_path)
Comments