I'm generating lot's of cubes this was (code from the solution to one of my previous question: Low level fast way to create cube). I want to add different materials with colors to the cubes, so I've expanded the code that way, but this method seems to be slow. How can I make it faster? Full code that I'm running in a loop:
name = "Cube"
size = 2
s = size / 2
vertices = [(s, s, s),
(s, s, -s),
(s, -s, s),
(s, -s, -s),
(-s, s, s),
(-s, s, -s),
(-s, -s, s),
(-s, -s, -s)]
faces = [(0, 2, 3, 1), (0, 1, 5, 4), (2, 0, 4, 6), (1, 3, 7, 5), (3, 2, 6, 7), (4, 5, 7, 6)]
mesh = bpy.data.meshes.new(name)
mesh.from_pydata(vertices, [], faces)
object = bpy.data.objects.new(name, mesh)
object.location = pos
mat = bpy.data.materials.new(name)
mat.use_nodes = True
nodes = mat.node_tree.nodes
nodes["Principled BSDF"].inputs[0].default_value = (0.8, 0.111491, 0.169871, 1)
bpy.data.objects[name].data.materials.append(mat)
bpy.context.collection.objects.link(object)