Created
September 18, 2022 06:41
-
-
Save CGArtPython/aa89961b369b4aa2c0b68be9d9d0c70e to your computer and use it in GitHub Desktop.
Blender Python script that copies the location animation on the Z axis from one cube to another.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Copy location animation on the Z axis from one cube to another. | |
| This script expects that there are two cubes in the scene. | |
| The first one named 'Cube' that has an location animation on the Z axis. | |
| The second one named 'Cube.001'. We will be copying the animation from 'Cube' to 'Cube.001'. | |
| """ | |
| # give Python access to Blender's functionality | |
| import bpy | |
| # helper class to make working with Axis arrays more readable | |
| class Axis: | |
| X = 0 | |
| Y = 1 | |
| Z = 2 | |
| # get a reference to the cube with the location animation | |
| source_cube = bpy.data.objects['Cube'] | |
| # get a reference to the cube that we want to apply the location animation | |
| destination_cube = bpy.data.objects['Cube.001'] | |
| # the name of the object animation data we are copying | |
| data_path = 'location' | |
| # loop over all the fcurves of the cube we are copying from | |
| for fcurve in source_cube.animation_data.action.fcurves: | |
| # we only want to copy the location animation on the Z axis | |
| # so we check if the data path matches the one we want | |
| # and | |
| # we check if the animation data is animating on the Z axis | |
| if fcurve.data_path == data_path and fcurve.array_index == Axis.Z: | |
| # now we loop over all the keyframes in the fcurve | |
| for keyframe in fcurve.keyframe_points: | |
| # extract the frame and the value of the location | |
| frame, location_value = keyframe.co | |
| # move the target cube into position | |
| destination_cube.location[Axis.Z] = location_value | |
| # add a location keyframe on the Z axis at the given frame | |
| destination_cube.keyframe_insert(data_path, index=Axis.Z, frame=frame) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Victor,
Thank you for this. But I only manage to convert the source object to active_object and when I try to change the destination_Cube to be a bpy.context.selected_object It give me an error. and also I was try it also to paste on a current frame, but it only copy the last frame.
I attached here my edited script and blender file.
basically this is my problem that I wanted to achieve.
Plate_01(Source object) has his own keyframes in location and base color on material and I wanted
to paste in Cube_01(Destination object) which has his own keyframes. while selecting the two object at the same time with the Source object to be the active object
The only thing I can do this for now is to only select Source object and hit cntrl C and select destination object and hit Cntrl V it on the timeline or graph. but the problem with this method. I needed to apply all this on 20-50 objects like plate_01-40 to Cube_01-40 that have their own individual animations
which it takes my a lot time to do. hehehe.
and some times a got an error to while doin this method. some times only the location and not the base color. and vise versa.
I was hoping you could help me with this.
even if only the transferring of keyframes between two selection will be enough for me. and maybe I'll try to Loop it on my own with the help of your tutorials.
[https://we.tl/t-V0OZEFH8wt](https://we.tl/t-V0OZEFH8wt)
link file