Create New Take Using Python



  • Hi,

    I'm very new to C4D python and am having trouble doing the simplest thing. Eventually I want to create a script that controls quite a few things related to Takes. However, my first baby step is simply to create a new Take and add it to the list.

    My first attempt seems to work but when I right-click my new take I cannot delete it and certain behaviours crash C4D! Can someone tell me what I am doing wrong, as I've copied most of this script from a Cineversity script that seems to behave just fine.

    def main():
        td = doc.GetTakeData()
        pt = td.GetMainTake()
        ct = td.SetCurrentTake(pt)
        nt = td.AddTake("",pt,pt)
        td.InsertTake(nt,pt,2)
        td.SetCurrentTake(nt)
        nt.SetName("My Take")
        c4d.EventAdd()
    

    Any help is much appreciated.



  • Hi,

    welcome to the forum. Your major mistake is to try to clone the main take which apparently is meant to be unique.

    Cheers,
    zipit

    """ This will clone the first take after the main take and insert it as a new
    take.
    """
    
    import c4d
    
    def main():
        """
        """
        take_data = doc.GetTakeData()
        # This is the top most take node in the take window. It cannot be deleted
        # and basically is only there to "bind all the other takes together".
        main_take = take_data.GetMainTake()
        # Its first child which is the first "actual" take.
        child_take = main_take.GetDown()
    
        if child_take is None:
            msg = "Please insert at least one take to clone."
            raise RuntimeError(msg)
    
        # Not really needed.
        # current_take = take_data.SetCurrentTake(main_take)
    
        # Creates a new take and inserts it. The first argument is the name of
        # the node (which does not seem to work), the second the insertion point
        # and the third the cloning template. When you clone here from the main
        # take, you will get a "second main take", i.e. one that is not 
        # deletable and you probably will confuse the heck out of Cinema.
        new_take = take_data.AddTake("", main_take, child_take)
        new_take.SetName("Bob is your uncle.")
    
        # We do not need to invoke TakeData.InsertTake().
    
        # Set the new take as the active one and tell Cinema that we did
        # poke around in the scene graph.
        take_data.SetCurrentTake(new_take)
        c4d.EventAdd()
        
    if __name__ == "__main__":
        main()
    


  • Hi,

    welcome to the forum. Your major mistake is to try to clone the main take which apparently is meant to be unique.

    Cheers,
    zipit

    """ This will clone the first take after the main take and insert it as a new
    take.
    """
    
    import c4d
    
    def main():
        """
        """
        take_data = doc.GetTakeData()
        # This is the top most take node in the take window. It cannot be deleted
        # and basically is only there to "bind all the other takes together".
        main_take = take_data.GetMainTake()
        # Its first child which is the first "actual" take.
        child_take = main_take.GetDown()
    
        if child_take is None:
            msg = "Please insert at least one take to clone."
            raise RuntimeError(msg)
    
        # Not really needed.
        # current_take = take_data.SetCurrentTake(main_take)
    
        # Creates a new take and inserts it. The first argument is the name of
        # the node (which does not seem to work), the second the insertion point
        # and the third the cloning template. When you clone here from the main
        # take, you will get a "second main take", i.e. one that is not 
        # deletable and you probably will confuse the heck out of Cinema.
        new_take = take_data.AddTake("", main_take, child_take)
        new_take.SetName("Bob is your uncle.")
    
        # We do not need to invoke TakeData.InsertTake().
    
        # Set the new take as the active one and tell Cinema that we did
        # poke around in the scene graph.
        take_data.SetCurrentTake(new_take)
        c4d.EventAdd()
        
    if __name__ == "__main__":
        main()
    


  • Hi @zipit . Thank you so much for your help. I don't think I would have ever figured this out for myself!



  • Out of interest - How did you learn C4D Python? I can't find any ground up courses or tutes. They're either too advanced for beginners or so basic they just show you how to drag and drop commands from the script log.

    I've already taken a general python course. But I find the structure of C4D python baffling and need some help understand the basic structure of data in the program.



  • Hi,

    Python was not my first programming language, so I sort of had a heads start. Learning a programming language is always a painful process to some degree, just like for a natural language. The only thing that helps is practice. I am also not too convinced about the quality or general helpfulness of programming language books or tutorials, they are usually a mess IMHO.

    But you are probably more after learning the Python standard libraries and the Cinema API, not the principal syntax of Python.

    There are for once a collection of sort of narrative scripts by Maxon which explain the basics of their classic API. And I know that @Cairyn has some Python stuff on Patreon. I have never seen the content, its probably also classic API only, but judging from his posts here, he seems to put quite some work into it. And then there is of course Cineversity, but that you already know.

    Getting to know APIs can only achieved by practice in the end. The first time doing it might be a bit daunting, but you will see its not so bad in the end. Just dive in and prepare for some bruises in the beginning ;)

    Cheers,
    zipit



  • Hi @davidtodman first of all welcome to the plugincafe community :)

    I would like to point you to few rules for your next topics (I've set up this one correctly, but don't worry this is your first topic)

    @zipit already provide you the answers, but I just wanted to point you to take_system Github repository where you can find several examples about the Take system, finally, I know it's C++ but you will see the code is very similar and you can find more verbal information about the Take system in Take System Overview.

    With that's said I guess we all agree that for the moment the best way to learn the Cinema 4D API is to simply practice and ask questions here whenever there is something you don't understand.

    Cheers,
    Maxime.



  • @zipit Thank you for taking the time to reply. Much appreciated.



  • @m_adam Thanks for your help. Apologies for not following the rules. I'm struggling with the rules of Python at the moment so it's not surprising! Will try to be a better poster in future.



  • Hello. Is this the only way to make a duplicate plugin? We studied the case in a slightly different way. Hi. Is this the only way to make a duplicate plugin? We studied the case a little differently. I will find old abstracts and describe in detail the second way.