1

A light in Maya has an attribute Use Color Temperature, which is a checkbox, when I toggle it, a function is called inside Maya to actually do the work behind the scene. Unfortunately the function address <function callback at 0x0000000051CD7DD8> is printed out instead of it's name. I don't know which function is executed when clicking the checkbox. Is there any way of converting this address to python function object or can I print the actual name of the function using this memory address?

qurban
  • 3,885
  • 24
  • 36

2 Answers2

3

This means a function named callback is called.

I'd look for def callback(...): in the corresponding code file and see what it does.

To re-produce your output all that's needed is a function with the name callback:

>>> def callback():
...     print "My name is Reut Sharabani"
... 
>>> cb = callback
>>> cb
<function callback at 0x7f182f3ca6e0>

If you want to programmatically get a function's name:

>>> cb.__name__
'callback'

If you want to know more about it you can even disassemble it using dis and it's code object:

>>> import dis
>>> dis.dis(cb.__code__)
  2           0 LOAD_CONST               1 ('My name is Reut Sharabani')
              3 PRINT_ITEM          
              4 PRINT_NEWLINE       
              5 LOAD_CONST               0 (None)
              8 RETURN_VALUE      
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
3

This seems like pure evil and to be honest, I think you can/should use callback instead. But for reference, apparently you can use the garbage collection module gc to get the object from that id.

Found on this SO response (you really can find anything on SO...):

import gc

def objects_by_id(id_):
    for obj in gc.get_objects():
        if id(obj) == id_:
            return obj
    raise Exception("No found")

I believe you need to coerce that number into an int representation because id(obj) returns an int rather than hex.

NB this may have some weird race conditions with object ids potentially being recycled after an object is deleted.

Community
  • 1
  • 1
zehnpaard
  • 6,003
  • 2
  • 25
  • 40