Ok, I got it to work. Here’s what I did.
First off, to deal with the namespace issue, I just pop up the dialog asking which namespace to use. Not the prettiest way, but it does work, and it will work no matter how many times you import the same file (which is something we will most likely do). I’m sure there are other ways to do that, but this one is straightforward and our artist knows how to respond to it.
Edit: Here’s the code that I used to ask for the namespace. Not really very interesting, but it might help someone.
print("Getting the namespace.")
namespaces = cmds.namespaceInfo(lon=True)
ns = cmds.confirmDialog(
message='The selector needs to know then namespace of the character: Choose the namespace:',
if ns == "(none)":
ns = ""
Second, to deal with running the script node on import or reference, I used
MSceneMessage.addCallback(). Here’s the code I used:
# register for restoring from a reference.
# store the reference to it so we can unregister it later if necessary. never throw away your receipts! er, references.
if Selector.referenceStartup == 0:
referenceStartup = OpenMaya.MSceneMessage.addCallback(37, "selectorMain")
print("Registering for loading references.")
# register for restoring from an import.
# store the reference to it so we can unregister it later if necessary.
if Selector.importStartup == 0:
importStartup = OpenMaya.MSceneMessage.addCallback(4, "selectorMain")
print("Registering for importing.")
The integers are the message IDs. I found that the constants given in the documentation don’t work. Use the numbers instead.
Then, in the
selectorMain() that I referenced:
ns = Selector.getNamespace()
selID = cmds.getAttr(ns + ':SelectorNode.selectorID')
mulID = cmds.getAttr(ns + ':SelectorNode.multiID')
Selector.register(selID, mulID, ns)
except RuntimeError, err:
print("The selector identifiers were not set. You will need to set them manually. Error: " + err)
And of the
Selector.register() code is nothing special.
So I hope that helps someone else that’s having the same issue. You won’t hurt my feelings if you have any corrections to make, so please do point out any problems with this technique.