blender bpy入门笔记

目录

bpycv:

加载,渲染demo

可以导出图片,但是图片为空:

导出obj模型:

随机旋转

录制常见脚本:

渲染属性:胶片->透明

其他命令:


bpycv:

https://github.com/DIYer22/bpycv

扫描二维码关注公众号,回复: 14591041 查看本文章

加载,渲染demo

import bpy
import os

# 设置场景
scene = bpy.context.scene
scene.render.engine = 'CYCLES'
scene.world.color = (1, 1, 1)

for obj in bpy.context.scene.objects:
    if obj.name in ['Light','Camera']:
        continue
    print('del',obj.name)
    bpy.context.view_layer.objects.active = obj
    bpy.ops.object.delete()
# 加载模型
bpy.ops.import_scene.obj(filepath=r"F:\bjl_1.obj")

# 检查是否有网格对象
if bpy.context.selected_objects:
    # 选择第一个网格对象
    obj_object = bpy.context.selected_objects[0]
    # 检查网格对象是否为 Mesh 类型
    if obj_object.type == 'MESH':
        print("obj 模型已正确加载")
    else:
        print("obj 模型不是 Mesh 类型")
else:
    print("没有找到 obj 模型")
# obj_object = bpy.context.objects[1]
obj_object = bpy.context.selected_objects[0]
bpy.context.view_layer.objects.active = obj_object

obj_object.scale = (10, 10, 10)

if obj_object.data.materials:
    # 如果 obj 模型有材质,则检查其纹理设置
    for material in obj_object.data.materials:
        if material.texture_paint_slots:
            for texture_slot in material.texture_paint_slots:
                # 编辑纹理的设置
                texture = texture_slot.texture
                # 检查纹理的类型并根据需要编辑设置
                if texture.type == 'IMAGE':
                    # 编辑图像纹理的路径
                    print('-----------------vvvvvvvv------------------')
                    texture.image.filepath = r"F:\3d\bjl1.jpg"
else:
    # 如果 obj 模型没有材质,则创建一个新材质
    material = bpy.data.materials.new("Material")
    obj_object.data.materials.append(material)

# bpy.ops.object.mode_set(mode="EDIT")
# bpy.ops.mesh.subdivide(number_cuts=2)
# bpy.ops.export_scene.obj(filepath="zetaxh.obj")

# 渲染场景
bpy.context.scene.render.filepath=r'F:\3d\Python-3D-Rasterizer-main\pyrender-main\src/aaa.png'
bpy.ops.render.render(write_still=True)

可以导出图片,但是图片为空:

import bpy
import math
import time

# 根据摄像机的名称,获取实例
camera = bpy.data.objects['Camera']

# 摄像机与物体的距离
distance = 1.375

# 总的张数
loc_total_num = 16

# 每张之间的角度间隔
div_angle = 4.5

# 设置渲染为图片
bpy.data.scenes["Scene"].render.image_settings.file_format = 'PNG'

# 循环渲染16张
for i in range(loc_total_num):
    # 角度从 -4.5° * 8 到 4.5° *7,并转换为弧度值
    angle = (i - loc_total_num / 2) * div_angle / 360 * 2 * math.pi

    # 根据三角函数计算摄像机的x,y坐标,z坐标不变
    x = distance * math.sin(angle)
    y = distance * math.cos(angle)

    # 将计算到的坐标赋值给到摄像机
    camera.location[0] = x
    camera.location[1] = -y

    # 将旋转角度赋值给到摄像机,保证摄像头正对着目标
    camera.rotation_euler[2] = angle

    # 设置保存图片的文件名
    bpy.data.scenes["Scene"].render.filepath = r'F:\3d\Python-3D-Rasterizer-main\pyrender-main\src\%02d' % (i)

    # 渲染并保存为文件
    bpy.ops.render.render(write_still=True)

导出obj模型:

可以导出,结果不对

bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.subdivide(number_cuts=2)
bpy.ops.export_scene.obj(filepath="zetaxh.obj")

随机旋转

import bpy
import random

bpy.context.object.rotation_euler = (random.uniform(0, 360), random.uniform(0, 360), random.uniform(0, 360))

录制常见脚本:

渲染属性:胶片->透明

bpy.context.scene.render.film_transparent = True

其他命令:

使用节点:
bpy.context.scene.use_nodes = True


添加节点:
bpy.ops.node.add_node(type="CompositorNodeImage", use_transform=True)


添加mp4 或图片
bpy.ops.node.add_file(filepath="F:\\test_222.mp4")


bpy.data.scenes["Scene"].node_tree.nodes["图像.001"].frame_duration = 10000


bpy.data.scenes["Scene"].node_tree.nodes["图像.001"].use_cyclic = True


bpy.data.scenes["Scene"].node_tree.nodes["缩放"].space = 'RENDER_SIZE'

猜你喜欢

转载自blog.csdn.net/jacke121/article/details/129881323