Adding random noise to a mesh -- programmatically (bpy.ops.mesh.noise) Trying to draw a scene by a script that is started by a single click, ran into problem creating bumpy terrain.

It is easy to create a slightly bumpy ground using mouse and keyboard -- create  plane, subdivide, add material, add cloud texture, and click 'noise'.  However, all attempts to do the same programmatically have failed:

bpy.ops.mesh.primitive_plane_add
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.subdivide(number_cuts=subdivisions)
bpy.ops.object.editmode_toggle()

mat = bpy.data.materials.new('INVOKE_DEFAULT')
mat.name = "GroundMaterial"
bpy.context.active_object.data.materials.append(mat)

tex = bpy.data.textures.new('CloudNoise', type = 'CLOUDS')
texSlot = mat.texture_slots.add()
texSlot.texture = tex

bpy.ops.object.editmode_toggle()
bpy.ops.mesh.noise(factor=1)
bpy.ops.object.editmode_toggle()


The above code does the noise -- sort of, it does not take the texture as input, so the plane remain plain, it is merely shifted along z-axis.

Copying code from the code console runs in the read-only constraint on the first line:

bpy.context.area.type = 'PROPERTIES'
bpy.context.space_data.context = 'MATERIAL'
bpy.ops.material.new()
bpy.ops.object.material_slot_assign()
bpy.context.space_data.context = 'TEXTURE'
bpy.ops.texture.new()
bpy.data.textures["Texture"].type = 'CLOUDS'
bpy.ops.mesh.noise()


How can I set the 'active' texture, or to navigate to the properties -> textures tab programmatically?
Ugh, found it:

bpy.context.active_object.active_material

bpy.ops.mesh.primitive_plane_add()
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.subdivide(number_cuts=subdivisions)

bpy.ops.object.editmode_toggle()
bpy.ops.object.editmode_toggle()

#mat = getOrCreateMaterial()  Does not work
mat = bpy.data.materials.new("Material")
mat.texture_slots.add().texture = getOrCreateTexture()  #works

bpy.context.active_object.data.materials.append(mat)
bpy.context.active_object.active_material = mat

bpy.ops.mesh.noise(factor=0.5)


Had to look in the C source code for clues.