Skip to content

Instantly share code, notes, and snippets.

@tamask
Created November 30, 2020 16:46
Show Gist options
  • Save tamask/0bfa926cdcc1d5154923aa52a640f38f to your computer and use it in GitHub Desktop.
Save tamask/0bfa926cdcc1d5154923aa52a640f38f to your computer and use it in GitHub Desktop.
Blender addon: Use Simplify F-Curves on any action visible in the graph editor.
import bpy
import curve_simplify
bl_info = {
'name': 'Simplify Action',
'author': 'Tamas Kemenczy',
'version': (0, 1),
'blender': (2, 91, 0),
'description': 'Use Simplify F-Curves on any action visible in the graph editor.',
'category': 'Add Curve',
}
def get_action(context=None):
if not context:
context = bpy.context
for area in context.screen.areas:
if area.type == 'GRAPH_EDITOR':
for space in area.spaces:
if space.type == 'DOPESHEET_EDITOR':
return space.action
class GRAPH_OT_simplify_action(bpy.types.Operator):
bl_idname = 'graph.simplify_action'
bl_label = 'Simplify Action'
bl_description = ("Simplify selected Curves\n"
"Does not operate on short Splines (less than 3 points)")
bl_options = {'REGISTER', 'UNDO'}
error: bpy.props.FloatProperty(
name="Error",
description="Maximum allowed distance error in Blender Units",
min=0, soft_min=0,
default=0.0, precision=5,
step = 0.001)
@classmethod
def poll(cls, context):
return get_action(context) is not None
def execute(self, context):
action = get_action(context)
options = ['DISTANCE', 'DISTANCE', 0, 5, self.error, 5, 0]
fcurves = []
for fc in action.fcurves:
if fc.select:
verts = [v.co.to_3d() for v in fc.keyframe_points.values()]
fcurves.append(verts)
fcurves_sel = []
for i, fc in enumerate(action.fcurves):
if fc.select:
fcurves_sel.append(fc)
for fcurve_i, fcurve in enumerate(fcurves):
if len(fcurve) >= 3:
new_verts = curve_simplify.simplify_RDP(fcurve, options)
new_points = []
for v in new_verts:
new_points.append(fcurve[v])
for i in range(len(fcurve) - 1, 0, -1):
fcurves_sel[fcurve_i].keyframe_points.remove(fcurves_sel[fcurve_i].keyframe_points[i])
for v in new_points:
fcurves_sel[fcurve_i].keyframe_points.insert(frame=v[0], value=v[1])
return {'FINISHED'}
__REGISTER_CLASSES__ = (
GRAPH_OT_simplify_action,
)
def register():
for cls in __REGISTER_CLASSES__:
bpy.utils.register_class(cls)
def unregister():
for cls in __REGISTER_CLASSES__:
bpy.utils.unregister_class(cls)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment