ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

script 2

2022-04-26 19:35:31  阅读:191  来源: 互联网

标签:node random mat script material tree bpy


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有