blender bpy入门笔记

blender bpy入门笔记目录 安装 bpy 关键概念 拷贝对象 bpycv 加载 渲染 demo 可以导出图片 但是图片为空 导出 obj 模型 随机旋转 录制常见脚本 渲染属性 胶片 gt 透明 其他命令

大家好,我是讯享网,很高兴认识大家。

目录

安装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'

小讯
上一篇 2025-01-17 15:21
下一篇 2025-04-05 21:24

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/90900.html