0
$\begingroup$

Trying to extrude specific polygons. Instead, it's duplicating the whole mesh. I know some kind of updating is required, just having trouble finding the correct solution.

import mathutils,bpy

S=100
c1 =  mathutils.Vector((2/3,3/4,4/5))*10
c2 = -c1

for o in bpy.data.objects:
    bpy.data.objects.remove(o)

def Box(c1,c2):
    bpy.ops.mesh.primitive_cube_add(enter_editmode=False, location=(0, 0, 0))
    bpy.ops.object.mode_set(mode = 'EDIT')
    bpy.ops.transform.resize(value=[abs(n/2) for n in (c1-c2)])
    bpy.ops.object.mode_set(mode = 'OBJECT')
    bpy.context.object.name="Box"
    return bpy.context.object
box = Box(c1,c2)

def Extrude(box,S,c1,c2):
    epsilon = 1e-04
    mesh = box.data
    polygons = mesh.polygons

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

    for c in [c1,c2]:
        for i in [0,1,2]:
            sa=[]
            pci=None
            for p in polygons:
                pci_=p.center[i]
                s = abs(pci_-c[i]) < epsilon
                if s: pci=pci_

                p.select = s

                sa.append(s)
            print(sa) # confirming the correct poly selections

            si = -1. if c[i]<0. else 1. if c[i]>0. else 0.

            movement = [0,0,0]
            movement[i] = ( (S/2-abs(c[i])) - abs(pci) ) * si
            bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value": movement})
Extrude(box,S,c1,c2)

$\endgroup$
  • $\begingroup$ Ok, thank you @batFINGER. $\endgroup$ – Chronocide Mar 20 at 22:42
0
$\begingroup$

seems vert and edge selection was being extruded too, so they needed to be cleared with bpy.ops.mesh.select_all(action='DESELECT'), and mode_set flips to update mesh's selection, and then extrusion result:

def Extrude(box,S,c1,c2):
    epsilon = 1e-04
    mesh = box.data
    polygons = mesh.polygons
    for c in [c1,c2]:
        for i in [0,1,2]:
            sa=[]
            pci=None
            bpy.ops.object.mode_set(mode = 'EDIT')
            bpy.ops.mesh.select_mode( type  = 'FACE'   )
            bpy.ops.mesh.select_all(action='DESELECT')
            bpy.ops.object.mode_set(mode = 'OBJECT')
            for p in polygons:
                pci_=p.center[i]
                s = abs(pci_-c[i]) < epsilon
                if s: pci=pci_
                p.select = s
                sa.append(s)
            si = -1. if c[i]<0. else 1. if c[i]>0. else 0.
            d = [0,0,0]
            d[i] = ( (S/2-abs(c[i])) - abs(pci) ) * si
            bpy.ops.object.mode_set(mode = 'EDIT')
            bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value": d})
    #bpy.ops.object.mode_set(mode = 'OBJECT')
Extrude(box,S,c1,c2)```
| improve this answer | |
$\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.