blender bpy入门笔记

目录

安装bpy

关键概念:

拷贝对象

bpycv:

加载,渲染demo

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

导出obj模型:

随机旋转

录制常见脚本:

渲染属性:胶片->透明

其他命令:


安装bpy

pypi上搜索 bpy,然后,下载whl,可以试试

关键概念:

Blender Python 编程:关键概念 - 知乎

拷贝对象

    armature_copy = armature.copy()
    armature_copy.data = armature.data.copy()
    bpy.context.collection.objects.link(armature_copy)

    # 确保复制的骨架处于姿势模式
    bpy.ops.object.mode_set(mode='OBJECT')
    bpy.context.view_layer.objects.active = armature_copy

bpycv:

https://github.com/DIYer22/bpycv

加载,渲染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'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值