www.xbdev.net
xbdev - software development
Thursday June 26, 2025
Home | Contact | Support | Blender (.py) Scripts... Automating Blender ..
     
 

Blender (.py) Scripts...

Automating Blender ..

 

Fun with Fluids


The ability to add a simple fluid effect to a scene is brilliant! What is great is that you can do it easily with Blender scripts. We'll clear the scene, add a ground plane and a cube. Then we'll create an sphere - which will be our particle emmiter (fluid is going to flow out of this sphere).

Simply a matter of adding in a light and camera - and we're done!


This is what the fluid simulation looks like after a few frames in the layout window.
This is what the fluid simulation looks like after a few frames in the layout window.


import bpy

# 1. Clear the scene
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()

# 2. Add plane (ground)
bpy.ops.mesh.primitive_plane_add(size=10location=(000))
plane bpy.context.object
plane
.name "Ground"

# 3. Add cube
bpy.ops.mesh.primitive_cube_add(size=2location=(001))
cube bpy.context.object
cube
.name "Cube"

# Make cube a fluid obstacle
bpy.ops.object.modifier_add(type='FLUID')
cube.modifiers["Fluid"].fluid_type 'EFFECTOR'
#cube.modifiers["Fluid"].effector_settings.effector_type = 'OBSTACLE'

# 4. Add small ico sphere (twice height above cube)
bpy.ops.mesh.primitive_ico_sphere_add(radius=0.5location=(005))
sphere bpy.context.object
sphere
.name "FluidEmitter"

# 5. Set up fluid simulation
# First make the sphere a fluid emitter
bpy.ops.object.modifier_add(type='FLUID')
sphere.modifiers["Fluid"].fluid_type 'FLOW'
sphere.modifiers["Fluid"].flow_settings.flow_type 'LIQUID'
sphere.modifiers["Fluid"].flow_settings.flow_behavior 'INFLOW'

# Create a domain (must be named "Fluid" in Blender 4.1)
bpy.ops.mesh.primitive_cube_add(size=8location=(004))
domain bpy.context.object
domain
.name "FluidDomain"

# Set as fluid domain
bpy.ops.object.modifier_add(type='FLUID')
domain.modifiers["Fluid"].fluid_type 'DOMAIN'
domain.modifiers["Fluid"].domain_settings.domain_type 'LIQUID'
domain.modifiers["Fluid"].domain_settings.use_mesh True
domain
.modifiers["Fluid"].domain_settings.resolution_max 32 

# Add pink fluid material
mat bpy.data.materials.new(name="PinkFluid")
domain.data.materials.append(mat)
mat.use_nodes True
nodes 
mat.node_tree.nodes
nodes
.clear()

bsdf nodes.new('ShaderNodeBsdfPrincipled')
bsdf.inputs['Base Color'].default_value = (0.90.40.71.0)  # Pink
bsdf.inputs['Roughness'].default_value 0.1

output 
nodes.new('ShaderNodeOutputMaterial')
mat.node_tree.links.new(bsdf.outputs['BSDF'], output.inputs['Surface'])


# Add camera for better view
bpy.ops.object.camera_add(location=(8, -86))
camera bpy.context.object
camera
.rotation_euler = (1.200.8)
bpy.context.scene.camera camera

# Add lighting
bpy.ops.object.light_add(type='SUN'location=(5510))
sun bpy.context.object
sun
.data.energy 5

# Reset animation frame 
bpy.context.scene.frame_set(1)


Tweaks


We can reposition the camera and make the surface of the fluid 'smooth' - also increase the resolution and do a render - and this what it looks like.


Animation runs for about 50ish frames so we get the fluid mostly covered the cube and is filling up the domain box.
Animation runs for about 50ish frames so we get the fluid mostly covered the cube and is filling up the domain box.


import bpy

# 1. Clear the scene
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()

# 2. Add plane (ground)
bpy.ops.mesh.primitive_plane_add(size=10location=(000))
ground bpy.context.object
ground
.name "Ground"

# 3. Add cube
bpy.ops.mesh.primitive_cube_add(size=2location=(001))
cube bpy.context.object
cube
.name "Cube"

# Make cube a fluid obstacle
bpy.ops.object.modifier_add(type='FLUID')
cube.modifiers["Fluid"].fluid_type 'EFFECTOR'

# 4. Add small ico sphere (twice height above cube)
bpy.ops.mesh.primitive_ico_sphere_add(radius=0.5location=(005))
sphere bpy.context.object
sphere
.name "FluidEmitter"

# 5. Set up fluid simulation
# First make the sphere a fluid emitter
bpy.ops.object.modifier_add(type='FLUID')
sphere.modifiers["Fluid"].fluid_type 'FLOW'
sphere.modifiers["Fluid"].flow_settings.flow_type 'LIQUID'
sphere.modifiers["Fluid"].flow_settings.flow_behavior 'INFLOW'

# Create a domain (must be named "Fluid" in Blender 4.1)
bpy.ops.mesh.primitive_cube_add(size=8location=(003.85))
domain bpy.context.object
domain
.name "FluidDomain"

# Set as fluid domain
bpy.ops.object.modifier_add(type='FLUID')
domain.modifiers["Fluid"].fluid_type 'DOMAIN'
domain.modifiers["Fluid"].domain_settings.domain_type 'LIQUID'
domain.modifiers["Fluid"].domain_settings.use_mesh True
domain
.modifiers["Fluid"].domain_settings.resolution_max 64
# Adjust domain size by scaling the object
domain.scale = (1.01.01.0)  
bpy.ops.object.transform_apply(scale=True)  # Apply the scale

# Set smooth shading for fluid surface
domain.data.polygons.foreach_set('use_smooth', [True] * len(domain.data.polygons))
domain.data.update()

# Add pink fluid material
mat bpy.data.materials.new(name="PinkFluid")
domain.data.materials.append(mat)
mat.use_nodes True
nodes 
mat.node_tree.nodes
nodes
.clear()

bsdf nodes.new('ShaderNodeBsdfPrincipled')
bsdf.inputs['Base Color'].default_value = (0.90.40.71.0)  # Pink
bsdf.inputs['Roughness'].default_value 0.1

output 
nodes.new('ShaderNodeOutputMaterial')
mat.node_tree.links.new(bsdf.outputs['BSDF'], output.inputs['Surface'])


# Add camera for better view
bpy.ops.object.camera_add(location=(10, -106))
camera bpy.context.object
camera
.rotation_euler = (1.200.8)
bpy.context.scene.camera camera

# Add lighting
bpy.ops.object.light_add(type='SUN'location=(5510))
sun bpy.context.object
sun
.data.energy 5

# Reset animation frame 
bpy.context.scene.frame_set(1)






 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2025 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.