1
$\begingroup$

I've spent 2 days trying to figure this out, and I'm at my wits end. Sorry for the crufty code, but I am trying to replace this chunk here:

        if "__extend__" in object.name:
            object.select_set(True)
            bpy.ops.object.mode_set(mode='EDIT')
            bpy.ops.mesh.select_all(action='DESELECT')
            bpy.ops.object.vertex_group_set_active(group='LegExtender')
            bpy.ops.object.vertex_group_select()

            bpy.ops.mesh.extrude_context_move(MESH_OT_extrude_context={"use_normal_flip":False, "mirror":False},
                    TRANSFORM_OT_translate={
                        "value":(2.32831e-09, -3.72529e-09, 0.0919363),
                        "orient_type":'NORMAL',
                        "orient_matrix":(
                                    (-0.984399, 0.0837139, 0.154759),
                                    (-0.0896121, -0.518401, -0.850429),
                                    (0.15142, 0.85103, 0.502812)),
                        "orient_matrix_type":'NORMAL',
                        "constraint_axis":(False, False, True),
                        "mirror":False,
                        "use_proportional_edit":False,
                        "proportional_edit_falloff":'SMOOTH',
                        "proportional_size":1,
                        "use_proportional_connected":False,
                        "use_proportional_projected":False,
                        "snap":False,
                        "snap_target":'CLOSEST',
                        "snap_point":(0, 0, 0),
                        "snap_align":False,
                        "snap_normal":(0, 0, 0),
                        "gpencil_strokes":False,
                        "cursor_transform":False,
                        "texture_space":False,
                        "remove_on_cancel":False,
                        "release_confirm":True,
                        "use_accurate":False
                        })

            bpy.ops.object.mode_set(mode='OBJECT')

I have a known vertex-group in a known object. The object is the leg of an item, diagonally orientated towards the ground. I want to extrude_context_move that vertex group so that the end of the leg touches a notional floor position. But I have pulled all of my hair out trying to solve this.

I could really do with a non-bpy.ops way to do this. Can anyone help?

$\endgroup$
1
  • 1
    $\begingroup$ It would be nice to have some pictures of your end goal mesh and starting mesh $\endgroup$
    – Reigen
    Commented Jul 4, 2021 at 17:55

1 Answer 1

0
$\begingroup$

I ended up doing some trig. This is what I got:

        if "__extend__" in object.name:
    
            bm = bmesh.new()
            me = object.data
            bm.from_mesh(me)

            deform = bm.verts.layers.deform.active
            verts_select = set()
            bm.verts.ensure_lookup_table()

            for vert in bm.verts:
                for group in vert[deform].items():
                    group_index, _ = group
                    if group_index == 0:
                        verts_select.add(vert)

            start_vert = bm.verts[1227]
            end_vert = bm.verts[1225]
            x_len = abs(end_vert.co[0])
            y_len = abs(end_vert.co[1])
            z_len = abs(end_vert.co[2])

            c = y_len**2 + z_len**2
            c = sqrt(c)

            A = math.acos((z_len**2 + c**2 - y_len**2) / (2 * z_len * c))
            B = math.acos((y_len**2 + c**2 - z_len**2) / (2 * y_len * c))
            C = math.acos((y_len**2 + z_len**2 - c**2) / (2 * y_len * z_len))

            new_b = abs(baseplate_limits_z_min) - 0.05
            alpha = B
            beta = A
            new_a = new_b * tan(alpha)
            new_c = new_b * tan(beta)

            new_y = new_c
            new_z = new_b

            c = x_len**2 + z_len**2
            c = sqrt(c)

            A = math.acos((z_len**2 + c**2 - x_len**2) / (2 * z_len * c))
            B = math.acos((x_len**2 + c**2 - z_len**2) / (2 * x_len * c))
            C = math.acos((x_len**2 + z_len**2 - c**2) / (2 * x_len * z_len))

            new_b = abs(baseplate_limits_z_min) - 0.05
            alpha = B
            beta = A
            new_a = new_b * tan(alpha)
            new_c = new_b * tan(beta)

            new_x = new_c

            old_end_x = end_vert.co[0]
            old_end_y = end_vert.co[1]
            old_end_z = end_vert.co[2]

            end_vert.co[0] = new_x - 0.005
            end_vert.co[1] = new_y
            end_vert.co[2] = -new_z

            offset_x = end_vert.co[0] - old_end_x
            offset_y = end_vert.co[1] - old_end_y
            offset_z = end_vert.co[2] - old_end_z

            print(offset_x,  offset_y, offset_z)

            for vert in verts_select:
                vert.co[0] += offset_x
                vert.co[1] += offset_y
                vert.co[2] -= -offset_z

            bm.to_mesh(me)
            bm.free()
    return
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .