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

    Joining Polygon Objects (MCOMMAND_JOIN)

    Cinema 4D SDK
    python
    2
    2
    218
    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.
    • KantroninK
      Kantronin
      last edited by ferdinand

      Hi
      The menu Mesh > Convert > Connect Objects allows you to connect polygons.
      I need to make this type of connection with a Python script.
      My current script isn't working properly.

      script_connect.py

      alt text

      ferdinandF 1 Reply Last reply Reply Quote 0
      • ferdinandF
        ferdinand @Kantronin
        last edited by ferdinand

        Hello @Kantronin,

        Thank you for reaching out to us. Your script does not work because it does not follow the conditions of MCOMMAND_JOIN.

        Joins the objects that are parented to the passed null object. Passing multiple objects into SMC will not join them.

        edit: Moved this into the correct forum and added a small example.

        Cheers,
        Ferdinand

        Here is how I would write that in modern Cinema 4D. I have seen that your screenshot is from an older version such as S26, or even older, but I cannot write code examples for such old versions. There are multiple things that wont work in such old version such as type hinting and the mxutils lib, but you will be able to copy the general approach - move everything under a null while preserving the transforms.

        Code
        """Demonstrates how to join all objects in a document.
        """
        
        import c4d
        import mxutils
        
        doc: c4d.documents.BaseDocument  # The currently active document.
        op: c4d.BaseObject | None  # The primary selected object in `doc`. Can be `None`.
        
        def main() -> None:
            """Called by Cinema 4D when the script is being executed.
            """
            # There is no super good way to do this in the manner you implied, joining everything in a
            # document. I clone here the whole document, which is not the cheapest operation (but also
            # not as expensive as it may sound like). When we do not have to join everything, it is better
            # just to clone the things you want to join. En even better way could be to not clone anything
            # and instead use undos to revert the transform and hierarchy changes we have to make.
            temp: c4d.documents.BaseDocument = doc.GetClone(c4d.COPYFLAGS_NONE)
            firstObject: c4d.BaseObject | None = temp.GetFirstObject()
            if firstObject is None:
                c4d.gui.MessageDialog("No object selected.")
                return
            
            # Get all objects in the document, and move them under a null object. We must record and
            # restore their global matrices, when we deconstruct the scene and move it all under one null
            # object.
            allObjects: list[c4d.BaseObject] = list(mxutils.IterateTree(firstObject, True))
            null: c4d.BaseObject = mxutils.CheckType(c4d.BaseObject(c4d.Onull))
            for obj in allObjects:
                mg: c4d.Matrix = obj.GetMg() # save the transform of the object
                obj.Remove() # Technically not necessary in the Python API, as it will do it for you when
                             # you call things like InsertUnderLast. But C++ will not do that and a node
                             # cannot be inserted more than once in a document, otherwise the fireworks
                             # will start.
                obj.InsertUnderLast(null)
                obj.SetMg(mg) # restore the transform of the object under the null
        
            # Insert the null object into the temporary document and then join all objects under it.
            temp.InsertObject(null)
            result: list[c4d.BaseObject] = c4d.utils.SendModelingCommand(
                command=c4d.MCOMMAND_JOIN,
                list=[null],
                doc=temp,
                mode=c4d.MODELINGCOMMANDMODE_ALL,
            )
            if not result:
                c4d.gui.MessageDialog("Failed to join objects.")
                return
            
            # Now insert the joined object into the original document.
            joinedObject: c4d.BaseObject = result[0]
            joinedObject.SetName("Joined Object")
            joinedObject.Remove()
            doc.InsertObject(joinedObject)
        
            c4d.EventAdd()
        
        if __name__ == '__main__':
            main()
        

        MAXON SDK Specialist
        developers.maxon.net

        1 Reply Last reply Reply Quote 0
        • ferdinandF ferdinand moved this topic from General Talk on
        • First post
          Last post