2
$\begingroup$

I tried bpy.ops.texture.new() to create a new texture using python, however it does not work. I have seen another post here, but couldn't get this to work either. Ideally, I would like to give it a name as well:

bpy.ops.texture.new()
bpy.data.textures["Texture"].type = 'VORONOI'
bpy.data.textures["Texture"].noise_intensity = 10
bpy.data.textures["Texture"].color_mode = 'POSITION_OUTLINE'
$\endgroup$
2
  • $\begingroup$ Does this help? Otherwise please add an example what kind of texture (node based, generic...). $\endgroup$
    – brockmann
    Sep 17, 2019 at 8:47
  • $\begingroup$ I've edited my post to clarify $\endgroup$ Sep 17, 2019 at 9:07

1 Answer 1

2
$\begingroup$

Name is no guaranty as another texture with the same name may exist. Use bpy.data.textures.new() and just pass a name as well as the modifier type:

import bpy

# Get the object and context
C = bpy.context
ob = C.object

# Generate the texture and set the attributes
voronoi_tex = bpy.data.textures.new("displace_voronoi", 'VORONOI')
voronoi_tex.noise_intensity = .5
voronoi_tex.noise_scale = .5
voronoi_tex.nabla = .05

# Displacement modifier
disp_mod = None
for modifier in ob.modifiers:
    if modifier.type == 'DISPLACE':
        disp_mod = modifier        

if not disp_mod:
    disp_mod = ob.modifiers.new(name='MyVoronoiDisplace', type='DISPLACE')

# Assign the texture
disp_mod.texture = voronoi_tex
disp_mod.strength = .5
disp_mod.vertex_group = "Group"
$\endgroup$
3
  • $\begingroup$ It works perfectly in creating new textures, thank you. The only thing is that it doesn't link up to my displace modifier. In the modifier itself you can select the texture, the python code however does not select it yet..greatly appreciate this. $\endgroup$ Sep 17, 2019 at 10:08
  • $\begingroup$ @MichaelTeiniker, little bug. Corrected above and should work now $\endgroup$
    – lemon
    Sep 17, 2019 at 10:23
  • 1
    $\begingroup$ Should use disp_mod.vertex_group = "Group" as last line of the code above. Will edit it. In general, bpy.context needs a context in which you are sure that the good object is selected. That's why you should avoid it when possible. $\endgroup$
    – lemon
    Sep 17, 2019 at 10:44

You must log in to answer this question.

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