script 2
作者:互联网
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)
clear_material(mat)
if mat.node_tree.links:
mat.node_tree.links.clear()
if mat.node_tree.nodes:
mat.node_tree.nodes.clear()
mat.use_nodes = True
print(mat.name_full)
nodes = mat.node_tree.nodes
links = mat.node_tree.links
output = nodes.new( type = 'ShaderNodeOutputMaterial' )
diffuse = nodes.new( type = 'ShaderNodeBsdfDiffuse' )
#With names
link = links.new( diffuse.outputs['BSDF'], output.inputs['Surface'] )
#Or with indices
#link = links.new( diffuse.outputs[0], output.inputs[0] )
mat.node_tree.nodes["Diffuse BSDF"].inputs[0].default_value = (r, g, b, 1)
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.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 = 10
cubeLen = 10
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)
return
def RandomScene():
count = 10
cubeLen = 10
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)
标签:node,random,mat,script,material,tree,bpy 来源: https://www.cnblogs.com/Searchor/p/16196129.html