I have more than 200 cards and each card have one face that have image on top. I assigned material to surface with image in material nodes.
I am trying to change image inside material programmatically based on object name. So that each card will automatically have respective image on face without making 100 materials.
I am trying not to do it with OSL as it does not support GPU. Not sure where to start with script. Any guidance would be really helpful.
Without it, you don’t have access to object names, and you’d need to use something else to distinguish your objects, particularly Object Info->Object Index. Then it would be just a matter of checking that and use RGBMixes to change which texture will end up in the closure.
Perhaps better is just to have 200 materials, one per object (it throws the heavy lift of assigning materials to Python instead of throwing it to the render, where each sample would need to calc everything).
here’s something simple:
# you need:
# collection = a list of objects
# name2image_map = a correspondence between names and images;
# it could be a function, a dict, list of tuples, etc
# matbase = a base material from which the rest will derive.
# common parts could be a nodegroup, for latter tweaking.
for obj in collection:
newmat = matbase.copy()
newmat.node_tree.nodes['Image Texture'].image = name2image_map(obj.name)
obj.material_slots[0].material = newmat