Skip to content

Commit

Permalink
Fixed #2 - Unable to insert mesh keyframes
Browse files Browse the repository at this point in the history
- This behaviour was caused by the addon checking for a preexisting
  mesh in the current frame, using the name of the meshes.

- The name system won't work as it will not be associated with the
  correct keyframe if the user moves the keyframe from the original
  position

- This has been fixed by checking the actual keyframes, for existing
  keyed meshes in the current frame
  • Loading branch information
AldrinMathew committed Feb 13, 2022
1 parent 20f8b98 commit b8a04bc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 30 deletions.
2 changes: 1 addition & 1 deletion stopmagic/__init__.py
Expand Up @@ -24,7 +24,7 @@
bl_info = {
"name": "Stopmagic",
"author": "Aldrin Mathew",
"version": (0, 3, 0),
"version": (0, 3, 1),
"blender": (2, 91, 0),
"location": "Sidebar > Stopmagic",
"warning": "beta",
Expand Down
3 changes: 1 addition & 2 deletions stopmagic/functions/get_object_keyframes.py
Expand Up @@ -2,9 +2,8 @@
import bpy


def get_object_keyframes() -> List[int]:
def get_object_keyframes(obj: bpy.types.Object) -> List[int]:
"""Get all keyframes that has mesh keyframes, associated with the active object"""
obj = bpy.context.view_layer.objects.active
keyframes: List[int] = []
if obj is not None:
if obj.get("sm_id") is not None:
Expand Down
47 changes: 22 additions & 25 deletions stopmagic/functions/insert_mesh_keyframe.py
Expand Up @@ -2,14 +2,14 @@
from typing import Any, List
import bpy

from .get_object_keyframes import *
from .handle_onion_skin import *
from .next_qualified_frame import *
from .new_object_id import *
from .update_stopmagic import *


def insert_mesh_keyframe(obj: bpy.types.Object | Any) -> None:
new_keyframe_index: int = bpy.context.scene.frame_current

if obj.type == "MESH":
# Gets the data that's not persistent when the Keyframe is added to the mesh
Expand All @@ -19,7 +19,7 @@ def insert_mesh_keyframe(obj: bpy.types.Object | Any) -> None:
symmetry_y = obj.data.use_mirror_y
symmetry_z = obj.data.use_mirror_z

is_done = insert_mesh_keyframe_ex(obj, new_keyframe_index)
is_done = insert_mesh_keyframe_ex(obj)
if is_done:
fcurves = obj.animation_data.action.fcurves
for fcurve in fcurves:
Expand All @@ -41,42 +41,32 @@ def insert_mesh_keyframe(obj: bpy.types.Object | Any) -> None:
handle_onion_skin(obj)


def insert_mesh_keyframe_ex(obj: bpy.types.Object, frame_index: int) -> bool:
def insert_mesh_keyframe_ex(obj: bpy.types.Object) -> bool:
if obj.get("sm_id") is None:
obj["sm_id"] = new_object_id()
#
object_sm_id = obj["sm_id"]
ob_name_full = obj.name_full
new_mesh_name = ob_name_full + "_sm_" + str(frame_index)
mesh_index = get_next_mesh_index(obj)
mesh_name = ob_name_full + "_sm_" + str(mesh_index)
del_mesh = []
proceed = False
for mesh in bpy.data.meshes:
if mesh.name is not None:
if mesh.name != new_mesh_name:
proceed = True
else:
return False
if bpy.context.scene.frame_current in get_object_keyframes(obj):
return False
else:
proceed = True
#
new_mesh = bpy.data.meshes.new_from_object(obj)
new_mesh.name = new_mesh_name
new_mesh.name = mesh_name
new_mesh["sm_id"] = object_sm_id
new_mesh["sm_datablock"] = frame_index
if proceed:
if obj.get("sm_datablock") is not None:
num = obj.get("sm_datablock")
if num != bpy.context.scene.frame_current:
proceed = True
else:
proceed = True
else:
return proceed
#
new_mesh["sm_datablock"] = mesh_index
if proceed:
obj.data = new_mesh
obj.data.use_fake_user = True
current_frame = bpy.context.scene.frame_current
obj["sm_datablock"] = current_frame
obj.keyframe_insert(data_path='["sm_datablock"]', frame=current_frame)
obj["sm_datablock"] = mesh_index
obj.keyframe_insert(
data_path='["sm_datablock"]', frame=bpy.context.scene.frame_current
)
for mesh in del_mesh:
mesh.use_fake_user = False
update_stopmagic(bpy.context.scene)
Expand All @@ -85,3 +75,10 @@ def insert_mesh_keyframe_ex(obj: bpy.types.Object, frame_index: int) -> bool:
bpy.data.meshes.remove(mesh)
#
return proceed


def get_next_mesh_index(obj: bpy.types.Object) -> int:
if obj.get("sm_datablock") is not None:
return obj.get("sm_datablock") + 1
else:
return 0
2 changes: 1 addition & 1 deletion stopmagic/operators/keyed_frame_next.py
Expand Up @@ -17,7 +17,7 @@ def poll(cls, context: bpy.types.Context) -> bool:
return True

def execute(self, context: bpy.types.Context) -> Set[int] | Set[str]:
keyframes = get_object_keyframes()
keyframes = get_object_keyframes(context.view_layer.objects.active)
if len(keyframes) > 0:
frame = bpy.context.scene.frame_current
keyframes = [k for k in keyframes if k > frame]
Expand Down
2 changes: 1 addition & 1 deletion stopmagic/operators/keyed_frame_previous.py
Expand Up @@ -17,7 +17,7 @@ def poll(cls, context: bpy.types.Context) -> bool:
return context.view_layer.objects.active is not None

def execute(self, context: bpy.types.Context) -> Set[int] | Set[str]:
keyframes = get_object_keyframes()
keyframes = get_object_keyframes(context.view_layer.objects.active)
if len(keyframes) > 0:
frame = bpy.context.scene.frame_current
keyframes = [k for k in keyframes if k < frame]
Expand Down

0 comments on commit b8a04bc

Please sign in to comment.