Changing the angle of spline-aligned objects
-
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.
-
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,
FerdinandResult
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()
-
@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.
-
@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!
-
Good to hear that you found your solution!