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

    Changing the angle of spline-aligned objects

    Cinema 4D SDK
    python 2024 windows
    2
    5
    819
    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.
    • S
      SmetK
      last edited by

      Hey, everybody. The code aligns my objects along the direction of the spline, but the problem is that the corner of the original meshes, do not look along the spline. I have tried c4d.utils.MatrixRotY() and other axes, but I don't get the rotation I want. I feel I can solve this problem somewhere in the green area(screenshot). Is there any solution to this problem? And to have the last object rotated 180 degrees relative to the others. The second screenshot is as it should be.
      123.png
      45ed0532-cfab-48d8-9f77-baa937e17a47-image.png

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

        Hey @SmetK,

        Thank you for reaching out to us. Please provide your code in future postings as pointed out before. Screenshots are not a substitute for code. I have provided a brief example of what you are trying to do.

        Cheers,
        Ferdinand

        Result

        4cf1b10d-933e-43e8-b679-3054cf1a615a-image.png

        Code

        """Constructs cone objects on the selected spline object at five different points and rotates them 
        by 90 degrees on their principal X and Z axes.
        """
        
        import c4d
        
        doc: c4d.documents.BaseDocument  # The currently active document.
        op: c4d.BaseObject | None  # The primary selected object in `doc`. Can be `None`.
        
        # Ninety degrees in radians and two pre-built matrices for rotating 90 degrees around the X and Z 
        # axes. The Y axis is not too useful here since a cone is symmetrical along its Y axis.
        RAD_90_DEG : float = c4d.utils.DegToRad(90)
        TRANSFORM_90_X : c4d.Matrix = c4d.utils.MatrixRotX(RAD_90_DEG)
        TRANSFORM_90_Z : c4d.Matrix = c4d.utils.MatrixRotZ(RAD_90_DEG)
        
        def GetCone(doc: c4d.documents.BaseDocument) -> c4d.BaseObject:
            """Creates a new cone object.
            """
            cone: c4d.BaseObject = c4d.BaseObject(c4d.Ocone)
            cone[c4d.PRIM_CONE_HEIGHT] = 40
            cone[c4d.PRIM_CONE_BRAD] = 10
            doc.InsertObject(cone)
        
            return cone
        
        def main() -> None:
            """Called by Cinema 4D when the script is being executed.
            """
            # Ensure that there is an editable spline object selected.
            if not isinstance(op, c4d.SplineObject):
                return
            
            # Create a new SplineHelp object and initialize it with the selected spline object. Then iterate
            # over five offsets (0, 0.25, 0.5, 0.75, 1) and get the matrix at each offset. A spline does not
            # have unambiguously a matrix for each offset since a normal and a tangent of a point only define
            # two out of the three degrees of freedom. The matrices which are constructed here are in respect
            # to the #upvector we pass to the SplineHelp object.
            sh: c4d.utils.SplineHelp = c4d.utils.SplineHelp()
            sh.InitSplineWithUpVector(op, upvector=c4d.Vector(0, 1, 0))
        
            for offset in (0, 0.25, 0.5, 0.75, 1):
                mg: c4d.Matrix = sh.GetMatrix(offset)
        
                # Now construct two cones on each point and rotate them 90 degrees around the X and Z axes.
                # The order in matrix multiplication is important, because other than for example the natural
                # or real numbers, matrix multiplication is not commutative. So X * Y is not always equal to
                # Y * X in matrix multiplication. In this case we want to multiply the spline point matrix
                # by our additional rotation matrices. We already operate in world coordinates, with #mg,
                # so we do not need to multiply by the matrix of the spline object itself.
                coneX: c4d.BaseObject = GetCone(doc)
                coneZ: c4d.BaseObject = GetCone(doc)
                coneX.SetMg(mg * TRANSFORM_90_X)
                coneZ.SetMg(mg * TRANSFORM_90_Z)
                coneX.SetName(coneX.GetName() + "X")
                coneZ.SetName(coneZ.GetName() + "Z")
        
            c4d.EventAdd()
        
        
        if __name__ == '__main__':
            main()
        

        MAXON SDK Specialist
        developers.maxon.net

        S 2 Replies Last reply Reply Quote 1
        • S
          SmetK @ferdinand
          last edited by

          @ferdinand I'm sorry, for some reason I didn't realize I had to put the code in right away. Thanks for your help, I will study the code! I'll let you know what happens.

          1 Reply Last reply Reply Quote 0
          • S
            SmetK @ferdinand
            last edited by

            @ferdinand How nice to know there are professionals like you out there. Thank you for your help, it is encouraging to keep going. Optimized and Works as it should!

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

              Good to hear that you found your solution!

              MAXON SDK Specialist
              developers.maxon.net

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