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

    Running Automatic UV for all objects

    Cinema 4D SDK
    python windows
    3
    3
    1.2k
    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.
    • B
      BineeMan
      last edited by

      Hello everyone!
      I'm making a Python script that applies Automatic UV algorithm for each object in the scene. This algorithm is used for making UVs for lightmaps for hundreds of objects for game engines. It means that I have two UVWs on each object: one for a texture and one for a lightmap.
      The problem is that I don't know how to run "Automatic UV" algorithm with "Packed" mode for the object, I haven't found how to do this in the documentation.

      import c4d
      
      def main():
          doc = c4d.documents.GetActiveDocument()
          objects = doc.GetObjects() #getting all objects
          for obj in objects:
              tags = obj.GetTags() #getting all tags
              lightmapTagIndex = 0
              i = 0
              for tag in tags: #finding tag with "Lightmap" name to avoid modifying UV tag for texture
                  if str(tag.GetName()) == "Lightmap":
                      lightmapTagIndex = i
                      break
                  i += 1
                  
              doc.SetActiveObject(obj, c4d.SELECTION_NEW) #selecting our object
              doc.SetActiveTag(tags[lightmapTagIndex], c4d.SELECTION_NEW) #selecting Lightmap tag 
      
              #Pseudo Code: obj.RunAutimaticUV(mode.Packed)
               
          c4d.EventAdd()
      
      if __name__=='__main__':
          main()
      

      I'm referencing to this Cinema4D's function:
      ads.png
      Thank you for your help.

      DunhouD 1 Reply Last reply Reply Quote 1
      • DunhouD
        Dunhou @BineeMan
        last edited by

        @BineeMan here is my unwrap script, but I didn't find how to change to cubic or angle mode, there seems not a symbol in the .h file.

        import c4d
        
        MCOMMAND_AUTOMATICUV = 1053624
        
        def unwrap(node: c4d.BaseObject):
            doc = node.GetDocument()
            if isinstance(node, c4d.PolygonObject) and node.GetTag(c4d.Tuvw) is not None:
                pass
            elif isinstance(node, c4d.PolygonObject) and node.GetTag(c4d.Tuvw) is None:
                doc.AddUndo(c4d.UNDOTYPE_CHANGE, node)
                settings = c4d.BaseContainer()
                settings[c4d.MDATA_AUTOMATICUV_TAGINDEX] = 0
                settings[c4d.MDATA_AUTOMATICUV_FORCESEAMS] = False
                settings[c4d.MDATA_AUTOMATICUV_OVERLAPMIRRORED] = False
                settings[c4d.MDATA_AUTOMATICUV_OVERLAPIDENTICAL] = False
                settings[c4d.MDATA_AUTOMATICUV_SPACING] = 0.01
                settings[c4d.MDATA_AUTOMATICUV_USENORMALS] = True
                settings[c4d.MDATA_AUTOMATICUV_SPACINGASPECT] = 1.0
                c4d.utils.SendModelingCommand(command = MCOMMAND_AUTOMATICUV,
                                            list=[node],
                                            mode=c4d.MODELINGCOMMANDMODE_POLYGONSELECTION,
                                            bc=settings,
                                            doc=doc)
        
        def do_it():
            doc = c4d.documents.GetActiveDocument()
            for node in doc.GetActiveObjects(0):
                unwrap(node)
            c4d.EventAdd()
        
        if __name__ == '__main__':
            do_it()
        

        https://boghma.com
        https://github.com/DunHouGo

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

          Hi @BineeMan sorry for the late reply I was pretty busy Friday and thanks @Dunhou for demonstrating how Packed Auto UV work.
          For Cubic and Angle you need to use c4d.modules.bodypaint.CallUVComman as demonstrated in the call_uv_command_ example.

          So for the Angle automatic UV you need to use the command ID c4d.UVCOMMAND_OPTIMALMAPPING with the next parameters:

          • c4d.OPTIMALMAPPING_PRESERVEORIENTATION # Preserve Orientation
          • c4d.OPTIMALMAPPING_STRETCHTOFIT # Stretch to Fit
          • c4d.OPTIMALMAPPING_DISTORTION # Maximu, Distortion
          • c4d.OPTIMALMAPPING_RELAXCOUNT # Relaxation Steps
          • c4d.OPTIMALMAPPING_SPACING # Spacing

          And for the Cubic automatic UV you need to use the command ID c4d.UVCOMMAND_OPTIMALCUBICMAPPING with the next parameters:

          • c4d.OPTIMALMAPPING_PRESERVEORIENTATION # Preserve Orientation
          • c4d.OPTIMALMAPPING_STRETCHTOFIT # Stretch to Fit
          • c4d.OPTIMALMAPPING_TWOD # 2D
          • c4d.OPTIMALMAPPING_AREAFAK # Maximum Area Factor
          • c4d.OPTIMALMAPPING_RELAXCOUNT # Relaxation Steps
          • c4d.OPTIMALMAPPING_SPACING # Spacing

          Cheers,
          Maxime.

          MAXON SDK Specialist

          Development Blog, MAXON Registered Developer

          1 Reply Last reply Reply Quote 1
          • ferdinandF ferdinand forked this topic on
          • First post
            Last post