Solved Limit the number of executions of an object plugin.

Hello,
I'm creating an object plugin that not need to be opened more then one time. I'm searching how to limit the open of an object plugin to only one time. I had tried via GetState() but it doesn't work.

def get_all_objects(op, filter, output):
    while op:
        if filter(op):
            output.append(op)
        get_all_objects(op.GetDown(), filter, output)
        op = op.GetNext()
    return output
	
class MyObject(c4d.plugins.ObjectData):  
    def GetVirtualObjects(self, op, hierarchyhelp):
	doc = documents.GetActiveDocument()
	search = get_all_objects(doc.GetFirstObject(), lambda x: x.CheckType(MY_OBJECT_ID), [])    
		
	if len(search) :			
	     ...

	return op.GetCache(hierarchyhelp)

Thank you.

Hi @mfersaoui,

It was not clear for us when you said " need to be opened more than one time" do you mean that you should have only one Object instance in the current document? in all scene?
If that's the case you can search if there is already an object existing, and return False to the ObjectData.Init so the object will not be created.

If it's not that then please be more specific.

Finally, I would like to point you to in order to set up correctly a topic (I've done it for you this time, but please do it for the next one).

If you have any question, please let me know.
Cheers,
Maxime.

Hi @m_adam,
Yes I want to open only one Object instance in all scene.
Thank you.

Below solution :

def get_all_objects(op, filter, output):
    while op:
        if filter(op):
            output.append(op)
        get_all_objects(op.GetDown(), filter, output)
        op = op.GetNext()
    return output

def Init(self, node):
    doc = documents.GetActiveDocument()
    search = get_all_objects(doc.GetFirstObject(), lambda x: x.CheckType(MY_OBJECT_ID), [])    
    if len(search) 
        return False
    return True

def GetVirtualObjects(self, op, hierarchyhelp):
    ...

Hello,

never use GetActiveDocument() in a generic function of a NodeData based plugin ( ObjectData etc.). Only use GetActiveDocument() in reaction to some user interaction in the UI.

In certain scenarios there is no active document (e.g. Team Render Client, Command Line Renderer), so calling this function will return nothing.

To get the BaseDocument that stores a given object, use GetDocument() . But that only works after an object has been inserted into the BaseDocument .

See BaseDocument Manual.

Alternatively, you could catch the message c4d.MSG_MENUPREPARE which is sent to the object when the user creates the object from the GUI. There you can do the check and return False if the object should not be created.

See NodeData::Message() Manual and MSG PLUGINS.

best wishes,
Sebastian

@s_bach Sorry, I just saw your message. the replies notifications was disabled.
Thank You!