1
$\begingroup$

I can add an object to a scene, and apply a Displace modifier to it using the following code:

bpy.ops.mesh.primitive_cube_add()

bpy.ops.object.modifier_add(type='DISPLACE')

It's also easy to change some of the Modifiers attributes, like so:

bpy.context.object.modifiers["Displace"].strength =  1

However, I'm not sure how to link a texture to the modifier via Python. Tried the following code:

bpy.context.object.modifiers["Displace"].texture = 'Tex'

But it gives this error:

TypeError: bpy_struct: item.attr = val: DisplaceModifier.texture expected a Texture type, not str

Linking a texture to the modifier manually shows nothing in the Info window, so I'm not sure how to accomplish this using Python.

$\endgroup$
1
  • $\begingroup$ try bpy.context.object.modifiers["Displace"].texture = bpy.data.textures['Tex'] $\endgroup$
    – WhatAMesh
    Sep 12, 2018 at 21:17

1 Answer 1

3
$\begingroup$

I had a similar issue and solved it this way. I was trying to create a blobbiness effect:

Make base object:

import bpy
bpy.ops.mesh.primitive_ico_sphere_add(subdivisions = 6, size = 1.0, location = [0.,0.,0.])
base_geometry = bpy.data.objects['Icosphere']
base_geometry.name = 'Base_Geometry'

I add this by default, you can't apply modifiers in edit mode, this assumes the object is already selected:

bpy.ops.object.mode_set(mode='OBJECT')

Make texture:

tex = bpy.data.textures.new("Voronoi", 'VORONOI')
tex.distance_metric = 'DISTANCE_SQUARED'

Make the modifier, set settings, and apply:

modifier = base_geometry.modifiers.new(name="Displace", type='DISPLACE')
modifier.texture = bpy.data.textures['Voronoi']
modifier.strength = -0.2
bpy.ops.object.modifier_apply(apply_as='DATA',modifier='Displace')

Hope it helps

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .