I'm working in Maya using Python 2.5, writing a dynamic hotkey manager class and ran into trouble trying to assign commands that are instance specific since nameCommands get represented as strings in mel. I end up with commands that look like:
<bla.Bla instance at 0x0000000028F04388>.thing = 'Dude'
I've seen quite a bit on the topic of repr and eval but my test example below fails.
class Foo:
pass
f = Foo()
f.thing = 'Jeff'
rep = repr(f)
y = eval(rep) # errors here
name = y.thing
# Error: invalid syntax
# Traceback (most recent call last):
# File "<maya console>", line 7, in <module>
# File "<string>", line 1
# <__main__.Foo instance at 0x00000000294D8188>
# ^
# SyntaxError: invalid syntax #
I assume what I want is some way to get a proper object of that instance from a string. I could format the string to an evaluate-able command if I knew what that looked like. see my cgtalk post for more usage info
SO related topics:
This one says it's not possible but users also wanted a use case, which I hope I've provided. How to obtain an object from a string?
Others: When is the output of repr useful? How to print a class or objects of class using print()? Allowing the repr() of my class's instances to be parsed by eval() Main purpose of __repr__ in python how do you obtain the address of an instance after overriding the __str__ method in python Python repr for classes how to use 'pickle'