3
$\begingroup$

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)

        bpy.ops.object.mode_set(mode="EDIT")
        bpy.ops.mesh.select_mode(type="FACE")


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

        bpy.ops.mesh.extrude_region_move(
        MESH_OT_extrude_region={"mirror":True}, 
        TRANSFORM_OT_translate={"value":(randExt,0,0),
        "mirror":False,
        "proportional":'DISABLED',
        "proportional_edit_falloff":'SMOOTH',
        "proportional_size":1,
        "snap":False,
        "snap_target":'CLOSEST',
        "snap_point":(0, 0, 0),
        "snap_align":False,
        "snap_normal":(0, 0, 0),
        "release_confirm":False}) 

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

        bpy.ops.object.mode_set(mode="OBJECT")
$\endgroup$
  • $\begingroup$ I think this answer is what you are after. $\endgroup$ – sambler Feb 7 '15 at 12:32
9
$\begingroup$

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()
bm.from_mesh(me)
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)

bm.to_mesh(me)
me.update()

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.

$\endgroup$

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.