#!BPY
"""
Name: 'Material by Element'
Blender: 261
Group: 'Material'
Tooltip: 'Material by Element'
"""
import bpy
import random
print("MaterialByElement:") #to test output...
scena = bpy.context.scene #active scene
contesto = bpy.context #context
N=10 #n. of material you want to be created
K=200 #KAOS - N*5 gives more contrast - N*10 is regular
#put here RGB values of main color:
r=0.4 #R
g=0.08 #G
b=0.04 #B
i = 0 #reset counter
mats=bpy.data.materials
#unlink all materials and deletes them
for matter in mats:
matter.user_clear() #unlink
bpy.data.materials.remove(matter) #remove "matter" from materials slot
i+=1
i=0 #reset counter
#creates new material set
for i in range(0,N):
k=bpy.data.materials.new("MatByEl_"+str(i))
#this is to have more difference between objects
if i%2==0:
k.diffuse_color=(r+(i/K), g+(i/K), b+(i/K))
else:
k.diffuse_color=(r-(i/K), g-(i/K), b-(i/K))
matter=bpy.data.materials
for obj in contesto.selected_objects: #for every object in selection
scena.objects.active = obj #makes it active
for mat in obj.material_slots:
bpy.ops.object.material_slot_remove() #remove all materials
bpy.ops.object.material_slot_add() #add ONE material
obj.data.materials[0]=matter[random.randrange(0,N-1)] #assign one of the N materials