bpy.context.fluid 是在 Blender 中创建和编辑流体模拟时所使用的上下文对象。下面是一些常用的属性和方法:
bpy.context.fluid.settings
:流体模拟的整体设置信息。bpy.context.fluid.domain_settings
:流体模拟中容器的设置信息。bpy.context.fluid.domain
:流体模拟中容器对象的引用。bpy.context.fluid.objects
:流体模拟中所有粒子系统和固体对象的列表。bpy.context.fluid.reset()
:重置流体模拟以进行新的模拟。bpy.context.fluid.bake()
:对当前流体模拟进行烘焙,即计算出所有帧的流体模拟器数据。bpy.context.fluid.free_bake()
:清除烘焙的流体数据以释放内存。bpy.context.fluid.swap()
:在流体模拟图像序列的前后进行交换。import bpy
# 选择场景中的一个对象作为容器
container_obj = bpy.context.selected_objects[0]
# 创建一个新的流体模拟
bpy.ops.fluid.simulation_create()
# 将容器对象设置为流体模拟中的容器
bpy.context.scene.objects.active = container_obj
bpy.context.active_object.dimensions = (2, 2, 2) # 设置容器大小
# 将一个圆柱体对象添加到流体模拟中作为流体
bpy.ops.mesh.primitive_cylinder_add(radius=0.5, depth=2.0, location=(0, 0, 1.5))
fluid_obj = bpy.context.selected_objects[0]
fluid_mod = fluid_obj.modifiers.new("Fluid", type="FLUID_SIMULATION")
fluid_mod.settings.type = "LIQUID" # 设置流体类型为液体
fluid_mod.settings.use_export_point_cache = True # 允许烘焙模拟数据
# 将流体模拟对象添加到容器中
bpy.ops.object.select_all(action='DESELECT')
fluid_obj.select = True
container_obj.select = True
bpy.context.scene.objects.active = container_obj
bpy.ops.object.parent_set(type='GROUP')
# 启动模拟
bpy.context.scene.frame_start = 0
bpy.context.scene.frame_end = 100
bpy.ops.ptcache.bake_all(bake=True)
import bpy
# 获取选中的流体模拟对象
fluid_obj = bpy.context.selected_objects[0]
# 修改流体模拟的一些属性
fluid_mod = fluid_obj.modifiers["Fluid"]
fluid_mod.settings.viscosity_base = 0.1
fluid_mod.settings.viscosity_exponent = 2.0
# 取消流体模拟对象的烘焙数据并重新启动烘焙
bpy.context.scene.selected_objects[:] = [fluid_obj]
bpy.ops.ptcache.free_bake_all()
bpy.context.scene.frame_current = 0
bpy.context.scene.frame_end = 100
bpy.ops.ptcache.bake_all(bake=True)