What am I missing?

Haven’t really used python ever. I am working on a tiny script. I have a space dock with several thousand panels so I want to randomize the materials on each panel so it doesn’t look so uniform for all the arms of the dock. With help I have gotten too the below script…

import Blender
from Blender import Object
from Blender import Material
from Blender.Mathutils import Rand

cubes = []
materials = []

objs = Blender.Object.Get()
for obj in objs:
if ‘Cube’ in obj.name:
cubes.append(obj)

mats = Blender.Material.Get()
for mat in mats:
materials.append(mat)

for cube in cubes:
matnum = int(Rand(0.0, len(materials)))
cube.setMaterials([materials[matnum]])

Blender.Redraw()
Blender.Window.Redraw()

…which does actually work somewhat, but doesn’t update on the screen automatically. I can however go into outliner and click on the materials spheres on each of the objects listed and they will update after the script is run. I just don’t want to have to select roughly 15000 cubes in the outliner to get this to work.

What is currently missing to update the screen and show the proper materials info?

I would also like to do a bounding box check.  If bounding box values = xval, yval, zval, then modify this cube, if not disregard it.  Any good ways to proceed on this so that it properly checks the correct size?

Also what is the best way of blocking out materials, 3 of the materials I have just wouldn’t work well for this but they are in the middle of the set of materials.

Any help is appreciated.

Here is what it takes to fix it:


import Blender
from Blender import Object
from Blender import Material
from Blender.Mathutils import Rand

cubes = []
materials = []

objs = Blender.Object.Get()
for obj in objs:
   if 'Cube' in obj.name:
      cubes.append(obj)
      print obj.name
      
mats = Blender.Material.Get()
for mat in mats:
   materials.append(mat)
   print mat.name

for cube in cubes:
   matnum = int(Rand(0.0, len(mats)))
   cube.setMaterials([mats[matnum]])
   
   if cube.getType() == 'Mesh':
      mesh_obj = cube.getData()
      mesh_obj.update()

Blender.Redraw()
Blender.Window.Redraw()

Unfortunately it still has the same effect. Need to figure out what the outliner materials button calls since that allows the script to work.