jeudi 30 septembre 2021

How to randomize material for multiple objects in blender in python?

I'm trying to randomize the material of multiple objects in blender, but all I found is this code

import bpy

prefix = '_'

materials = []
for mat in bpy.data.materials:
     if mat.name.startswith(prefix):
         materials.append(mat)

for obj in bpy.context.selected_objects:
     obj.active_material = random.choice(materials)

but this changes it for only the selected object. what I want is to type the name of the objects I want to change material to it instead of only the selected ones so for example:

change the "cube" in the scene material to Only materials that start with the prefix "x" at the same time change the materials of the "circle" with Only the materials that start with the prefix "Y" so now I have multiple objects change material at the same time but each object has its own materials to change from. Is there any way I can do it?




Aucun commentaire:

Enregistrer un commentaire