script

import bpy
import random

# Clear all nodes in a mat
def clear_material( material ):
   if material.node_tree:
       material.node_tree.links.clear()
       material.node_tree.nodes.clear()
       

def newMaterial(id, r , g ,b):
   #mat = bpy.data.materials.get(id)

   mat = bpy.data.materials.new(name=id)

   mat.diffuse_color = (r, g, b, 1)
   
   print(id)
   print(mat.name)
   print('----------------------')
   return mat

def offsetVertex(vertices):
   vNum = len(vertices)
   random.randint(0, vNum)
   
   for i in range(0, int(vNum/3)):
       vertices[random.randint(0, vNum-1)].co.z += random.uniform(-1.5, 1.5)
   
   

def createUnique(subdivLevel, x, y, z, r, g, b, id):
   bpy.ops.mesh.primitive_plane_add(size=6, enter_editmode=False, align='WORLD', location=(x, y, z), scale=(1, 1, 1))
   
   bpy.ops.object.modifier_add(type='SUBSURF')
   bpy.context.object.modifiers["Subdivision"].render_levels = 2
   bpy.context.object.modifiers["Subdivision"].levels = subdivLevel
   bpy.ops.object.modifier_apply(modifier="Subdivision")
   
   
   obj = bpy.context.object  # Select the active object.
   offsetVertex(obj.data.vertices)


   mat = newMaterial(id,r,g,b)
   # because there is already a material with name NAME from the op this will have name NAME.00x 
   print(mat.name)
   obj.data.materials.clear()
   obj.data.materials.append(mat)  # Assign the new material.

   return

def RandomScene():
   count = 1000
   
   cubeLen = 300
   startNum = 0
   for i in range(startNum, startNum + count):
       createUnique(2, random.randint(-cubeLen, cubeLen) , random.randint(-cubeLen, cubeLen) , random.randint(-cubeLen, cubeLen) ,random.uniform(0.0, 1.0), random.uniform(0.0, 1.0),random.uniform(0.0, 1.0),"mat" + str(i))



RandomScene()

#bpy.ops.mesh.primitive_plane_add(size=2, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))

#bpy.ops.material.new()
#bpy.data.materials["Material.001"].node_tree.nodes["Diffuse BSDF"].inputs[0].default_value = (0.0804491, 0.8, 0.116792, 1)
posted @ 2022-04-26 19:16  fndefbwefsowpvqfx  阅读(54)  评论(0编辑  收藏  举报