1
$\begingroup$

I have a script that creates a frustum with an upper lip and I add a loopcut to the mesh to create that lip. When I run the script until the first return in my code, it works fine. But when I run it till the second return, where all I do is select new vertices, for some reason the indexing of the vertices in the mesh data changes and the wrong part receives the loop cut.

How is this possible? Does this have to do with overrides?

import bpy
import numpy as np

# Reset scene/delete all objects
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)
# TODO: LARGE EXTRUDE PERCENTUALLY EQUIVALENT ACROSS ALL NS TYPES SEE LINE 177
def create_NS(d2,S):
    
    '''Create a Normschliff from inputs for dimensions
    of a frustum. d2 is the larger diameter and S the 
    length of the inner side of the frustum (not the 
    height).'''
    
    def calculate_specs(d2,S):
        
        '''Get height and d1 for the frustum.'''
        a_half = 1/20
        L = S*np.cos(a_half)
        d1 = d2 - 2*L*np.tan(a_half)
        
        return L,d1
    
    
    def get_override():
        
        '''Obtains the current overwrite for a scene...or something.'''
        for area in bpy.context.screen.areas:
            if area.type == 'VIEW_3D':
                for region in area.regions:
                    if region.type == 'WINDOW':
                        override = {'area': area, 'region': region, 'edit_object':bpy.context.edit_object}
        
        return override
    
    def select_vertices(vert_list):
        
        '''Selects the vertices in the vert_list. Relevant vertices need to be determined beforehand.
        Vertices can be found in the bpy.context.active_object.data.vertices list. They are indexed 
        by time of creation/in chronological order.'''

        # upper ring vertices selection    
        bpy.ops.object.mode_set(mode = 'OBJECT')
        obj = bpy.context.active_object
        bpy.ops.object.mode_set(mode = 'EDIT') 
        bpy.ops.mesh.select_mode(type="VERT")
        bpy.ops.mesh.select_all(action = 'DESELECT')
        bpy.ops.object.mode_set(mode = 'OBJECT')
        
        for i in vert_list:
            obj.data.vertices[i].select = True
        
        bpy.ops.object.mode_set(mode = 'EDIT')
            
    
    def len_verts():
        
        '''Returns the current length of the vertex list of the active object.'''
        
        obj = bpy.context.active_object
        
        return len(obj.data.vertices)
        
        
    L,d1 = calculate_specs(d2,S)
    
    r1 = d1/2
    r2 = d2/2
    
    # add reference image. this needs to be adapted manually and isn't necessary for the final function.
    bpy.ops.object.load_reference_image(filepath="C:\\Users\\Chris\\Desktop\\Jobs\\Uni\\Virtual Lab\\Models\\NS29_32.png")
    bpy.ops.transform.rotate(value=1.5708, orient_axis='X', orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(True, False, False), mirror=False, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False)
    bpy.ops.transform.resize(value=(0.35, 0.35, 0.35), orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', mirror=False, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False)
    bpy.ops.transform.translate(value=(-0.005, -0, -0.52), orient_axis_ortho='X', orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', 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)
    
    # Add lower radius of frustum
    bpy.ops.mesh.primitive_circle_add(vertices=64, radius=r1/100, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
    
    # Go to edit mode to start vertex editing         
    bpy.ops.object.editmode_toggle()
    bpy.ops.mesh.extrude_region_move(MESH_OT_extrude_region={"use_normal_flip":False,"use_dissolve_ortho_edges":False, "mirror":False}, TRANSFORM_OT_translate={"value":(0, 0, L/100), "orient_axis_ortho":'X', "orient_type":'GLOBAL', "orient_matrix":((1, 0, 0), (0, 1, 0), (0, 0, 1)), "orient_matrix_type":'GLOBAL', "constraint_axis":(True, True, 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, "view2d_edge_pan":False, "release_confirm":False, "use_accurate":False, "use_automerge_and_split":False})
    
    # resize upper radius to r2 size.
    scaling_fac = r2/r1
    bpy.ops.transform.resize(value=(scaling_fac, scaling_fac, scaling_fac), orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False)
    
    # Add loopcuts
    bpy.ops.mesh.loopcut_slide(get_override(),MESH_OT_loopcut={"number_cuts":4, "smoothness":0, "falloff":'INVERSE_SQUARE', "object_index":0, "edge_index":131, "mesh_select_mode_init":(True, False, False)}, TRANSFORM_OT_edge_slide={"value":0, "single_side":False, "use_even":False, "flipped":False, "use_clamp":True, "mirror":False, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "correct_uv":True, "release_confirm":False, "use_accurate":False})
    bpy.ops.mesh.loopcut_slide(get_override(),MESH_OT_loopcut={"number_cuts":1, "smoothness":0, "falloff":'INVERSE_SQUARE', "object_index":0, "edge_index":131, "mesh_select_mode_init":(True, False, False)}, TRANSFORM_OT_edge_slide={"value":0.756757, "single_side":False, "use_even":False, "flipped":False, "use_clamp":True, "mirror":True, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "correct_uv":True, "release_confirm":False, "use_accurate":False})
    bpy.ops.transform.resize(value=(1.01947, 1.01947, 1), orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(True, True, False), mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False)

    return 
    vertex_list_upper_ring = [i for i in range(64,128)] + [i for i in range(64*6,64*7)]
    select_vertices(vertex_list_upper_ring)
    return
    
create_NS(29,32)

```
$\endgroup$

1 Answer 1

0
$\begingroup$

This is not the final solution just a fix I found:

For whatever reason, my blind guessing fixed the problem:

If you change the edge_index in

bpy.ops.mesh.loopcut_slide(get_override(),MESH_OT_loopcut={"number_cuts":1, "smoothness":0, "falloff":'INVERSE_SQUARE', "object_index":0, "edge_index":131, "mesh_select_mode_init":(True, False, False)}, TRANSFORM_OT_edge_slide={"value":0.756757, "single_side":False, "use_even":False, "flipped":False, "use_clamp":True, "mirror":True, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "correct_uv":True, "release_confirm":False, "use_accurate":False})

from 131 to 130, suddenly everything works.

$\endgroup$

You must log in to answer this question.

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