1
$\begingroup$

I'm trying to write a script to rip and move the selected vertices of an object, but I keep getting an error that my context is incorrect. I'm writing this on Blender 3.3, but I suspect the context will be the same with newer version of Blender.

Error message

Here's my simplest test case that reproduces the error on the default cube scene. I've tried it both in Object mode and Edit mode. I have tried with bpy.ops.mesh.rip() instead of rip_move(), but still get the same error (and I need rip_move for my end project).

import bpy

# Find the default cube in the scene
my_obj = [obj for obj in bpy.context.scene.objects if obj.type == 'MESH'][0]
my_mesh = [m for m in bpy.data.meshes if m.name == my_obj.name][0]

rip_verts = [0, 1, 2] 

# Select the mesh
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = my_obj
bpy.data.objects[my_obj.name].select_set(True)

# Deselect the verts (all selected by default)
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_mode(type='VERT')
bpy.ops.mesh.select_all(action='DESELECT')

# Select the verts. Counter-intuitively, you need to be in Object mode to select the verts
bpy.ops.object.mode_set(mode='OBJECT')
for index in rip_verts:
    my_mesh.vertices[index].select = True
    
bpy.ops.object.mode_set(mode='EDIT') # I've tried this commented out, and still have issues

# Copy/pasted rip_move command from manually doing the operation.
bpy.ops.mesh.rip_move(MESH_OT_rip={"mirror":False, "use_proportional_edit":False, "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "use_proportional_connected":False, "use_proportional_projected":False, "release_confirm":False, "use_accurate":False, "use_fill":False}, TRANSFORM_OT_translate={"value":(0.696373, -0.770531, -0.101068), "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, False), "mirror":False, "use_proportional_edit":False, "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "use_proportional_connected":False, "use_proportional_projected":False, "snap":False, "snap_elements":{'INCREMENT'}, "use_snap_project":False, "snap_target":'CLOSEST', "use_snap_self":False, "use_snap_edit":False, "use_snap_nonedit":False, "use_snap_selectable":False, "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})
$\endgroup$

1 Answer 1

1
$\begingroup$

I wasn't able to get this simple example to work, but my full version of the code now works. I needed to add 'INVOKE_DEFAULT' as a parameter and call it from inside a class that extends bpy.types.Operator. Here's a rough approximation of what that looks like (not tested, just to give a rough idea).

class MyRipOperator(bpy.types.Operator):
    bl_idname = 'my_tools.rip_mesh'
    bl_label = 'Rip it'
    bl_options = {'REGISTER', 'UNDO'} # Enable undo

    def execute(self, context):
        # Select the verts you want to rip, just like the above example
        bpy.ops.mesh.rip_move('INVOKE_DEFAULT') # add whatever parameters you need after INVOKE_DEFAULT
        return {'FINISHED'}

```
$\endgroup$

You must log in to answer this question.

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