5
$\begingroup$

I found a script with an instruction to run it, but it doesn't work for me. Script is working for other people but if I run it the way it's described I only get errors.

It says:

Just copy the code into a new file on blender's text editor, call the text file apply_with_shape_keys.py, and then, select the object with the modifiers and shape keys, (make a backup, just in case). Now from the console execute it like this:

import apply_with_shape_keys

apply_with_shape_keys.super_apply_modifiers()

My results:

>>> import apply_with_shape_keys
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
ModuleNotFoundError: No module named 'apply_with_shape_keys'

The script text is named apply_with_shape_keys.py

The script:

import bpy

def reset_shape_keys ():    
    for name, shape_key in get_active_block().items():
        shape_key.value = 0

def get_active_block ():
    block_id = bpy.context.object.active_shape_key.id_data.name
    return bpy.data.shape_keys[block_id].key_blocks

def select (selection):
    bpy.ops.object.select_all(action='DESELECT')
    selection.select = True
    bpy.context.scene.objects.active = selection

def select_last_shape_key ():
    shape_key_count = len(get_active_block().items())
    bpy.context.object.active_shape_key_index = shape_key_count - 1

def remove_shape_keys (object):
    selection = bpy.context.object
    select(object)
    shape_key_count = len(get_active_block().items())
    select_last_shape_key()
    for i in range(0, shape_key_count):
        bpy.ops.object.shape_key_remove(all=False)
    select(selection)

def apply_modifiers (object):
    selection = bpy.context.object
    select(object)

    for key, modifier in object.modifiers.items():
        if key != 'Armature':
            bpy.ops.object.modifier_apply(apply_as='DATA', modifier=key)

    select(selection)        

def super_apply_modifiers ():
    original = bpy.context.object
    bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(0, 0, 0), "constraint_axis":(False, False, False), "constraint_orientation":'GLOBAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "gpencil_strokes":False, "texture_space":False, "remove_on_cancel":False, "release_confirm":False, "use_accurate":False})
    backup = bpy.context.object
    backup.name = 'backup'
    remove_shape_keys(original)
    apply_modifiers(original)

    for key, shape_key in get_active_block().items():
        select(backup)
        bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(0, 0, 0), "constraint_axis":(False, False, False), "constraint_orientation":'GLOBAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "gpencil_strokes":False, "texture_space":False, "remove_on_cancel":False, "release_confirm":False, "use_accurate":False})

        meshed_shape_key = bpy.context.object
        select(meshed_shape_key)
        reset_shape_keys()
        get_active_block()[key].value = 1
        bpy.ops.object.convert(target='MESH')

        select(original)
        meshed_shape_key.select = True
        bpy.ops.object.join_shapes()
        select_last_shape_key()
        bpy.context.object.active_shape_key.name = key

        select(meshed_shape_key)
        bpy.ops.object.delete(use_global=False)

How do I get it working?

$\endgroup$
5
$\begingroup$

This has changed

Being able to import py suffixed text editor scripts by name is no longer available in blender 2.8+

Instead of importing, use the Text.as_module() method to return script as a module like object.

For example, copying script from question, pasting into text editor, naming it "Foo"

Python console test:

>>> foo = D.texts['Foo'].as_module()
>>> foo.
        apply_modifiers(
        bpy
        get_active_block(
        remove_shape_keys(
        reset_shape_keys(
        select(
        select_last_shape_key(
        super_apply_modifiers(
| improve this answer | |
$\endgroup$
  • $\begingroup$ The second command is confusing. Do you mean I need to call "foo.super_apply_modifiers()"? $\endgroup$ – user3024701 Aug 9 at 10:44
  • $\begingroup$ The beauty of the python console is .. give it a try to confirm When you type foo.<TAB> Iwhere tab is the tab key) into blenders interactive python console it autocompletes. Result of autocomplete is shown. showing the members of the text editor module foo $\endgroup$ – batFINGER Aug 9 at 11:33
  • $\begingroup$ Can't find the autocompletion but the functions do seem to run, thanks. However there are errors probably unrelated to the topic that I now need to get sorted TypeError: Converting py args to operator properties: : keyword "constraint_orientation" unrecognized $\endgroup$ – user3024701 Aug 9 at 13:23
  • $\begingroup$ On prior version <ctrl><space> is autocomplete. Simply look at the console menu. IIRC constraint_orientation is a version issue too. blender.stackexchange.com/questions/166397/… $\endgroup$ – batFINGER Aug 9 at 13:27
  • $\begingroup$ @user3024701 also any value passed to an operator that is default need not be passed to the operator. Most operators in question code could be replaced with API methods, eg bpy.data.objects.remove(meshed_shape_key) removes the object $\endgroup$ – batFINGER Aug 9 at 18:45

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.