This sounds like a dev problem, if I understand your question correctly:
Users will not experience this issue, only you, as you develop the code. Is
that correct? If so, I would write a small bit of code designed to handle
the specifics of your situation. You could essentially remove the callback
with this code, then do your imports. There may be more that you want to do
here, but I think you get the idea.
An alternative to the above option, (and I can't say I recommend this,
unless you have a good understanding of how Python reference counting and
imports work together) is to pass a wrapper function in as the callback.
Within that function you make a call to the fully qualified canonical name
of the actual worker function. This worker function should reside in a
separate module so that it can be updated as you need.
ex:
def callback_wrapper(*args, **kwargs):
my_reloadable_module.my_worker_function(*args, **kwargs)
Referring to that worker function in this manner means that each time the
wrapper is called, a lookup for your worker function is done. Since the
module has been reloaded, this lookup will resolve to the new function
within the newly reloaded module, even though it's the same name as the old
function. If you don't use the canonical name, you create a direct
reference to the function that will point to the function in the old module
once you do a reload - the wrong one.
Check out effbot's page about imports if you want more info. There is a
post on the page near the bottom that covers some of this material:
http://effbot.org/zone/import-confusion.htm
-Judah
On Fri, Apr 5, 2013 at 6:04 AM, Sune <[email protected]> wrote:
> On Thursday, April 4, 2013 11:16:24 PM UTC+2, Judah wrote:
> > Ok, if that's going to work for your use case that's terrific.
> >
> >
> > I notice you are fixing up file reference paths. You may want to look
> into kBeforeReferenceCheck. It's a great entry point for handling all
> reference path manipulation. I've used this, for instance, to prefix all
> references with a common %ENVIRONMENT_VAR%. It's great because you only
> need to do this in one place, whether you are handling user input or file
> loading, this callback will be triggered.
> >
> >
> >
> > -Judah
> >
> >
> >
> >
> >
> >
> > On Thu, Apr 4, 2013 at 9:56 AM, Sune wrote:
> >
> >
> >
> >
> > On Thursday, April 4, 2013 4:19:56 PM UTC+2, Judah wrote:
> >
> > > Yes, that's right. You could use a dictionary or an array of tuples,
> or simply a module or member variable, depending on what makes most sense
> for your situation. This way you would be able to look up your callback
> later. Store the return from the callback creation in a manner that allows
> you to associate it with your call.
> >
> >
> >
> > >
> >
> > >
> >
> > > If the callback is used by a particular module or package then I would
> add a module or static class level var that initializes to None. Once your
> Maya startup runs, you store this callback ID on the var. Then you can
> check to see if that var is None later on. If its not None, you know it's
> been initialized.
> >
> >
> >
> > >
> >
> > >
> >
> > >
> >
> > > On Thursday, April 4, 2013, Sune wrote:
> >
> > >
> >
> > > On Wednesday, April 3, 2013 10:36:19 PM UTC+2, Sune wrote:
> >
> > >
> >
> > > > I'm adding and removing callbacks with MSceneMessage from a python
> script. I'd like to somehow query if these callbacks exist already, before
> adding them. I searched for the answer, but came up blank.
> >
> >
> >
> > >
> >
> > > >
> >
> > >
> >
> > > > I guess I could define these in a python plugin, then I would only
> ever have one instance, but that seems like a workaround.
> >
> > >
> >
> > > >
> >
> > >
> >
> > > > Any help would be great!
> >
> > >
> >
> > > >
> >
> > >
> >
> > > > Sune
> >
> > >
> >
> > >
> >
> > >
> >
> > > I would be likely just be adding these on start up. When adding
> callbacks all I seem to get back is their id and from their ID I would not
> know what they are, unless I keep track of that myself, that's I think is
> what you are suggesting right?
> >
> >
> >
> > >
> >
> > >
> >
> > > Thanks :-)
> >
> > >
> >
> > >
> >
> > >
> >
> > > --
> >
> > >
> >
> > > You received this message because you are subscribed to the Google
> Groups "Python Programming for Autodesk Maya" group.
> >
> > >
> >
> > > To unsubscribe from this group and stop receiving emails from it, send
> an email to [email protected].
> >
> >
> > >
> >
> > >
> >
> > > To post to this group, send email to [email protected].
> >
> > >
> >
> > > For more options, visit https://groups.google.com/groups/opt_out.
> >
> >
> >
> > Awesome, thank you! Looks like I'm making a class for each callback,
> that has a add() and remove(). I make the class a singleton, this way I
> don't have to check if it already exists:
> >
> >
> >
> > # Untested Code!
> >
> > userCallbacks = []
> >
> >
> >
> > class Singleton(type):
> >
> > _instances = {}
> >
> > def __call__(cls, *args, **kwargs):
> >
> > if cls not in cls._instances:
> >
> > cls._instances[cls] = super(Singleton, cls).__call__(*args,
> **kwargs)
> >
> > return cls._instances[cls]
> >
> >
> >
> > class Save_Cleanup_Callback(Callback):
> >
> > __metaclass__ = Singleton
> >
> > def __init__(self):
> >
> > self.idList = []
> >
> >
> >
> > def add_callback(self):
> >
> >
> self.idList.append(OpenMaya.MSceneMessage.addCallback(OpenMaya.MSceneMessage.kAfterSave,
> repath.make_file_references_relative, repath.get_last_save("ma")))
> >
> >
> self.idList.append(OpenMaya.MSceneMessage.addCallback(OpenMaya.MSceneMessage.kAfterExport,
> repath.make_file_references_relative, repath.get_last_save("ma")))
> >
> > userCallbacks.append(self)
> >
> >
> >
> > def remove_callback(self):
> >
> > for _id in self.idList:
> >
> > OpenMaya.MMessage.removeCallback(_id)
> >
> > userCallbacks.remove(self)
> >
> >
> >
> > Might be overkill, but seemed like the easiest path (especially with
> stealing the singleton code).
> >
> >
> >
> >
> >
> > --
> >
> > You received this message because you are subscribed to the Google
> Groups "Python Programming for Autodesk Maya" group.
> >
> > To unsubscribe from this group and stop receiving emails from it, send
> an email to [email protected].
> >
> >
> > To post to this group, send email to [email protected].
> >
> > For more options, visit https://groups.google.com/groups/opt_out.
>
> Thanks for that! Post save I'm removing any occurrence of the current Maya
> project path in the file on disc. The benefit of doing it this way, is that
> it works for all types of external file references in one go (textures,
> audio, references and so on) and it's really fast.
>
> I am having an issue with scope and the module that holds the
> userCallbacks list existing more than once though, which blows. For
> instance importing it during start up and then importing it from the script
> editor, makes two separate instances. Any hint's on how to avoid that, if
> possible?
>
> Btw. I modified the code a bit:
>
> import maya.OpenMaya as OpenMaya
> import repath
>
> userCallbacks = []
>
> class _Singleton(type):
> _instances = {}
> def __call__(cls, *args, **kwargs):
> if cls not in cls._instances:
> cls._instances[cls] = super(_Singleton, cls).__call__(*args,
> **kwargs)
> return cls._instances[cls]
>
> class Callback:
> __metaclass__ = _Singleton
>
> def __init__(self):
> self.idList = []
>
> def add(self):
> try:
> if self not in userCallbacks:
> userCallbacks.append(self)
> except:
> pass
>
> def remove(self):
> for _id in self.idList:
> OpenMaya.MMessage.removeCallback(_id)
> try:
> userCallbacks.remove(self)
> except:
> pass
>
> class Save_Relative_Callback(Callback):
> def __init__(self):
> Callback.__init__(self)
> self.create_callbacks()
>
> def create_callbacks(self):
> try:
>
> self.idList.append(OpenMaya.MSceneMessage.addCallback(OpenMaya.MSceneMessage.kAfterSave,
> repath.make_file_references_relative))
>
> self.idList.append(OpenMaya.MSceneMessage.addCallback(OpenMaya.MSceneMessage.kAfterExport,
> repath.make_file_references_relative))
> except:
> pass
>
> def add(callback):
> callback.add()
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.