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
    • Register
    • Login

    Create dynamic selection tag for voronoi fracture polygon index

    General Talk
    programming
    2
    5
    608
    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.
    • F
      filsdegraphiste
      last edited by

      So... I have a little problem, I have made a grid of objects made with Voronoi Fracture, and I'm trying to assign a material to each individual polygon from the fracture object. I can't see a way to assign a material based on the number index of a cloner/fracture object/voronoi object or create a list of selection tag on the voronoi fracture...
      I wish that the index number could be a variable that we could use as a selection or tag! If anyone has an idea of how to tackle this issue, I would love to hear you out, this is what I have so far:

      import c4d
      def main():
          # Get the active object, which should be a Voronoi Fracture object
          fracture_obj = doc.GetActiveObject()
          # Check if the object is a Voronoi Fracture object
          if fracture_obj is None or not fracture_obj.CheckType(c4d.Ofracture):
              raise RuntimeError("Please select a Voronoi Fracture object.")
          # Access the children of the Voronoi Fracture object (these are the fractured pieces)
          fracture_pieces = fracture_obj.GetChildren()
          # Ensure there are fracture pieces
          if not fracture_pieces:
              raise RuntimeError("No fracture pieces found. Please ensure the Voronoi Fracture is applied to an object.")
          # Loop through each fracture piece
          for i, piece in enumerate(fracture_pieces):
              # Create a unique polygon selection tag for each piece
              selection_tag = piece.MakeTag(c4d.Tpolygonselection)
              selection_tag.SetName(f"Piece_{i}_Selection")
              # Select all polygons of this fracture piece
              poly_selection = c4d.BaseSelect()
              poly_count = piece.GetPolygonCount()
              for poly_index in range(poly_count):
                  poly_selection.Select(poly_index)
              # Assign the selection to the tag
              selection_tag.SetBaseSelect(poly_selection)
          # Refresh the scene to apply the changes
          c4d.EventAdd()
      # Execute the script
      if __name__ == '__main__':```
      i_mazlovI 1 Reply Last reply Reply Quote 0
      • F
        filsdegraphiste
        last edited by

        I actually found something that would help me A LOT but I can't find it anywhere online! what I described is what is possible with this python script:
        CV-Parametric Selection Tag for Cinema 4D from Cineversity! I tried to find it but it's impossible to get my hand on it! can someone help please!

        1 Reply Last reply Reply Quote 0
        • i_mazlovI
          i_mazlov @filsdegraphiste
          last edited by i_mazlov

          Hi @filsdegraphiste,
          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

          Generally speaking what you're trying to do is not possible out of the box. There're many unknowns one needs to fix to make the workflow smooth (how do you define a list of materials to assign to fractures? what do you do if there's not enough materials? how do you distinguish between outside/inside polygons? etc..).

          If you really want to achieve this, you can try using Python Generator to manually clone voronoi fracture cache (GetCache()), access its cached polygon objects and assign materials to them (related thread on material assignment topic). Please note, if you decide going this way that this is a very tricky route and there're many places that things can work not in a way you expect them to work, e.g. if you use cloners inside voronois fracture, or splines, or deformers... Also note that python generator is slow, so any scene larger than "a simple voronoi fracture with 20 points of a cube" will lead to significant performance slow downs.

          With that's said, I leave below a very draft and dirty python generator script that just shows the concept.

          You could likely achieve similar results with way less efficiency drops with our C++ API, so I suggest considering this option.

          Cheers,
          Ilia

          The scene to test the concept:
          voronoi_mat_assignment.c4d
          Please note, it's a very draft script, which lacks a lot of necessary checks, so you need to click "Force Update" button on Python Generator object, to actually see the result.

          The screenshot of the scene:
          1b3c9be6-2f63-4686-a0fb-abf40337b065-image.png

          The screenshot of how simple Voronoi Fracture cache looks like:
          853e478f-ea1b-4ab3-9f65-9a82318869a3-image.png

          The script in the python generator object:

          import c4d
          from typing import Iterator
          
          doc: c4d.documents.BaseDocument # The document the object `op` is contained in.
          op: c4d.BaseObject # The Python generator object holding this code.
          hh: "PyCapsule" # A HierarchyHelp object, only defined when main is executed.
          
          def main() -> c4d.BaseObject:
              vorOrig = op.GetDown()
              vor = vorOrig.GetCache(hh).GetClone()
          
              fractures: list[c4d.BaseObject] = vor.GetChildren()
              curMaterial = None
              for fracOp in fractures:
                  if curMaterial is None:
                      curMaterial = doc.GetFirstMaterial()
                  tag = fracOp.MakeTag(c4d.Ttexture)
                  tag[c4d.TEXTURETAG_MATERIAL] = curMaterial
                  curMaterial = curMaterial.GetNext()
          
          
              return vor
          

          MAXON SDK Specialist
          developers.maxon.net

          1 Reply Last reply Reply Quote 0
          • F
            filsdegraphiste
            last edited by

            Hello @i_mazlov!

            Thank you so much for your fast and thoughtful answer, I'm sorry if I didn't post to the right place or in the right form. I'm going to read the rules of the forum more.
            I'm more a designer than a developer but I understand the solution path you provided to me and I truly appreciate it! Do you know anything about the CV-parametric selection tag from Cineversity?

            i_mazlovI 1 Reply Last reply Reply Quote 0
            • i_mazlovI
              i_mazlov @filsdegraphiste
              last edited by

              Hi @filsdegraphiste,

              The links under the youtube video from Donnovan have been broken after updating Cineversity. I've contacted the Web team, they are working on it. As of today it looks like the links have been fixed (e.g. here is the link to the CV-Parametric selection tag tutorial on the Cineversity portal). However, it also looks like the project files are still broken. It might take the Web team some time to recover them completely.

              The CV Toolbox in turn is available on downloads page under "Cineversity Legacy Downloads" section.

              Cheers,
              Ilia

              MAXON SDK Specialist
              developers.maxon.net

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