I have a very simple task I want to run inside a function, which is:
bpy.ops.object.duplicate(linked=False, mode='DUMMY')#Duplicate the active object
objDup = bpy.context.scene.objects.active#make it the active object
bpy.ops.object.shape_key_remove(all=True)#and delete all its shapekeys
Now when I run this as is, outside the function, everything’s ok. It does what it’s supposed to, takes the active object, duplicates it, makes the duplicate the active object and removes all its shapekeys.
But when I run it inside a function, it duplicates the active object, but removes the shapekeys from the original instead of the duplicated object, although the code is very clear about it… What gives???
Here’s the pertinent part of the code:
class FreezeShapes(bpy.types.Operator):
bl_label = "Freeze ShapeKeys"
bl_idname = "objeler.frzshps"
def execute(self, context):
obj = bpy.context.active_object
if obj.type == "MESH":
shpKys = obj.data.shape_keys
if shpKys is not None:
self.shapeUtils(obj)
else:
pass
else:
pass
return {'FINISHED'}
def shapeUtils(self, objeler):
objeler.select = True
bpy.ops.object.duplicate(linked=False, mode='DUMMY')
objDup = bpy.context.scene.objects.active
bpy.ops.object.shape_key_remove(all=True)
jacobo’s
<class FreezeShapes(bpy.types.Operator):> works as designed:
duplicates current selected object with shapekey and remove cloned object’s shapekey.
Tested in manual mode (button) and automatic mode (run by other function).
Suppose there is an object with shapekey and it is the only one selected.
Blender 2.72
import bpy
# Menu in toolprops region
class ToolPropsPanel(bpy.types.Panel):
bl_label = "my Buttons in Tool props"
bl_space_type = "VIEW_3D"
bl_region_type = "TOOL_PROPS"
def draw(self, context):
self.layout.operator("objeler.frzshps")
# test 1: manually
# class will be assigned to a button
class FreezeShapes(bpy.types.Operator):
bl_label = "Freeze ShapeKeys"
bl_idname = "objeler.frzshps"
def execute(self, context):
obj = bpy.context.active_object
if obj.type == "MESH":
shpKys = obj.data.shape_keys
if shpKys is not None:
self.shapeUtils(obj)
else:
pass
else:
pass
return {'FINISHED'}
def shapeUtils(self, objeler):
objeler.select = True
bpy.ops.object.duplicate(linked=False, mode='DUMMY')
objDup = bpy.context.scene.objects.active
bpy.ops.object.shape_key_remove(all=True)
# test 2: automatically
# use another function to create instance of this class
class FreezeShapesNotOperator():
bl_label = "Freeze ShapeKeys"
bl_idname = "objeler.frzshps"
#def execute(self, context):
def __init__(self):
obj = bpy.context.active_object
if obj.type == "MESH":
shpKys = obj.data.shape_keys
if shpKys is not None:
self.shapeUtils(obj)
else:
pass
else:
pass
#return {'FINISHED'}
def shapeUtils(self, objeler):
objeler.select = True
bpy.ops.object.duplicate(linked=False, mode='DUMMY')
objDup = bpy.context.scene.objects.active
bpy.ops.object.shape_key_remove(all=True)
if __name__ == "__main__":
# Before run this script, please make sure that
# an object with shapekey is only one selected
# register menu and button
bpy.utils.register_module(__name__)
# automatically run the function,
fs = FreezeShapesNotOperator()