I can't seem to get sufficient context for calling bpy.ops.transform.shrink_flatten() without this error:

RuntimeError: Operator bpy.ops.transform.shrink_fatten.poll() failed, context is incorrect

I've attempted to specify the active object and some area parameters (via this answer),

# attempt to set obj-to-be-resized as active obj
bpy.context.scene.objects.active = obj_to_resize

context = bpy.context
override = context.copy()

for area in context.screen.areas:
    if area.type == 'VIEW_3D':
        override["area"] = area
        override["space_data"] = area.spaces.active
        override["region"] = area.regions[-1] # rule of thumb r.type == 'WINDOW'

bpy.ops.transform.shrink_fatten(override, value=0.02)

but this isn't enough to avoid the error. I'm not sure which context parameters I'm still missing. Inspecting the source (line 947) gave me its polling function (line 373), ED_operator_editmesh, which checks the edit_object.

However, attempting to override this parameter by supplying override["edit_object"] = obj_to_resize produces a seg fault:

# backtrace
blender(BLI_system_backtrace+0x34) [0x561503952f54]
blender(+0xb7b882) [0x561502ede882]
/usr/lib/libc.so.6(+0x37e00) [0x7f060029ce00]
blender(BLI_addtail+0xc) [0x56150391cfac]
blender(ED_region_draw_cb_activate+0x39) [0x561502f04739]
blender(initTransform+0x8d3) [0x561503040e73]
blender(+0xcfd54c) [0x56150306054c]
blender(+0xcfd5ff) [0x5615030605ff]
blender(+0xb836ba) [0x561502ee66ba]
blender(WM_operator_call_py+0x6d) [0x561502ee6fed]
blender(+0xfab340) [0x56150330e340]
/usr/lib/libpython3.7m.so.1.0(_PyMethodDef_RawFastCallKeywords+0x148) [0x7f0605df31c8]
/usr/lib/libpython3.7m.so.1.0(_PyCFunction_FastCallKeywords+0x21) [0x7f0605df3461]
/usr/lib/libpython3.7m.so.1.0(_PyEval_EvalFrameDefault+0x4a75) [0x7f0605e62a75]
/usr/lib/libpython3.7m.so.1.0(_PyEval_EvalCodeWithName+0x2e9) [0x7f0605dac879]
/usr/lib/libpython3.7m.so.1.0(_PyFunction_FastCallDict+0x2fc) [0x7f0605dadadc]
/usr/lib/libpython3.7m.so.1.0(_PyObject_Call_Prepend+0x68) [0x7f0605dbd028]
/usr/lib/libpython3.7m.so.1.0(+0x16d3ef) [0x7f0605e0b3ef]
/usr/lib/libpython3.7m.so.1.0(_PyObject_FastCallKeywords+0x3ac) [0x7f0605e1f2fc]
/usr/lib/libpython3.7m.so.1.0(_PyEval_EvalFrameDefault+0x5d35) [0x7f0605e63d35]
/usr/lib/libpython3.7m.so.1.0(_PyFunction_FastCallKeywords+0x10b) [0x7f0605df2a3b]
/usr/lib/libpython3.7m.so.1.0(_PyEval_EvalFrameDefault+0x76d) [0x7f0605e5e76d]
/usr/lib/libpython3.7m.so.1.0(_PyEval_EvalCodeWithName+0x2e9) [0x7f0605dac879]
/usr/lib/libpython3.7m.so.1.0(PyEval_EvalCodeEx+0x44) [0x7f0605dad7a4]
/usr/lib/libpython3.7m.so.1.0(PyEval_EvalCode+0x1c) [0x7f0605dad7cc]
/usr/lib/libpython3.7m.so.1.0(+0x2381e4) [0x7f0605ed61e4]
/usr/lib/libpython3.7m.so.1.0(PyRun_FileExFlags+0x9e) [0x7f0605ed76ee]
blender(+0xf95b38) [0x5615032f8b38]
blender(+0xb79be5) [0x561502edcbe5]
blender(BLI_argsParse+0xd7) [0x561503908347]
blender(main+0x2e9) [0x561502ec5a39]
/usr/lib/libc.so.6(__libc_start_main+0xf3) [0x7f0600289223]
blender(_start+0x2e) [0x561502edb33e]

What context am I missing? And why does attempting to override edit_object cause a seg fault?

  • 2
    $\begingroup$ AFAIK you cannot override context for edit mode, ie set an object as the edit object without it being in edit mode. Speculate the edit mode data is "loaded" on toggle into edit mode, and the data updated when toggled back. Recent and related This may be more relaxed in 2.8 where multiple objects can be edited simultaneously. $\endgroup$ – batFINGER Apr 27 '19 at 7:16
  • $\begingroup$ @batFINGER thanks! Included your comment in my answer. $\endgroup$ – Jack Lynch May 4 '19 at 0:05

@batFINGER's comment is helpful: you (probably) can't override context for edit mode. I ended up just replacing the shrink_fatten operator with the equivalent function, per this:

def execute(self, context):

    ob = bpy.context.object
    me = ob.data
    bm = bmesh.from_edit_mesh(me)

    for v in bm.verts:
        if v.select:
            v.co += v.normal * self.factor

    bmesh.update_edit_mesh(me, True, False)
    return {'FINISHED'}

which was an order of magnitude easier and could be, in general, potentially the best way to replace relatively simple edit operations instead of overriding their context.


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.