Listening for new objects, tags, tracks, etc

On 29/03/2013 at 20:29, xxxxxxxx wrote:

What's the best way to execute a block of code whenever a new object, tag, track, or marker has been created somewhere in the current document?

I'm currently using a MessageData plugin and listening for c4d.EVMSG_CHANGE - but this gives a lot of false positives and counting all objects/tags every time this message arrives seems wasteful. Is there a more specific message that I can be listening for?
Thanks,

Donovan

On 04/04/2013 at 13:47, xxxxxxxx wrote:

Okay, I'm not sure if this is the best method, but I listen for c4d.EVMSG_CHANGE and then I check to see whether a new object, or material has been selected by comparing the active object to a cache from the last time the EVMSG_CHANGE message was received. Because the code I'm running is rather light, I'm okay with the number of false-positives this will receive:

  
class MyMessageData(c4d.plugins.MessageData) :   
    def __init__(self) :   
        #Caches to help test whether there are new objects or materials   
        self.active_object_cache = None   
        self.active_mat_cache = None   
        pass   
  
    def GetTimer(self) :   
        return 0 #Don't update on a timer   
  
    def CoreMessage(self, id, bc) :   
        #Listen for a change to the document   
        if id == c4d.EVMSG_CHANGE:   
            doc = c4d.documents.GetActiveDocument()   
            active_object = doc.GetActiveObject()   
            active_mat = doc.GetActiveMaterial()   
               
            #Check to see whether a new object or material has been selected   
            #This is a hack way of checking for new objects in the scene   
            #NOTE: This will result in some false positives every time   
            #selection changes but no object has been created   
            diff_obj = active_object != self.active_object_cache   
            diff_mat = active_mat != self.active_mat_cache   
            if diff_obj or diff_mat:   
               #There's a new selection   
               self.active_object_cache = active_object   
               self.active_mat_cache = active_mat   
                 
               #EXECUTING CODE GOES HERE   
        return True   

On 04/04/2013 at 14:14, xxxxxxxx wrote:

Isn't EVMSG_CHANGE also sent when the selection changes? Didn't check it, though.
I unfortunately do not know a better way to check. I could really need this as well.

-Niklas

On 04/04/2013 at 15:29, xxxxxxxx wrote:

Yes, it also updates with selection changes. I'm tempted to store an object count cache as well and compare that when the selection changes, but I'm not sure if that will be any faster.