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
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Automatic UV Rectangularize / MDATA_UVRECTANGULARIZE

    Cinema 4D SDK
    2
    2
    476
    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.
    • V
      vwgamedev
      last edited by

      I am currently trying to find out if it is possible to run the UV Rectangulize function via Python. I can only find two references to it in the entire Python SDK MDATA_UVRECTANGULARIZE_ALIGN: int = 2280
      MDATA_UVRECTANGULARIZE_EQUIDISTANT: int = 2281
      and this reference in the Cinema4D C++ SDK
      Unfortunately these are not mentioned in any way in the documentation and I don't understand to which c4d.MCOMMAND_* they belong.

      Basically I built a script that returns each UV island as a BaseSelect, now I want to iterate over this list of BaseSelect and try to apply the rectangulize for each island. I want to avoid doing this via c4d.CallCommand(1055347) if there is another way, especially because the method via CallCommand displays the dialog box with the message in the GUI for a UV island that cannot be rectangulated and this would obviously be extremely annoying in an automated plugin.

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

        Hi sadly the only way is to use c4d.CallCommand(1055347) which does not open the window but directly execute the command.
        You can change the setting as you could in the windows, by editing the value in the BaseContainer of the document.
        Finally it act on the selected polygon, so the best way would be to select them.

        Find bellow a script that you can execute in the script manager that will select all polygon of an object and run the command.

        from typing import Optional
        import c4d
        
        doc: c4d.documents.BaseDocument  # The active document
        op: Optional[c4d.BaseObject]  # The active object, None if unselected
        
        def main() -> None:
            # Enables UV Polygon Mode if not already in any UV mode (needed for GetActiveUVSet to works)
            if doc.GetMode() not in [c4d.Muvpoints, c4d.Muvpolygons]:
                doc.SetMode(c4d.Muvpolygons)
        
            # Retrieves active UVSet, The UV windows need to be opened at least one time
            handle = c4d.modules.bodypaint.GetActiveUVSet(doc, c4d.GETACTIVEUVSET_ALL)
            if handle is None:
                # If fail it may be because the Texture view is not open
                # Open A texture View
                c4d.CallCommand(170103)
                # In S22 you need to update the UV Mesh
                if c4d.API_VERSION >= 22000:
                    c4d.modules.bodypaint.UpdateMeshUV(False)
        
                # Retrieves active UVSet, The UV windows need to be opened at least one time
                handle = c4d.modules.bodypaint.GetActiveUVSet(doc, c4d.GETACTIVEUVSET_ALL)
                if handle is None:
                    raise RuntimeError("There is no Active UVSet")
            
            # Select the polygon you want to operate on
            currentSel = op.GetPolygonS()
            previousSel = currentSel.GetClone() # Will be used to restore the initial selection state at the end
            currentSel.SelectAll(op.GetPolygonCount() - 1 )
            # Update the UV mesh to properly take into account the new selection
            c4d.modules.bodypaint.UpdateMeshUV(True)
        
            # Define the settings
            settings = doc.GetSettingsInstance(c4d.DOCUMENTSETTINGS_MODELING)
            settings[c4d.MDATA_UVRECTANGULARIZE_ALIGN] = True
            settings[c4d.MDATA_UVRECTANGULARIZE_EQUIDISTANT] = False
        
            # Execute the command
            c4d.CallCommand(1055347)
        
            # Restore the initial selection
            previousSel.CopyTo(currentSel)
            c4d.modules.bodypaint.UpdateMeshUV(True)
        
        
            c4d.EventAdd()
        
        if __name__ == '__main__':
            main()
        

        Cheers,
        Maxime.

        MAXON SDK Specialist

        Development Blog, MAXON Registered Developer

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