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

    Selecting Different components but in Different modes

    Cinema 4D SDK
    4
    14
    1.9k
    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.
    • ferdinandF
      ferdinand
      last edited by ferdinand

      Hi,

      yes, that should be technically possible, but a "script" (assuming you are referring to a Script Manager script) is a rather awkward place to do this, because Script Manager scripts are blocking. What you would have to do would be:

      1. Get the active BaseDraw via c4d.document.BaseDocument.GetActiveBaseDraw
      2. Get active object of the document.
      3. Initialise a c4d.utils.ViewportSelect with 1. and 2.
      4. Contentiously poll the mouse for its screen position via c4d.gui.GetInputState
      5. Exit the loop in 4. once the user has chosen a position (via a LMB for example).
      6. Feed the result of 5. into your ViewportSelect.GetNearestEdge after converting it into viewport coordinates (the conversion part could be tricky).
      7. Unpack the edge index into its points/vertices via c4d.utils.Neighbor.
      8. Construct your modelling axis matrix in whatever fashion you want it to be constructed.
      9. Write the axis to the object defined in 2. with BaseObject.SetModelingAxis.

      But due to Script Manager scripts being blocking:

      1. The user could not interact with Cinema while your script is running.
      2. Which also means no viewport navigation and redraws.
      3. Which in turn also means no highlighting etc.

      You might get better results out of an Interaction Tag, but I have never used them, so I cannot say much about them. But with a ToolData plugin this should be fairly easy to implement.

      Cheers,
      zipit

      MAXON SDK Specialist
      developers.maxon.net

      1 Reply Last reply Reply Quote 0
      • N
        noseman
        last edited by

        thank you so much for the detailed reply, and helping me save a lot of time by not doing it that way after all 🙂
        I am trying to come up with a "shortcut" method for something I'm doing a tutorial on.
        All the functionality is already possible inside C4D, but to set it up you need to do a bunch of things and set a bunch of settings.
        The "manual" way is:

        1. Use Axis Center to align and Center the Object Axis to the Selected Edge.
        2. Set the Modeling Axis to "Object"
        3. Change Component mode to Polygons
        4. Select your Polygons
        5. Use Quantizing to Move or Rotate the said Polygons.
          All this could be possible via a script, instead of doing Matrix math 🙂
          Thank you very much again for your input!
        1 Reply Last reply Reply Quote 0
        • ferdinandF
          ferdinand
          last edited by ferdinand

          Hi,

          you lost me a bit in your workflow description, sorry. I am not really a Cinema user (anymore). But if I understand you correctly, you basically only want to to swizzle the modelling axis, which is pretty easy (the hard part of your request was all the interaction stuff). Below is an example on how you could do that.

          For your exact use case you would have to select an edge and then execute the script, the modelling axis of the selected object will then be swizzled. By executing the script multiple times you can cycle through the three possible configurations. The script is neither bound to a specific document mode nor number of selected elements.

          Cheers,
          zipit

          """Script Manager script.
          
          Swizzles the modeling axis of the selected polygon object when run. As a
          side effect the active tool is changed to the LiveSelection tool and set
          into "Free" mode. You will have to pretty up the script yourself if you
          want a more streamlined experience.
          """
          
          import c4d
          
          def swizzle_modelling_axis(doc, op):
              """Swizzles the modeling axis of the passed object.
              
              Args:
                  doc (c4d.documents.BaseDocument): The active document.
                  op (c4d.PolygonObject): A node in the active document.
              
              Raises:
                  TypeError: When op is not a PolygonObject.
              """
              def swizzle_matrix(m):
                  """Swizzles a matrix, pushing in numerical and looping order of 
                  its components (e.g. v1, v2, v3 becomes v3, v1, v2).
                  """
                  return c4d.Matrix(off=m.off, v1=m.v3, v2=m.v1, v3=m.v2)
          
              # Sort out non-polygon-objects.
              if not isinstance(op, c4d.PolygonObject):
                  msg = ("swizzle_modelling_axis() requires a polygon object. "
                        "Received: {type}.")
                  raise TypeError(msg.format(type=type(op)))
          
              # We need the axis to be free in order to be able to modify the axis.
              # For that we select the live selection tool and set it to free mode.
              doc.SetAction(c4d.ID_MODELING_LIVESELECTION)
              plugin = c4d.plugins.FindPlugin(c4d.ID_MODELING_LIVESELECTION, 
                                              c4d.PLUGINTYPE_TOOL)
              if plugin is None:
                  return
              else:
                  plugin[c4d.MDATA_AXIS_MODE] = c4d.MDATA_AXIS_MODE_FREE
          
              # Get the modeling axis, swizzle it and write it back.
              axis = swizzle_matrix(op.GetModelingAxis(doc))
              op.SetModelingAxis(axis)
              op.Message(c4d.MSG_UPDATE)
          
          def main():
              """Entry point.
              """
              swizzle_modelling_axis(doc, op)
              c4d.EventAdd()
          
          if __name__=='__main__':
              main()
          

          MAXON SDK Specialist
          developers.maxon.net

          1 Reply Last reply Reply Quote 0
          • N
            noseman
            last edited by

            Thank you very much.
            The "swizzling" can be achieved with the Axis Center:
            177b4293-85c1-4e3d-8029-6265bec36b85-image.png
            using these settings.
            BUT the code you posted is very valuable for me to evolve my spaghetti coding... copy from here, and paste there 🙂

            Thanks Again!

            1 Reply Last reply Reply Quote 0
            • N
              noseman
              last edited by

              Full disclosure, I just realized that in "Axis Modification" Tool, when in Polygon mode, if you hover over an edge and click, it places the Axis there...
              I guess most of what I was trying to do is already there...

              "The path to wisdom, is paved with errors..."
              [just made that up...] Noseman

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

                Hi,

                I was not aware that Cinema can do this and I thought that was what you were going for, given your statement '[..] and the modeling Z Axis to align to that edge [...]'. Also: Swizzling is just computer graphics slang for creating a new vector or matrix by shuffling the components of an existing vector or matrix.

                Since I do not really understand what you are trying to do, I cannot really point you into the right direction. I can only point out that the 'matrix maths' that comes up with transforms in Computer Graphics is not all that scary. I made post here and recently here on the topic. Feel free to come back if you have any problems.

                Cheers,
                zipit

                MAXON SDK Specialist
                developers.maxon.net

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

                  hi,

                  @noseman said in Selecting Different components but in Different modes:

                  Full disclosure, I just realized that in "Axis Modification" Tool, when in Polygon mode, if you hover over an edge and click, it places the Axis there...
                  I guess most of what I was trying to do is already there...

                  yes, the only thing that doesn't answer your question it that you want the Z axis to be align with the edge.
                  The "L" shortcut come really useful if you keep that shortcut pressed while clicking on the polygon/edge/point you want to move to your axis. Than when you release the shortcut, is disable that command.

                  Don't be afraid of math, it's like cos, sin, tan, for some artist out there, it's really useful and not so complicated. (sometimes it's just function that you need to apply, not to understand like going from local to global and global to local)

                  Cheers,
                  Manuel

                  MAXON SDK Specialist

                  MAXON Registered Developer

                  1 Reply Last reply Reply Quote 0
                  • X
                    x_nerve
                    last edited by x_nerve

                    Hi:

                    This is a very old post, but it's very helpful.

                    I have found the algorithm for the Z axis to be align with the edge, but the sample script from the post does not restore the modeling axis. Can you provide a method to restore the original default modeling axis?

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

                      hi,

                      To "save" the current modeling axis matrix, as you are (i think) using a script, you need to save the matrix inside the document's BaseContainer
                      Of course you have to retrieve your own unique ID (the icon on the top of this page)

                      import c4d
                      from c4d import gui
                      # this ID have to be unique
                      myownID = 123456
                      
                      # Main function
                      def main():
                          if op is None:
                              return
                          if doc is None:
                              raise ValueError("doc isn't initialized !!!")
                          # retrieves the modeling axis
                          mmt = op.GetModelingAxis(doc)
                          # saves the modeling axis inside the document's BaseContainer'
                          doc[myownID] = mmt
                          # swaps the axis
                          temp = mmt.v1
                          mmt.v1 = mmt.v3
                          mmt.v3 = temp * -1.0
                          # Sets the modeling axis
                          op.SetModelingAxis(mmt)
                          # Pushes an update event to Cinema 4D
                          c4d.EventAdd()
                          
                          
                          
                      
                      # Execute main()
                      if __name__=='__main__':
                          main()
                      

                      to restore the previous modeling axis's position:

                      import c4d
                      from c4d import gui
                      # Welcome to the world of Python
                      myownID = 123456
                      
                      # Main function
                      def main():
                          if op is None:
                              return
                          if doc is None:
                              raise ValueError("doc isn't initialized !!!")
                          #retrieves the stored matrix inside the document's BaseContainer
                          mmt = doc[myownID]
                          # sets the current modeling axis matrix
                          op.SetModelingAxis(mmt)
                          
                          # Pushes an update event to Cinema 4D
                          c4d.EventAdd()
                      
                      # Execute main()
                      if __name__=='__main__':
                          main()
                      

                      But I'm not sure if it's this position you want to restore.

                      Cheers,
                      Manuel

                      MAXON SDK Specialist

                      MAXON Registered Developer

                      1 Reply Last reply Reply Quote 0
                      • X
                        x_nerve
                        last edited by

                        Hi:

                        Unfortunately, once the modeling axis is set, there is no way to recover.If I click on any point, the coordinates are the same.Even if you create a new polygon object and click on any point, the coordinates are the same.The coordinates of the entire file will not be refreshed unless a new document is created, but the script will still be able to retrieve the coordinate data normally。

                        874839.jpg
                        Coordinates are no longer refreshed and are consistent.

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

                          hi,

                          Do you mean with the code i provided ?
                          Do you have some step procedure / code to reproduce this behavior ?
                          Witch version of Cinema4D are you using ?

                          Cheers,
                          Manuel

                          MAXON SDK Specialist

                          MAXON Registered Developer

                          1 Reply Last reply Reply Quote 0
                          • X
                            x_nerve
                            last edited by

                            HI:

                            The modeling axis cannot be restored to its default state using the following sample code.Create a new cube object, collapse, and select any point.The world coordinates of points are all 0.

                            """Script Manager script.
                            
                            Swizzles the modeling axis of the selected polygon object when run. As a
                            side effect the active tool is changed to the LiveSelection tool and set
                            into "Free" mode. You will have to pretty up the script yourself if you
                            want a more streamlined experience.
                            """
                            
                            import c4d
                            
                            def swizzle_modelling_axis(doc, op):
                                """Swizzles the modeling axis of the passed object.
                                
                                Args:
                                    doc (c4d.documents.BaseDocument): The active document.
                                    op (c4d.PolygonObject): A node in the active document.
                                
                                Raises:
                                    TypeError: When op is not a PolygonObject.
                                """
                                def swizzle_matrix(m):
                                    """Swizzles a matrix, pushing in numerical and looping order of 
                                    its components (e.g. v1, v2, v3 becomes v3, v1, v2).
                                    """
                                    return c4d.Matrix(off=m.off, v1=m.v3, v2=m.v1, v3=m.v2)
                            
                                # Sort out non-polygon-objects.
                                if not isinstance(op, c4d.PolygonObject):
                                    msg = ("swizzle_modelling_axis() requires a polygon object. "
                                          "Received: {type}.")
                                    raise TypeError(msg.format(type=type(op)))
                            
                                # We need the axis to be free in order to be able to modify the axis.
                                # For that we select the live selection tool and set it to free mode.
                                doc.SetAction(c4d.ID_MODELING_LIVESELECTION)
                                plugin = c4d.plugins.FindPlugin(c4d.ID_MODELING_LIVESELECTION, 
                                                                c4d.PLUGINTYPE_TOOL)
                                if plugin is None:
                                    return
                                else:
                                    plugin[c4d.MDATA_AXIS_MODE] = c4d.MDATA_AXIS_MODE_FREE
                            
                                # Get the modeling axis, swizzle it and write it back.
                                axis = swizzle_matrix(op.GetModelingAxis(doc))
                                op.SetModelingAxis(axis)
                                op.Message(c4d.MSG_UPDATE)
                            
                            def main():
                                """Entry point.
                                """
                                swizzle_modelling_axis(doc, op)
                                c4d.EventAdd()
                            
                            if __name__=='__main__':
                                main()
                            
                            
                            1 Reply Last reply Reply Quote 0
                            • ManuelM
                              Manuel
                              last edited by Manuel

                              hi,
                              I can't reproduce that, what version are you using ?

                              the only moment where a coordinates could be 0 is if the point and the object's pivot are sharing the same coordinates AND you have the "Enable Axis" enable.
                              (or you are in object's rel or abs coordinate and the point is at the same position of the pivot's object.
                              As if you were at the same time in object's mode and in point mode. (moving the object's pivot point and the modeling axis at the same time)

                              So this look like a bug.

                              by the way, what do you call the "default state" ? The last modeling axis position ? the position when you selected the polygon ?

                              Cheers,
                              Manuel

                              MAXON SDK Specialist

                              MAXON Registered Developer

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