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

    How to move multiple objects axis to origin?

    Cinema 4D SDK
    python
    4
    4
    1.1k
    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.
    • W
      wilsonic
      last edited by

      Hello, I am a python novice, trying to find a way to move the axis of hundreds objects to the origin. I found a great script by Arttu Rautio https://aturtur.com/ar_scripts-for-cinema-4d/, specifically this one https://github.com/aturtur/cinema4d-scripts/blob/master/AR_Scripts_1.0.15/AR_AxisToCenter.py which will move the axis on many objects to their center and am wondering if it's possible to modify this to move the axis to the origin instead.

      This fuction appears to be doing the axis shifting in that script.

      def CenterAxis(obj): # Center object's axis
          doc = c4d.documents.GetActiveDocument() # Get active Cinema 4D document
          points = [] # Initialize empty list
          pointCount = obj.GetPointCount() # Get object's point count
          for i in range(0, pointCount): # Loop through points
              points.append(obj.GetPoint(i)) # Add point to points list
          matrix = obj.GetMg() # Get object's global matrix
          center = obj.GetMp() # Get Object's bounding box center in local space
          axis = obj.GetAbsPos() # Get object's absolute position
          difference = axis - (axis + center) # Calculate difference
          if difference != c4d.Vector(0): # If there is a difference
              for i in range(pointCount): # Loop through object's points
                  obj.SetPoint(i, points[i] + difference) # Set new point position
              obj.Message(c4d.MSG_UPDATE) # Send update message
              obj.SetMg(c4d.Matrix((matrix * center),
                  matrix.v1, matrix.v2, matrix.v3)) # Set new matrix for the object
      

      I'm wondering if it's as simple as setting the center matrix to a fixed point (0,0,0) but I'm not sure how to format those coordinates as a matrix. (or if it's even this small a change or would need a larger re-write)

      I've also tried using the script log while performing the action manually but the cooridinate manager does not produce log entries that I can see unfortunately.

      Does anyone have any insight on how this might be achieved?

      Thanks

      1 Reply Last reply Reply Quote 0
      • ManuelM
        Manuel
        last edited by Manuel

        Hi,

        You can't only move the axis, for that you need to move the points also (Visually nothing changes).
        The problem can be harder that you think because of hierarchies.
        You also need to check if the object's type is Opolygon. For that, you can use IsInstanceOf

        this question has been asked many times now:
        the first one will probably help you a lot, there is a "transfert axis" function.
        https://developers.maxon.net/forum/topic/13004/cad-normal-tag-flipped-after-polyon-merge-join-and-axis-repositioning-how-realign/13

        https://developers.maxon.net/forum/topic/11788/move-object-axis-problem-in-python/2
        https://developers.maxon.net/forum/topic/13142/how-to-move-axis-to-desired-matrix-without-affecting-object-in-python

        I also encourage you to read our matrix manual that will help you to understand how it works.

        Cheers,
        Manuel

        MAXON SDK Specialist

        MAXON Registered Developer

        1 Reply Last reply Reply Quote 0
        • ThomasBT
          ThomasB
          last edited by ThomasB

          Yes simply use a direction vector to move the points back

          Here is a script where the Z-Axis of all selected obects is set to Zero:

          import c4d
          
          
          # Main function
          def main():
              doc.StartUndo
              objs=doc.GetActiveObjects(0)
              points_list=[]
              pos_list=[]
          
              #==Append Position of every selected Object into a list ============
              for a in objs:
                  pos_list.append(a.GetAbsPos())
          
              counter=0
              #===move every Object in Z to Zero and a inner for-Loop to move every point of the object back with a direction vector
          
              for i in objs:
          
                  i.SetAbsPos(c4d.Vector(i.GetAbsPos().x,i.GetAbsPos().y,0))
                  doc.AddUndo(c4d.UNDOTYPE_CHANGE, i)
                  richtung=i.GetAbsPos()-pos_list[counter]
                  single_points=[]
          
                  for p in i.GetAllPoints():
                      single_points.append(p-richtung)
          
                  i.SetAllPoints(single_points)
          
          
                  counter+=1
          
              doc.EndUndo()
              c4d.EventAdd()
          
          
          
          # Execute main()
          if __name__=='__main__':
              main()
          

          luckily it worked for me 🙂

          Thanks,
          T.S.B

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

            Hello @wilsonic,

            without any further questions we will consider this topic as solved by Friday, December the 17th.

            Thank you for your understanding,
            Ferdinand

            MAXON SDK Specialist
            developers.maxon.net

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