python-3.x - 使用 bpy 渲染具有顶点颜色的对象或将顶点颜色转换为纹理

标签 python-3.x blender bpy

我尝试使用 bpy(版本 2.93.1)渲染一个对象(.obj 和 .ply - 两者都不起作用),但它们是灰色的(没有颜色),尽管它们有顶点颜色。

how object looks like in meshlab

import bpy
import os


bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.delete()

file_loc = '../from-obj-to-photos/objects/glass.ply'
# imported_object = bpy.ops.import_scene.obj(filepath=file_loc, split_mode="OFF")
bpy.ops.import_mesh.ply(filepath=file_loc)
obj_object = bpy.context.selected_objects[0]
print('Imported name:', obj_object.name)

bpy.context.scene.render.filepath = '~/Desktop/photos/img.jpg'
bpy.context.scene.render.engine = 'CYCLES'
bpy.ops.render.render('INVOKE_DEFAULT', write_still=True)

应用此代码后,我得到了一个像这样的灰色玻璃:grey glass

然后我找到了如何解决这个问题,我的最终代码如下所示:

import bpy

bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.delete()

file_loc = '../from-obj-to-photos/objects/glass.ply'
# imported_object = bpy.ops.import_scene.obj(filepath=file_loc, split_mode="OFF")
bpy.ops.import_mesh.ply(filepath=file_loc)
obj_object = bpy.context.selected_objects[0]
print('Imported name:', obj_object.name)


bpy.data.objects["glass"].select_set(True)
bpy.ops.paint.vertex_paint_toggle()

#bpy.context.area.ui_type = 'ShaderNodeTree'

#bpy.ops.material.new()

mat = bpy.data.materials.get("Material")


if mat:
    mat.node_tree.nodes.new("ShaderNodeVertexColor")
    mat.node_tree.links.new(mat.node_tree.nodes[2].outputs['Color'], mat.node_tree.nodes[1].inputs['Base Color'])


bpy.context.scene.render.filepath = '~/Desktop/photos/img.jpg'
bpy.context.scene.render.engine = 'CYCLES'
bpy.ops.render.render('INVOKE_DEFAULT', write_still=True)

但它显示错误:

Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
KeyError: 'bpy_prop_collection[key]: key "Base Color" not found'

要修复它,我必须打开着色器编辑器,点击按钮 browse material to be linked并选择 Material

执行代码后,玻璃被着色( result image )。但我不知道如何自动化上一段中的流程。

另外,我猜想将顶点颜色转换为纹理可能会解决这个问题,但我没有找到如何使用 python 来做到这一点。

如果有人帮助我解决这个问题,我将非常感激。

最佳答案

我找到了解决办法!如果你粘贴if len(bpy.context.active_object.data.materials) == 0: bpy.context.active_object.data.materials.append(bpy.data.materials['Material']) else: bpy.context.active_object.data.materials[0] = bpy.data.materials['Material']

关于python-3.x - 使用 bpy 渲染具有顶点颜色的对象或将顶点颜色转换为纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68392304/

相关文章:

blender - 如何在 Blender 中提取模型的边界框?

python - 在 python Blender (bpy) 中列出每个面的顶点

python - 将结果分配给虚拟变量 _

python-3.x - 使用 PyAudio 防止 ALSA 欠载

python - ValueError:太多值无法在OpenCV项目中解压缩

python - 如何使用 Python 从 PDF 文件中读取简单文本?

python - 导入错误 : No module named 'bpy'

properties - 什么是 RNA(或 DNA)特性?