Python Tag: Detecting Edge Selection Changes
-
Can Python tag detect when an object's edge selection is being added or modified?
So far, this is the only thing I've discovered: The detection only responds when a component is added or deleted.
import c4d def main(): pass def message(mid, data): if mid == c4d.MSG_POLYGONS_CHANGED: print(f"somthing is happening: {mid}") return True
-
Hey @ymoon addition and deletion are indeed reported with the MSG_POLYGON_CHANGED but this can be used also for other events like modification of the topology itself so it's not 100% reliable that you will have only edge selection information.
The best way for you will be to store and detect the change yourself based on the dirty count of the BaseSelect the tag is storing. Find bellow a code example that you can copy-paste in a Python Tag that will report you modifications to all edge selections and also the "active one"import c4d # Dictionary to store tags and their dirty states tagsHash = {} # Variable to track the dirty state of active edge selection activeEdgeSelectionDirty = 0 def GetEdgeSelectionDirty(tag): """ Retrieves the dirty state of an edge selection tag. Parameters: tag (c4d.SelectionTag): The selection tag to check. Returns: int: The dirty state of the selection tag. Raises: TypeError: If the provided tag is not a selection tag. """ if not isinstance(tag, c4d.SelectionTag): raise TypeError("Expected a selection tag") # Get the BaseSelect object from the selection tag bs = tag.GetBaseSelect() # Return the dirty state of the BaseSelect return bs.GetDirty() def main(): """ Main function to track and print changes in edge selection tags. This function checks all edge selection tags of the current object, compares their dirty states with stored values, and updates the state if there are changes. It also handles removed tags and prints changes in the active edge selection. """ # List of tags that we did not visited in the current execution # We will use this list to determine which tags have been deleted. notVisitedTags = list(tagsHash.keys()) # Get the object associated with the current operation obj = op.GetObject() # Iterate through all tags of the object for tag in obj.GetTags(): if not tag.CheckType(c4d.Tedgeselection): continue tagHash = hash(tag) # Get a unique identifier for the tag if tagHash not in tagsHash: # If the tag is new, store its dirty state tagsHash[tagHash] = GetEdgeSelectionDirty(tag) print(f"New Edge Selection Tag: {tag.GetName()}") else: # If the tag has been seen before, remove it from not visited list notVisitedTags.remove(tagHash) # Check if the dirty state has changed newDirty = GetEdgeSelectionDirty(tag) if tagsHash[tagHash] != newDirty: tagsHash[tagHash] = newDirty print(f"Edge Selection Changed: {tag.GetName()}") # Process tags that are not visited anymore for removedTagHash in notVisitedTags: print(f"Edge Selection with the hash: {removedTagHash} removed") global activeEdgeSelectionDirty # Check if the active edge selection dirty state has changed newActiveEdgeSelectionDirty = obj.GetEdgeS().GetDirty() if activeEdgeSelectionDirty != newActiveEdgeSelectionDirty: activeEdgeSelectionDirty = newActiveEdgeSelectionDirty print("Active Edge selection changed")
Cheers,
Maxime. -
Thanks, I'll test Dirty on selection.