简体   繁体   English

如何使用 python 在 Blender 2.9 中将材质的基色设置为等于顶点颜色?

[英]how do I set the base colour of a material to equal vertex colours in Blender 2.9 using python?

This is how I instantiated my mesh object and material:这就是我实例化网格 object 和材料的方式:

mymesh = bpy.data.meshes.new(objname)
myobject = bpy.data.objects.new(objname, mymesh)
mymat = bpy.data.materials.get("Material")
if mymat is None:
    mymat = bpy.data.materials.new(name="Material")
if myobject.data.materials:
    # assign to 1st material slot
    myobject.data.materials[0] = mymat
else:
    # no slots
    myobject.data.materials.append(mymat)

Here's how I set the color of the vertex colors:下面是我如何设置顶点 colors 的颜色:

if len(mymesh.vertex_colors) == 0:
    mymesh.vertex_colors.new()
    
for f in mymesh.vertex_colors[0].data:
    f.color = rgb # (0,0,0,1) for black

I can see the colors when I am in vertex paint mode, but when I am rendering the scene with Eevee or Cycles the colors don't show.当我处于顶点绘制模式时,我可以看到 colors,但是当我使用 Eevee 或 Cycles 渲染场景时,colors 不显示。

I can fix this by changing the base color of the material to equal Vertex Colors, using the UI, however how can I do this using python code?我可以通过使用 UI 将材质的基色更改为等于顶点 Colors 来解决此问题,但是如何使用 python 代码来做到这一点?

Edit: Maybe the solution uses bpy.data.materials["Material"].node_tree.nodes["Principled BSDF"].inputs ?编辑:也许解决方案使用bpy.data.materials["Material"].node_tree.nodes["Principled BSDF"].inputs

I was looking to do something similar.我想做类似的事情。 I don't know if you are still working on it but here is the solution I found which seems to work on Blender 2.92.0.我不知道您是否仍在研究它,但这是我发现的似乎适用于 Blender 2.92.0 的解决方案。

To test the script:要测试脚本:

  • Create a new cube (named "Cube", it's the case by default)创建一个新的立方体(命名为“Cube”,默认是这种情况)
  • Import the script into the scripting window in Blender将脚本导入Blender中的脚本window
  • Hit "run"点击“跑”
import bpy
from random import *

# ---------- SET RANDOM COLORS TO VERTICES ----------

# Load vertex colors data
mesh = bpy.data.objects["Cube"].data
if not mesh.vertex_colors:
    mesh.vertex_colors.new()
    print("Object had no vertex colors, adding them")

# Assign random colors to vertices
for vCol in mesh.vertex_colors[0].data:
    for i in range(len(vCol.color) - 1):
        vCol.color[i] = random()
        
# ---------- LOAD AND LINK MATERIAL TO MESH ----------

# Load material
mymat = bpy.data.materials.get("mymat")
if mymat is None:
    mymat = bpy.data.materials.new(name = "mymat")
    
mymat.use_nodes = True
    
# Link material to mesh
if mesh.materials:
    mesh.materials[0] = mymat
else:
    mesh.materials.append(mymat)
    

# ---------- MANAGE NODES OF SHADER EDITOR ----------

# Get node tree from the material
nodes = mymat.node_tree.nodes
principled_bsdf_node = nodes.get("Principled BSDF")

# Get Vertex Color Node, create it if it does not exist in the current node tree
vertex_color_node = None
if not "VERTEX_COLOR" in [node.type for node in nodes]:
    vertex_color_node = nodes.new(type = "ShaderNodeVertexColor")
else:
    vertex_color_node = nodes.get("Vertex Color")

# Set the vertex_color layer we created at the beginning as input
vertex_color_node.layer_name = "Col"

# Link Vertex Color Node "Color" output to Principled BSDF Node "Base Color" input
links = mymat.node_tree.links
link = links.new(vertex_color_node.outputs[0], principled_bsdf_node.inputs[0])

Here are some resources which helped me to do it:以下是一些帮助我做到这一点的资源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM