Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Beginner:How to set A cube as the child of B cube by python?

    Cinema 4D SDK
    2
    3
    777
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • I
      ilad
      last edited by

      Hi~I recently meet the problem to batch move objects with the same offset distance, however, to set position respectively can be a disaster and a little fool, so I want to make them as children of a Null object that I can move all of them with just move Null object.

      To let other potential beginners better pick up and understand, let me make 2 cubes as an easy example, so how to set B cube as the child of A cube by python? what if two B cubes or more?

      ABcube.png

      import c4d
      A_cube=c4d.BaseObject(c4d.Ocube)
      B_cube=c4d.BaseObject(c4d.Ocube)
      #?????unknow how to set children ???
      
      doc.InsertObject(B_cube)
      c4d.EventAdd()
      
      
      

      Thanks!

      1 Reply Last reply Reply Quote 0
      • M
        m_adam
        last edited by

        Hi @ilad

        TO inserts cube as a child of another one, call BaseList2D.InsertUnder Then here you can call BaseObject.SetRelPos To set the relative position. The relative position is relative to the parent so this way you can easily have the same distance between multiple objects.

        import c4d
        
        # Main function
        def main():
            # Creates 3 cubes in memory
            aCube = c4d.BaseObject(c4d.Ocube)
            bCube = c4d.BaseObject(c4d.Ocube)
            cCube = c4d.BaseObject(c4d.Ocube)
            
            # Inserts C under B, and B under A
            cCube.InsertUnder(bCube)
            bCube.InsertUnder(aCube)
            
            # Defines the relative position of B and C
            bCube.SetRelPos(c4d.Vector(250, 0, 0))
            cCube.SetRelPos(c4d.Vector(250, 0, 0))
        
            # Inserts a and all its children into the current document
            doc.InsertObject(aCube)
            
            # Pushes an update event to Cinema 4D
            c4d.EventAdd()
            
        
        # Execute main()
        if __name__=='__main__':
            main()
        

        Finally, while it's in C++, all the knowledge is applicable in python so I warmly suggest you to read the GeListNode Manual.

        If you have any questions, let me know.
        Cheers,
        Maxime.

        MAXON SDK Specialist

        Development Blog, MAXON Registered Developer

        I 1 Reply Last reply Reply Quote 2
        • I
          ilad @m_adam
          last edited by

          @m_adam Thanks a lot, great help, I will try your way

          1 Reply Last reply Reply Quote 0
          • First post
            Last post