Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CGArtPython/0f74f1c71051150514df309052a3a190 to your computer and use it in GitHub Desktop.
Save CGArtPython/0f74f1c71051150514df309052a3a190 to your computer and use it in GitHub Desktop.
Use a custom displacement image texture in a displacement modifier
# downlaod and extract example image from there https://polyhaven.com/a/mud_cracked_dry_03
import bpy
# set path to the folder where the texture is
image_folder_path = r'c:\path\to\your\textures'
# set the name of the displacement texture image
image_name = 'name_of_your_texture.png'
# load the image
bpy.ops.image.open(directory=image_folder_path, files=[{"name": image_name}])
# get a reference to the image
image_obj = bpy.data.images[image_name]
# create a new texture object and get a reference to it
texture_obj = bpy.data.textures.new(name=image_name, type='IMAGE')
# assign the image object to the image property of the texture object
texture_obj.image = image_obj
# get a reference to the active object
obj = bpy.context.active_object
# apply a subsurf modifier
bpy.ops.object.modifier_add(type='SUBSURF')
subdiv_modifier = obj.modifiers["Subdivision"]
# set the Subdivision levels
subdiv_modifier.levels = 4
subdiv_modifier.render_levels = 4
# apply a displacement modifier
bpy.ops.object.modifier_add(type="DISPLACE")
displace_modifier = obj.modifiers["Displace"]
# assign the texture object we created to the texture prop of the modifier
displace_modifier.texture = texture_obj
displace_modifier.name = f"displace.{image_name}"
displace_modifier.texture_coords = "OBJECT"
displace_modifier.strength = 0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment