1
$\begingroup$

As in title, I'm trying to extrude mesh via python as I have ~1.5k objects to extrude and when I do it in editor it works perfectly but when I run it via python it always go wrong. It just move face and I'm missing one face. So I tried to create it but I can create only half of it for some reason. Can somebody help?

Code:

import bpy, bmesh

for obj in bpy.context.scene.objects:
    if obj.name.startswith("Exterior"): # Name
        print(obj.name)
        bpy.context.scene.objects.active = obj

        # Go to edit mode, face selection mode and select all faces
        bpy.ops.object.mode_set( mode = 'EDIT' )     # Toggle edit mode
        bpy.ops.mesh.select_mode( type = 'FACE' )    # Change to face selection
        bpy.ops.mesh.select_all( action = 'SELECT' ) # Select all faces

        # As extrude move forward base face it needs to be saved
        bm = bmesh.new() # create an empty BMesh        
        bm = bmesh.from_edit_mesh(bpy.context.object.data)
        meshVertices = [e for e in bm.verts]    

        # Flip normals
        #bpy.ops.mesh.flip_normals() 

        # Extrude 
        bpy.ops.mesh.extrude_faces_move( 
            MESH_OT_extrude_faces_indiv={"mirror": True}, 
            TRANSFORM_OT_shrink_fatten={"value": -0.4,   # Dirrection and size of extrude
            "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}) 

        # Add face 
        bm.faces.new(meshVertices) # len(meshVertices)

        # Update & Destroy BMESH
        bmesh.update_edit_mesh(bpy.context.object.data) # Write the bmesh back to the mesh
        bm.free()  # free and prevent further access

        # At end recalculate UV
        bpy.ops.mesh.select_all( action = 'SELECT' )
        bpy.ops.uv.smart_project()

        # Switch back to Object at end
        bpy.ops.object.mode_set( mode = 'OBJECT' )

        # Origin
        #bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')

Mesh to be extruded.

Mesh to extrude

Extruded mesh.

Extruded mesh

Second side of extruded mesh.

Extruded mesh problem

PS: Mesh is just a 4 vertices square (one face) and I'm trying to make it 3d from 2d

$\endgroup$
1
$\begingroup$

I have done it. Here is code if anyone is interested:

import bpy, bmesh

for obj in bpy.context.scene.objects:
if obj.name.startswith("Exterior.462"): # Name
    print(obj.name)
    bpy.context.scene.objects.active = obj

    # Go to edit mode, face selection mode and select all faces
    bpy.ops.object.mode_set( mode = 'EDIT' )     # Toggle edit mode
    bpy.ops.mesh.select_mode( type = 'FACE' )    # Change to face selection
    bpy.ops.mesh.select_all( action = 'SELECT' ) # Select all faces

    # Create Bmesh
    bm = bmesh.new()
    bm = bmesh.from_edit_mesh( bpy.context.object.data )

    # Extude Bmesh
    for f in bm.faces:
        face = f.normal
    r = bmesh.ops.extrude_face_region(bm, geom=bm.faces[:])
    verts = [e for e in r['geom'] if isinstance(e, bmesh.types.BMVert)]
    TranslateDirection = face * 1 # Extrude Strength/Length
    bmesh.ops.translate(bm, vec = TranslateDirection, verts=verts)

    # Update & Destroy Bmesh
    bmesh.update_edit_mesh(bpy.context.object.data) # Write the bmesh back to the mesh
    bm.free()  # free and prevent further access

    # Flip normals
    bpy.ops.mesh.select_all( action = 'SELECT' )
    bpy.ops.mesh.flip_normals() 

    # At end recalculate UV
    bpy.ops.mesh.select_all( action = 'SELECT' )
    bpy.ops.uv.smart_project()

    # Switch back to Object at end
    bpy.ops.object.mode_set( mode = 'OBJECT' )

    # Origin to center
    bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY', center='BOUNDS')
$\endgroup$
  • 1
    $\begingroup$ pls accept your own answer $\endgroup$ – WhatAMesh Aug 11 '18 at 9:27

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.