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

    Set UVWs for Ngons?

    Cinema 4D SDK
    s26 python
    2
    4
    567
    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.
    • E
      ELJeffery
      last edited by i_mazlov

      For updating the changed UVWs I am using:

      # Update UV data in Cinema 4D using updated data from RizomUV
      def update_uvs_in_cinema4d(doc, obj, poly_sizes, poly_uvw_ids, updated_uvws):
          # Retrieve the active document and object once
          doc, obj = get_active_object()
          if not obj or not isinstance(obj, c4d.PolygonObject):
              gui.MessageDialog("No Polygon Object selected.")
              return
      
          # Get the Polygon Translation Map
          poly_trans_map = obj.GetPolygonTranslationMap()
          polygon_count, ngon_map = poly_trans_map
      
          # Detect if there are ngons by checking if the ngonMap contains repeated values
          all_polygons = obj.GetAllPolygons()
          has_ngons = len(set(ngon_map)) != len(ngon_map)
          ngon_count = obj.GetNgonCount()
          print("Number of polygons including polygons inside any ngons: ", len(all_polygons))
          print("Number of polygons including ngons: ",polygon_count)
          print("Number of ngons: ",ngon_count)
          print("Number of internal ngon edges: ",len(ngon_map) - len(set(ngon_map)))
      
          uvw_tag = obj.GetTag(c4d.Tuvw)
          if not uvw_tag:
              print("No UVW Tag found.")
              return
      
          doc.SetMode(c4d.Mpolygons)
      
          handle = c4d.modules.bodypaint.GetActiveUVSet(doc, c4d.GETACTIVEUVSET_ALL)
          if handle is None:
              c4d.CallCommand(170103)  # Open UV editor
              if c4d.API_VERSION >= 22000:
                  c4d.modules.bodypaint.UpdateMeshUV(False)
      
              handle = c4d.modules.bodypaint.GetActiveUVSet(doc, c4d.GETACTIVEUVSET_ALL)
              if handle is None:
                  raise RuntimeError("No Active UVSet")
      
          uv_index = 0
          assembled_uvws = []
          adjusted_uvws = adjust_uvs(updated_uvws)  # Ensure UVs are normalized correctly
      
          for poly_idx, poly_size in enumerate(poly_sizes):
              uv_dict = {}
              uv_indices = poly_uvw_ids[uv_index:uv_index + poly_size]
              uv_index += poly_size
      
              # Handle triangles
              if poly_size == 3:
                  uv_dict = {
                      'c': c4d.Vector(adjusted_uvws[uv_indices[0] * 3], adjusted_uvws[uv_indices[0] * 3 + 1], adjusted_uvws[uv_indices[0] * 3 + 2]),
                      'b': c4d.Vector(adjusted_uvws[uv_indices[1] * 3], adjusted_uvws[uv_indices[1] * 3 + 1], adjusted_uvws[uv_indices[1] * 3 + 2]),
                      'a': c4d.Vector(adjusted_uvws[uv_indices[2] * 3], adjusted_uvws[uv_indices[2] * 3 + 1], adjusted_uvws[uv_indices[2] * 3 + 2])
                  }
      
              # Handle quads
              elif poly_size == 4:
                  uv_dict = {
                      'd': c4d.Vector(adjusted_uvws[uv_indices[0] * 3], adjusted_uvws[uv_indices[0] * 3 + 1], adjusted_uvws[uv_indices[0] * 3 + 2]),
                      'c': c4d.Vector(adjusted_uvws[uv_indices[1] * 3], adjusted_uvws[uv_indices[1] * 3 + 1], adjusted_uvws[uv_indices[1] * 3 + 2]),
                      'b': c4d.Vector(adjusted_uvws[uv_indices[2] * 3], adjusted_uvws[uv_indices[2] * 3 + 1], adjusted_uvws[uv_indices[2] * 3 + 2]),
                      'a': c4d.Vector(adjusted_uvws[uv_indices[3] * 3], adjusted_uvws[uv_indices[3] * 3 + 1], adjusted_uvws[uv_indices[3] * 3 + 2])
                  }
      
              # Handle ngons (polygons with more than 4 vertices)
              elif has_ngons:
                  # Sort the vertices properly and create the UV dict dynamically
                  alphabet = 'abcdefghijklmnopqrstuvwxyz'
                  for i in range(poly_size):
                      # Make sure to use the correct UV order
                      key = alphabet[i] if i < len(alphabet) else f'v{i}'  # Handle large ngons
                      uv_dict[key] = c4d.Vector(
                          adjusted_uvws[uv_indices[i] * 3],
                          adjusted_uvws[uv_indices[i] * 3 + 1],
                          adjusted_uvws[uv_indices[i] * 3 + 2]
                      )
      
              assembled_uvws.append(uv_dict)
      
          handle.SetUVWFromTextureView(assembled_uvws, True, True, True)
          bodypaint.FreeActiveUVSet(handle)
          c4d.EventAdd()
      

      My problem is, it works with a mesh that is only quads and tris or any combination thereof, but if the mesh contains any ngons then I get this result:
      Issue.PNG

      E 1 Reply Last reply Reply Quote 0
      • E
        ELJeffery @ELJeffery
        last edited by

        @ELJeffery
        The image:
        Issue.PNG

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

          Hello @ELJeffery,

          Welcome to the Maxon developers forum and its community, it is great to have you with us!

          Getting Started

          Before creating your next postings, we would recommend making yourself accustomed with our forum and support procedures. You did not do anything wrong, we point all new users to these rules.

          • Forum Overview: Provides a broad overview of the fundamental structure and rules of this forum, such as the purpose of the different sub-forums or the fact that we will ban users who engage in hate speech or harassment.
          • Support Procedures: Provides a more in detail overview of how we provide technical support for APIs here. This topic will tell you how to ask good questions and limits of our technical support.
          • Forum Features: Provides an overview of the technical features of this forum, such as Markdown markup or file uploads.

          It is strongly recommended to read the first two topics carefully, especially the section Support Procedures: Asking Questions.

          About your First Question

          Please have a look at Support Procedures: How to Ask Questions, specifically in the context of repeatability. We cannot run your code as it is not executable as it is lacking things like imports and also some logic, e.g. adjust_uvs. Otherwise answering your question is pure guess work for us. Please also explain in one to two sentences what you want to achieve. "Updating" uv data is a very broad term. Until there some clarity and executable code, that is unfortunately all I can say.

          Cheers,
          Ferdinand

          MAXON SDK Specialist
          developers.maxon.net

          E 1 Reply Last reply Reply Quote 0
          • E
            ELJeffery @ferdinand
            last edited by

            @ferdinand I figured this issue out so you can mark this as solved/closed. Thank you.

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