Obj.to_mesh() error? (add_Corrective_shape_key.py)

I hope to use “corrective shape key add on” with 2.8. (it must need for me, to edit armature morphs,)


Then have tried to edit file for 2.8 API. I could register, un-register, but could not solve the erroer when
I generate copy mesh (armature modified with shape key).

line 204, in func_object_duplicate_flatten_modifiers
mesh = obj.to_mesh(depsgraph, True, ‘PREVIEW’)
TypeError: Object.to_mesh(): required parameter “calc_undeformed” to be a keyword argument!

I supposed I need to add “calc underomed” argument, (it was optional for 2.7)
then added it like that,

mesh = obj.to_mesh(depsgraph, True, ‘PREVIEW’, True)

But it cause another erroer, this time I seems only need 3 arguments,

corrective_shape_key_alpha.py", line 204, in func_object_duplicate_flatten_modifiers
mesh = obj.to_mesh(depsgraph, True, ‘PREVIEW’, True)
TypeError: Object.to_mesh(): takes at most 3 arguments, got 4

so what I can do? :no_mouth:

You have to pass the arguments as keywords:
obj.to_mesh(depsgraph=your_depsgraph, apply_modifiers=True, calc_undeformed=False)

Basically you look into the API docs for the required arguments, and you pass them in the form keyword=value, where keyword is from the manual and value is what value you want to use for the argument.

For further information you should look for a Python tutorial on positional vs. keyword arguments.

Thanks! After asked here, I tried to change code to use "keywords " (with serch python docs)
then at that time I only changed, about the arguments. (calc undefromed).

I checked API doc for 2.7, about “to_mesh” method, it not use “key words argumetns”,
there is no discribe about it, and “calc_undeformed” are optional arguments. then I checked
But I could not find about this.
So I edit, again ,
mesh = obj.to_mesh(scene, True, ‘PREVIEW’, calc_underormed = False)

But I saw same error message , TypeError: Object.to_mesh(): takes at most 3 arguments, got 4
then I almost gave up, at this time.

As your reply, now I understand, I needed to change all arguments as keywords for blender 2.8.
by your link documetns. I may try it, (now I am test render animation, then need more time,)
the new document is really helpful. thanks:blush:

Thanks I could remove the aruguments erroer, then console not show error message,
but The operation not work as intended :sweat:

this add on could duplicate armature posed mesh, (which applied armature current pose). then edit it, return it as shape key of soruce mesh (armature dformed) by this functtion in 2.79

def func_object_duplicate_flatten_modifiers(scene, obj):
    mesh = obj.to_mesh(scene, True, 'PREVIEW')
    name = obj.name + "_clean"
    new_object = bpy.data.objects.new(name, mesh)
    new_object.data = mesh
    return new_object

Then I change it for 2.8

def func_object_duplicate_flatten_modifiers(depsgraph, view_layer, obj):
    mesh = obj.to_mesh(depsgraph=depsgraph, apply_modifiers=True, calc_undeformed=False)
    name = obj.name + "_clean"
    new_object = bpy.data.objects.new(name, mesh)
    new_object.data = mesh
    return new_object

The function is called by Operator. I edit it too.

class object_duplicate_flatten_modifiers(bpy.types.Operator):
    """Duplicates the selected object with modifiers applied"""

    bl_idname = "object.object_duplicate_flatten_modifiers"
    bl_label = "Duplicate and apply all"

    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        view_layer = bpy.context.view_layer
        depsgraph = bpy.context.depsgraph
        obj_act = context.active_object

        new_object = func_object_duplicate_flatten_modifiers(depsgraph, view_layer, obj_act)

        # setup the context

        view_layer.objects.active = new_object

        return {'FINISHED'}


I do not know, why it just generate zero posed mesh. ( armature modifier seems not applied) I feel If I use depsgraph as worng way, but I think, “obj.to_mesh()” can not apply armature with my arguments setting,

Thanks BYOB I gave up once, but I try again and I could up-date correctly.


I seems miss used depthgraph argument, and matrix formula (changed with 2.8) then it did not work before.

As for me, this small script was very handy.
This script only forgive me to direct edit posing mesh with many shape keys (driven) and save(return) as new shape key of base shape, without changing any detail.

I am not used to up-load add on for official, and do not know how to request, for git, then just attach link for up-dated script file.If someone need to use it, download as you need, and test please, untill official update.

(update for recent API change)

Hi, there were up-date about Obj.to_mesh() Mar 27


I afraid, I might need to up-date again. Then actually my up-date version (Drop box link) plug in not work any more :persevere:

The problem is, this script can generate mesh as new obj, with applied all modifier.(so we can edit easy )

but when transfer edit shape to Posed mesh, it can not generate shape key, with keep delta anymore. (so it now generate only zero morphed shape key)

If someone can check script (I up-loaded in drop box) then help me to work ?
I could confirm, a week ago it worked. (generate shape key as it should be) with use slow all modifier version. I can not contact the original plug in author, but if someone already up-date this plug in for 2.8 your custom use, please help me. (I really often use this add on still)

blender UI options do not offer way to generate shape keys for posed mesh, which take count in modifiers effect (with armature preserve volume option) from another mesh. So I need to use bpy.

Is there any one who can up-date the" Add corrective shape key" add on for recent 2.8?

hope to know how you modified.
(“transfer slow option” only can count preserve volume defomation, and other modifier effect correctly
but now I could not at least about my edit version,)

Thanks I finally could up-date this add on again by Sergey Sharybin (sergey) discribe what I need to change.

object.to_mesh ignores object.update_from_editmode

I still do not know clear, when I need to change way about mesh.data update, with use object.to_mesh,

But anyway it actually solve this problem. Though I do not know anyone need my up-date version, but I correclt drop box file too.
The original add on generate morph, which change shape from base shape. ,then if I already apply many shape keys (driven shape keys etc), after generate new shape key, other shape key delta added too.

So there may better option, remove other shape key delta (which currently used) at same time.

But I only up-date as same as original (for 2.7) l to work for 2.8.

1 Like