Last active
January 28, 2022 21:40
-
-
Save programmarchy/17267c5ef9f146d0927951d53dd85060 to your computer and use it in GitHub Desktop.
Blender script which, given an active object, copies the material for that object assigning new PBR texture maps from a local directory structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
active_object = bpy.context.active_object | |
col_width = 10 | |
if active_object is None: | |
raise Exception("Select an object to use as a template") | |
for i in range(1, 100): | |
n = i + 1 | |
row = int(i / col_width) | |
col = int(i % col_width) | |
x = 3 * col | |
y = 3 * row | |
name = "Sphere_{0:03d}".format(n) # e.g. Sphere_001 | |
material_name = "Texture_{0:03d}".format(n) # e.g. Texture_001 | |
base_path = "//{0}/{0}".format(material_name) | |
copy_material = active_object.active_material.copy() | |
copy_material.name = material_name | |
copy_mesh = active_object.data.copy() | |
copy_mesh.name = name | |
copy_object = bpy.data.objects.new(name, copy_mesh) | |
copy_object.active_material = copy_material | |
copy_object.location.x = x | |
copy_object.location.y = y | |
basecolor_image = bpy.data.images.load(filepath = base_path + "_basecolor.png") | |
basecolor_image.colorspace_settings.name = "sRGB" | |
copy_material.node_tree.nodes["Base Color Texture"].image = basecolor_image | |
ao_image = bpy.data.images.load(filepath = base_path + "_ambientocclusion.png") | |
ao_image.colorspace_settings.name = "sRGB" | |
copy_material.node_tree.nodes["AO Texture"].image = ao_image | |
roughness_image = bpy.data.images.load(filepath = base_path + "_roughness.png") | |
roughness_image.colorspace_settings.name = "Non-Color" | |
copy_material.node_tree.nodes["Roughness Texture"].image = roughness_image | |
normal_image = bpy.data.images.load(filepath = base_path + "_normal.png") | |
normal_image.colorspace_settings.name = "Non-Color" | |
copy_material.node_tree.nodes["Normal Texture"].image = normal_image | |
height_image = bpy.data.images.load(filepath = base_path + "_height.png") | |
height_image.colorspace_settings.name = "Non-Color" | |
copy_material.node_tree.nodes["Height Texture"].image = height_image | |
bpy.context.collection.objects.link(copy_object) | |
active_object = copy_object | |
print("Placed {0} at ({1}, {2})".format(name, x, y)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment