Selecting Different components but in Different modes



  • Here's a number of "loaded" questions, as I have a goal in mind:
    Is it possible to do the following things via a script, when we're in Polygon Component Mode:

    1. when the script is activated, although we are still in Polygon mode, can we see Edge selection highlights when the cursor is hovering over some edge
    2. by clicking on that edge, could we set the Modeling Axis to align to the Center of the edge, and the modeling Z Axis to align to that edge?
    3. After the Click (on the edge) can we exit the script and use that Axis as the modeling Axis?
      Would those be possible?


  • 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



  • 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!


  • 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()
    


  • 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!



  • 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



  • 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



  • 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



  • 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?



  • 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



  • 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.



  • 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



  • 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()
    
    


  • 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


Log in to reply