Extruding already selected faces at different random amounts using python scripting

Hi guys, I’ve got a grid of SELECTED faces (left image) which form part of an existing object which I want to extrude at different random amounts using scripting to give the effect in the right image.

I’m fairly new to scripting and I’ve read a lot from different forums and watched many youtube videos but I still can’t get it to work.

I would be happy if someone could give me a snippet of code to work with.

Thanks

Hi.

This example extrudes and moves selected faces along their normal by a given value using a bmesh operator. These operators usually return the new geometry, which makes it easy to get the right ones and perform some operation after the extrude.

Here I used uniform() from the random module to generate a range (0.0 to 0.05). When you multiply a face’s normal with this value you get a randomized normal displacement vector, which you just add to the vertex coordinates.

Usage: Edit mode, select some faces, run the script.

import bpy, bmesh
from random import uniform

def extrude_individual():
    min_distance = 0
    max_distance = 0.05

    me = bpy.context.object.data
    bm = bmesh.from_edit_mesh(me)

    f_sel = [f for f in bm.faces if f.select]
    ret = bmesh.ops.extrude_discrete_faces(bm, faces=f_sel)

    for face in ret['faces']:
        factor = uniform(min_distance, max_distance)
        for vert in face.verts:
            vert.co += face.normal * factor

    bm.normal_update()
    bmesh.update_edit_mesh(me)

if __name__ == '__main__':
    extrude_individual()
2 Likes