Skip to content

Instantly share code, notes, and snippets.

@Evilsine
Created February 4, 2024 08:17
Show Gist options
  • Save Evilsine/181156262078428996d4d71b9f149d60 to your computer and use it in GitHub Desktop.
Save Evilsine/181156262078428996d4d71b9f149d60 to your computer and use it in GitHub Desktop.
C4D script that deletes unnecessary keyframe tracks(tracks with no value change) on select objects
import c4d
doc: c4d.documents.BaseDocument # The currently active document.
op: c4d.BaseObject | None # The primary selected object in `doc`. Can be `None`.
def allEqual(iterator):
iterator = iter(iterator)
try:
first = next(iterator)
except StopIteration:
return True
return all(first == x for x in iterator)
def main() -> None:
doc.StartUndo()
sel = doc.GetActiveObjects(0)
for obj in sel:
tracks = obj.GetCTracks()
for track in tracks:
curve = track.GetCurve()
keyCount = curve.GetKeyCount()
keys = []
for key in range(keyCount):
keys.append(curve.GetKey(key).GetValue())
if allEqual(keys) == True:
doc.AddUndo(c4d.UNDOTYPE_DELETE, track)
track.Remove()
doc.EndUndo()
c4d.EventAdd()
if __name__ == '__main__':
main()
@patomolinae
Copy link

not working :( , any tips?

@Evilsine
Copy link
Author

Evilsine commented Feb 8, 2024

not working :( , any tips?

which version of c4d are you on? and does it output any error in the python console when you run the script?

@patomolinae
Copy link

not working :( , any tips?

which version of c4d are you on? and does it output any error in the python console when you run the script?

I use c4d 2023, in the python console it does not run :(

@Evilsine
Copy link
Author

Evilsine commented Feb 8, 2024

not working :( , any tips?

which version of c4d are you on? and does it output any error in the python console when you run the script?

I use c4d 2023, in the python console it does not run :(

I tested the script in 2023.2.2 and it's working fine for me. just to make sure you're using the script correctly, can you download the script as a .py file, put it into your user script folder and run it under the user script section in the extensions menu?

image

@patomolinae
Copy link

not working :( , any tips?

which version of c4d are you on? and does it output any error in the python console when you run the script?

I use c4d 2023, in the python console it does not run :(

I tested the script in 2023.2.2 and it's working fine for me. just to make sure you're using the script correctly, can you download the script as a .py file, put it into your user script folder and run it under the user script section in the extensions menu?
image

perfect, I have version 2023.1.3 and I will try with 2023.2.2.

thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment