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


#1

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

https://en.blender.org/index.php/Extensions:2.6/Py/Scripts/Animation/Corrective_Shape_Key

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:


#2

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.


#3

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
https://wiki.blender.org/wiki/Reference/Release_Notes/2.80/Python_API
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:


#4

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
    scene.objects.link(new_object)
    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
    view_layer.active_layer_collection.collection.objects.link(new_object)
    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"

    @classmethod
    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
        bpy.ops.object.select_all(action='DESELECT')

        view_layer.objects.active = new_object
        new_object.select_set(True)

        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,