Hello @ferdinand,
This is the final script for PositionX.
I made it a bit shorter for making it more easy to adapt this to PosYZ/RotXYZ/SclXYZ.
And I added a possibility to undo the complete action (e.g. if there was already a key, which got overridden and the user wants to revert this). I am not sure, whether the undo commands are in the right place, but after some research I placed the AddUndo before the stuff begins and it works so far. For all selected objects.
Just wanted to post it, because maybe some else needs something like this.
"""Sets the current relative pos/rot/scl x/y/z-component of the selected object as a keyframe at the current
time.
"""
import c4d
import typing
doc: c4d.documents.BaseDocument # The active document.
op: typing.Optional[c4d.BaseObject] # The active object, can be None.
# The description level for the relative position, rotation, and scale parameter, we are going to
# reuse these when defining IDs for each component of them.
# Possible: ID_BASEOBJECT_REL_POSITION // ID_BASEOBJECT_REL_ROTATION // ID_BASEOBJECT_REL_SCALE
DLV_REL: c4d.DescLevel = c4d.DescLevel(c4d.ID_BASEOBJECT_REL_POSITION, c4d.DTYPE_VECTOR, 0)
# The description levels for the x, y, and z components of a vector parameter.
# Possible: VECTOR_X // VECTOR_Y // VECTOR_Z
DLV_VEC: c4d.DescLevel = c4d.DescLevel(c4d.VECTOR_X, c4d.DTYPE_REAL, 0)
# Define the IDs for the x/y/z relative SRT parameters.
DID_SRT: c4d.DescID = c4d.DescID(DLV_REL, DLV_VEC)
PRINT_MESSAGE = "Added Key PosX"
def main() -> None:
doc.StartUndo()
for node in doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_NONE):
doc.AddUndo(c4d.UNDOTYPE_CHANGE, node)
# Find the track for the parameter we are interested in creating, here the track for x
# component of the relative position, or create a new track when there is none.
track: c4d.CTrack = node.FindCTrack(DID_SRT)
if track is None:
track = c4d.CTrack(node, DID_SRT)
if not track:
raise MemoryError(f"{track = }")
node.InsertTrackSorted(track)
# Get the current time and the key in #track for this time. CCurve.AddKey will return an
# existing key if there is one, so we do not have to search for potentially existing keys
# first.
t: c4d.BaseTime = doc.GetTime()
curve: c4d.CCurve = track.GetCurve()
keyData: dict = curve.AddKey(t)
if keyData is None:
raise RuntimeError("Could not add key frame.")
# Set the value of the key to the current value of that parameter.
key: c4d.CKey = keyData["key"]
key.SetValue(curve, node[DID_SRT])
# Set different options for the key tangents
key[c4d.ID_CKEY_PRESET] = c4d.ID_CKEY_PRESET_AUTO_OVERSHOOTWEIGHTED
doc.EndUndo()
# Push an update event to Cinema 4D.
c4d.EventAdd()
print (PRINT_MESSAGE)
if __name__ == "__main__":
main()
Is there a documentation how to make a PlugIn of my 9 scripts?
I mean, atm I just saved them into ..\Maxon Cinema 4D 2023_0AF8E603\library\scripts
Maybe it's enough doing a nicely named subfolder there and to add the folder to search location. I don't know. I'll try it out. Never installed a plugin or had a look into a plugin structure yet.
Thank you very much again for your help, Sir!
Kind regards
Vannipo