Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!BPY
  2. """
  3. Name: 'Material by Element'
  4. Blender: 261
  5. Group: 'Material'
  6. Tooltip: 'Material by Element'
  7. """
  8.  
  9. import bpy
  10. import random
  11.  
  12.  
  13. print("MaterialByElement:")     #to test output...
  14.  
  15. scena = bpy.context.scene       #active scene
  16. contesto = bpy.context          #context
  17.  
  18. N=10    #n. of material you want to be created
  19. K=200   #KAOS - N*5 gives more contrast - N*10 is regular
  20.  
  21. #put here RGB values of main color:
  22. r=0.4    #R
  23. g=0.08   #G
  24. b=0.04   #B
  25.  
  26. i = 0                           #reset counter
  27.  
  28. mats=bpy.data.materials
  29.  
  30. #unlink all materials and deletes them
  31. for matter in mats:
  32.     matter.user_clear()                 #unlink
  33.     bpy.data.materials.remove(matter)   #remove "matter" from materials slot
  34.     i+=1
  35.  
  36. i=0                             #reset counter
  37.  
  38. #creates new material set
  39. for i in range(0,N):
  40.     k=bpy.data.materials.new("MatByEl_"+str(i))
  41.     #this is to have more difference between objects
  42.     if i%2==0:
  43.         k.diffuse_color=(r+(i/K), g+(i/K), b+(i/K))
  44.     else:
  45.         k.diffuse_color=(r-(i/K), g-(i/K), b-(i/K))
  46.    
  47. matter=bpy.data.materials
  48.  
  49. for obj in contesto.selected_objects:       #for every object in selection
  50.     scena.objects.active = obj              #makes it active
  51.     for mat in obj.material_slots:
  52.         bpy.ops.object.material_slot_remove()   #remove all materials
  53.     bpy.ops.object.material_slot_add()                  #add ONE material
  54.     obj.data.materials[0]=matter[random.randrange(0,N-1)] #assign one of the N materials