blender bpy将顶点颜色转换为UV纹理vertex color to texture

  一、关于环境

安装blender的bpy,不需要额外再安装blender软件。在python控制台中直接输入pip install bpy即可。

二、关于代码

本文所给出代码仅为参考,禁止转载和引用,仅供个人学习。 本文所给出的例子是https://download.csdn.net/download/weixin_42605076/89233917中的obj_000001.ply。

import bpy
import argparse
import sys
import os
for i in range(1):
    
    input_ply = "obj_0000%s.ply"%str(i+1).rjust(2, '0')
    output_obj = "obj_0000%s.obj"%str(i+1).rjust(2, '0')


    name, _ = os.path.splitext(output_obj)
    output_png = '{}.png'.format(name)
    output_mtl = '{}.mtl'.format(name)

    print('Input PLY: {}'.format(input_ply))
    print('Output OBJ: {}'.format(output_obj))
    print('Output PNG: {}'.format(output_png))
    print('Output MTL: {}'.format(output_mtl))

    bpy.ops.object.select_all(action='DESELECT')
    bpy.ops.object.select_by_type(type='MESH')
    bpy.ops.object.delete(use_global=False)

    # # https://docs.blender.org/api/current/bpy.data.html
    # print('Remove default cube mesh')
    # if "Cube" in bpy.data.meshes:
    #     mesh = bpy.data.meshes["Cube"]
    #     bpy.data.meshes.remove(mesh)

    print('Import PLY')
    bpy.ops.import_mesh.ply(
        filepath=input_ply)

    print('Toggle edit mode')
    bpy.ops.object.editmode_toggle()

    print('UV smart project')
    bpy.ops.uv.smart_project()

    # https://blender.stackexchange.com/questions/5668/add-nodes-to-material-with-python
    print('Add shading material')
    material = bpy.data.materials.new('SomeMaterial')
    material.use_nodes = True
    nodes = material.node_tree.nodes

    print('Toggle edit mode')
    bpy.ops.object.editmode_toggle()

    print('Add input vertex color')
    input_node = nodes.new('ShaderNodeVertexColor')
    bsdf_node = nodes.get('Principled BSDF')

    print('Link vertex color to bsdf')
    material.node_tree.links.new(bsdf_node.inputs[0], input_node.outputs[0])

    print('Add texture image')
    texture_node = nodes.new('ShaderNodeTexImage')

    print('Create empty image')
    image = bpy.data.images.new(name='SomeImage', width=1024, height=1024)

    print('Assign image to node')
    texture_node.image = image

    print('Switch to CYCLES render engine')
    bpy.context.scene.render.engine = 'CYCLES'

    print('Select active material')
    bpy.context.active_object.active_material = material

    print('Bake image')
    bpy.context.view_layer.objects.active = bpy.context.active_object
    bpy.ops.object.bake(type='DIFFUSE',
                        pass_filter={'COLOR'}, use_clear=True)

    print('Save image')
    image.save_render(output_png)

    # set map_Kd correctly in mtl file
    print('Set image path')
    image.filepath = os.path.basename(output_png)

    print('Connect texture node to bsdf')
    material.node_tree.links.new(bsdf_node.inputs[0], texture_node.outputs[0])

    print('Export OBJ')
    bpy.ops.export_scene.obj(filepath=output_obj)
# ​(原代码的地址在:jiegec/blender-scripts: Some useful Blender scripts (github.com)​,欢迎大家去给原作者点赞)

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-16 14:38:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-16 14:38:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-16 14:38:04       87 阅读
  4. Python语言-面向对象

    2024-06-16 14:38:04       96 阅读

热门阅读

  1. Spring Cloud应用框架

    2024-06-16 14:38:04       36 阅读
  2. 用ip link add link命令创建vlan子设备

    2024-06-16 14:38:04       33 阅读
  3. Spring 循环依赖详解

    2024-06-16 14:38:04       47 阅读
  4. 三生随记——茶叶的诅咒

    2024-06-16 14:38:04       29 阅读
  5. 【名词解释】Unity中的3D物理系统:碰撞体

    2024-06-16 14:38:04       34 阅读
  6. 查找——顺序查找和折半查找

    2024-06-16 14:38:04       33 阅读
  7. LeetCode题练习与总结:最长连续序列--128

    2024-06-16 14:38:04       33 阅读
  8. 前端 CSS 经典:好用的 CSS 选择器

    2024-06-16 14:38:04       31 阅读
  9. 免费外链网站大全,助力新站收录加速

    2024-06-16 14:38:04       32 阅读
  10. (60)MOS管专题--->(15)MOS场效应管

    2024-06-16 14:38:04       49 阅读
  11. 25.梯度消失和梯度爆炸

    2024-06-16 14:38:04       23 阅读
  12. yocto根文件系统如何配置静态IP地址

    2024-06-16 14:38:04       36 阅读
  13. web前端网上私活:探索、挑战与成长的独特之旅

    2024-06-16 14:38:04       29 阅读