1
$\begingroup$

The following python operator crashes blender. I'm using 2.93.5 on Win10.

Strangely however, if I use the python terminal built into blender I can import bpy, bmesh and from bmesh.types import BMVert and then paste the entire code between ### START ### and ### END ### and it all works fine, with no crash.

But when I try to run the code from the python terminal by typing bpy.ops.mesh.generate_arms() then blender will immediately crash.

Is there something wrong with the code, or is this something I should file a bug report for blender?

import bpy, bmesh
from bmesh.types import BMVert

class MESH_OT_Generate_Arms(bpy.types.Operator):
    """Generate Arms"""
    bl_idname = "mesh.generate_arms"
    bl_label = "Generate Arms"

    def execute(self, context):
        
        ### START ###

        #create cube
        bpy.ops.mesh.primitive_cube_add(size=0.1)
        
        # turn on edit mode
        bpy.ops.object.mode_set(mode='EDIT')
        
        # get the mesh data of the active object
        ao_d = context.active_object.data
        
        # get bmesh from ao_d
        bm = bmesh.from_edit_mesh(ao_d)

        bm.faces.ensure_lookup_table()
        
        # extrude
        ext = bmesh.ops.extrude_face_region(bm, geom=[bm.faces[1]])
        
        # translate
        bmesh.ops.translate(bm, vec=(0, 1, 0), verts=[v for v in ext["geom"] if isinstance(v, BMVert)])
        
        bm.normal_update()
        
        # update the viewport so we can see what we are doing
        bmesh.update_edit_mesh(ao_d, True, True)
        
        # return to object mode
        bpy.ops.object.mode_set(mode='OBJECT')

        ### END ###
        
        return {'FINISHED'}
      
def register():
    bpy.utils.register_class(MESH_OT_Generate_Arms)
    
def unregister():
    bpy.utils.unregister_class(MESH_OT_Generate_Arms)
    
if __name__ == '__main__':
    register()

Here is a screenshot of just cutting and pasting the text in the terminal, showing that it works fine. It only crashes when I run it in the terminal with bpy.ops.mesh.generate_arms()

It still immediately crashes blender when I run bpy.ops.mesh.generate_arms() in the terminal.

It also immediately crashes blender if I search with F3, and then click "Generate Arms" to run it that way.

enter image description here

$\endgroup$
6
  • $\begingroup$ one problem is that you shouldn't call bm.free when you use bmesh_from_edit_mesh but should instead call bmesh.update_edit_mesh, which you've already done. Also you don't need the del call, since bm will go out of scope anyway. $\endgroup$ Dec 10, 2021 at 18:42
  • $\begingroup$ Thanks, I have now removed those, and updated the code above, it still crashes blender as I have described. $\endgroup$ Dec 10, 2021 at 19:06
  • $\begingroup$ I think I have narrowed it down to line right after # extrude because I commented out the file line by line, testing each time if it would crash, starting from the bottom, and it continued to crash until I commented out that line. But I have no idea why that line might be cause it to crash. $\endgroup$ Dec 10, 2021 at 19:18
  • $\begingroup$ You're using bpy.context where you should use context, although that shouldn't cause a crash. The code works for me when I run it standalone, rather than as part of the execute method, if that's any help. $\endgroup$ Dec 10, 2021 at 19:52
  • $\begingroup$ I updated the code using just context, still crashes. Are you using Win10 as well as Blender_2.93.5? $\endgroup$ Dec 10, 2021 at 20:04

1 Answer 1

2
$\begingroup$

In the original version of the question you had an unnecessary call to bm.free that you removed. In the current version of the question, there is a use of bpy.context... that should be just context... but the main problem is that the vec argument of bmesh.ops.translate takes a tuple rather than a vector argument, as shown in this example. Here is a version of your script that works in 2.93.6:

import bpy, bmesh
from bmesh.types import BMVert

class MESH_OT_Generate_Arms(bpy.types.Operator):
    """Generate Arms"""
    bl_idname = "mesh.generate_arms"
    bl_label = "Generate Arms"

    def execute(self, context):
        
        ### START ###

        #create cube
        bpy.ops.mesh.primitive_cube_add(size=0.1)
        
        # turn on edit mode
        bpy.ops.object.mode_set(mode='EDIT')
        
        # get the mesh data of the active object
        ao_d = context.active_object.data
        
        # get bmesh from ao_d
        bm = bmesh.from_edit_mesh(ao_d)

        bm.faces.ensure_lookup_table()
        # extrude
        ext = bmesh.ops.extrude_face_region(bm, geom=[bm.faces[1]])
        
        # translate
        bmesh.ops.translate(bm, vec=(0, 1, 0), verts=[v for v in ext["geom"] if isinstance(v, BMVert)])
        
        bm.normal_update()
        
        # update the viewport so we can see what we are doing
        bmesh.update_edit_mesh(ao_d, True, True)
        
        # return to object mode
        bpy.ops.object.mode_set(mode='OBJECT')

        ### END ###
        
        return {'FINISHED'}
      
def register():
    bpy.utils.register_class(MESH_OT_Generate_Arms)
    
def unregister():
    bpy.utils.unregister_class(MESH_OT_Generate_Arms)
    
if __name__ == '__main__':
    register()

For completeness, here's a blend file with the version that works for me.

$\endgroup$
7
  • $\begingroup$ Unfortunately it still crashes when I run it in the python terminal with bpy.ops.mesh.generate_arms() but just like before I can run it in the terminal just by cutting and pasting the pertinent lines of code. But I do appreciate the help and I'm sure those are bugs that you squashed. I added another screenshot to the OP. $\endgroup$ Dec 10, 2021 at 20:22
  • $\begingroup$ It should not give you error messages if you do that. Try going to the 3d viewport, typing F3, and searching for "generate" and executing it that way. That should work. $\endgroup$ Dec 10, 2021 at 20:24
  • $\begingroup$ Going to a 3d viewport, then F3, and searching then clicking "Generate Arms" causes blender to immediately crash. $\endgroup$ Dec 10, 2021 at 20:26
  • $\begingroup$ maybe a 2.93.5 bug? I've upgraded to 2.93.6 $\endgroup$ Dec 10, 2021 at 20:29
  • $\begingroup$ I upgraded to 2.93.6 same result, maybe it is a bug $\endgroup$ Dec 10, 2021 at 20:56

You must log in to answer this question.

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