I want to create a blocky random terrain by extruding all faces of a plane a random amount. I had the idea you were supposed to iterate through all faces, select one, extrude at random, deselect, select next etc.etc. I made a script but it does not seem to be working. It will only extrude once on the face i have manually preselected. If I select none, nothing happens.

here is my script:

import bpy, bmesh, random

objList = list(bpy.data.objects)

mesh = None

for obj in objList:
    if obj.name == 'Plane':
    mesh = obj

if mesh != None:

    for face in mesh.data.polygons:

         randExt = random.uniform(0.001,0.06)


        #select does not seem to be working.
        face.select = True

        "snap_point":(0, 0, 0),
        "snap_normal":(0, 0, 0),

        bpy.ops.mesh.select_all(action = 'DESELECT')

  • $\begingroup$ I think this answer is what you are after. $\endgroup$
    – sambler
    Feb 7 '15 at 12:32

You can extrude individual faces quite efficiently with the bmesh module:

import bpy
import bmesh
from mathutils import Vector
from random import random

bpy.ops.mesh.primitive_grid_add(x_subdivisions=10, y_subdivisions=10, radius=5)
ob = bpy.context.object
me = ob.data

bm = bmesh.new()
faces = bm.faces[:]

for face in faces:
    r = bmesh.ops.extrude_discrete_faces(bm, faces=[face])
    bmesh.ops.translate(bm, vec=Vector((0,0,random()*2)), verts=r['faces'][0].verts)


Generated mesh, mine-craft style

If you used the standard API, a lot of mode changes would be required (select face in object mode, switch to edit mode, extrude face, switch back to object mode and so on). Mode switching is time and memory intense, thus it should be avoided as much as possible.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.