Hi!
Sorry I'm new to Python and still have to learn a lot, I've tested the Github example thanks for that, but I still can't get a keyframe to control my object visibility in Stage Animation?
It would be very nice of you if you could help me again.
Thank you very much!
Solved Script Befehl
Hi,
You should also describe where you are stuck instead of only talking about the final goal.. I've created you this script with comment. Please read them and ask question if there are things you do not understand.
To make it simple, i did not implemented the undo system. You can have a look at our manual to understand a bit better what you have to do. And ask questions :p
Cheers,
Manuel
from typing import Optional
import c4d
def SwitchVisibilityAndCreateKeyFrame (obj) -> None:
# Switch the visibility of the object and create a keyframe at the current time of the document.
# If the keyframe already exist, it will update the keyframe and switch between ON and OFF.
# DescID allow to define how parameter can be access and what data type they are made of.
# Just like parameter, DescID can be composed of several level to access the correct data.
# Those levels composed some sort of path to follow to retrieve the correct data.
# Define two desscID to access the editor and render visibility of the object.
trackIDs = []
trackIDs.append(c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_VISIBILITY_EDITOR, c4d.DTYPE_LONG,0)))
trackIDs.append(c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_VISIBILITY_RENDER, c4d.DTYPE_LONG,0)))
for trackID in trackIDs:
# Define the parameter value in the object itself. If it is undef we define it to off.
# Otherwise, we switch the value.
if obj[trackID] == c4d.OBJECT_UNDEF:
obj[trackID] = c4d.OBJECT_OFF
else:
obj[trackID] = obj[trackID] ^ 1
# With the track ID, FindCTrack can be used to retrieve if a track exist for this object.
track = obj.FindCTrack(trackID)
if track is None:
#if there is no track, create it and insert it in the object.
track = c4d.CTrack(obj, trackID)
if track is None:
raise RuntimeError("Cannot create the track.")
obj.InsertTrackSorted(track)
# Retrieve the curve from the track. For more information see our track manual
curve = track.GetCurve()
if curve is None:
raise RuntimeError("Failed to retrieve the curves, Object may not have curves.")
# Retrieve the current time of the document so the key can be added at the right place
currentTime = doc.GetTime()
keyDict = curve.AddKey(currentTime)
# Checks if the key have been added
if keyDict is None:
raise MemoryError("Failed to create a key")
# Retrieves the inserted key and its index
key = keyDict["key"]
keyIndex = keyDict["nidx"]
# Sets the value of the key. Because in this case, the function SetGeData must be used.
# SetValue should only be used for Float values.
key.SetGeData(curve, obj[trackID])
# Mandatory: Fill the key with default settings
curve.SetKeyDefault(doc, keyIndex)
# Changes it's interpolation to step.
key.SetInterpolation(curve, c4d.CINTERPOLATION_STEP)
def main() -> None:
# Called when the plugin is selected by the user. Similar to CommandData.Execute.
# Retrieve the active objects
allobj = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_CHILDREN)
for obj in allobj:
SwitchVisibilityAndCreateKeyFrame(obj)
c4d.EventAdd()
if __name__=='__main__':
main()
MAXON SDK Specialist
Hello @WDP,
without any further questions and other postings, we will consider this topic as solved and flag it as such by Friday, 17/06/2022.
Thank you for your understanding,
Ferdinand
MAXON SDK Specialist
developers.maxon.net