Reputation: 13
My current project requires that I call a particular class function whenever the timeline is scrubbed or selected (per frame, basically). I have tested the use of cmds.expression(s = 'print "hello "')
and found that, every time I moused over a different frame while scrubbing the timeline, it printed "hello "
just as I expected. However, whenever I tried to call a class function (or any other defined function for that matter), I receive errors. Here is a general synopsis of my code:
class DummyClass:
def __init__(self):
self.name = 'Dummy'
def displayName():
print self.name
d = DummyClass()
cmds.expression(s = 'd.displayName()')
I receive the error: # Error: line 0: Cannot find procedure "d.displayName". #
And this is a general synopsis of me trying to see whether or not I could call any function definition, let alone a class:
def foo():
print 'Success!'
cmds.expression(s = 'foo()')
I receive the error: # NameError: name 'foo' is not defined #
Maybe I just don't understand what cmds.expression()
truly does. Will it only work with external python files or something? I can get cmds.expression(s = 'print "hello "')
to work, but nothing else within the same script can be found, apparently. Am I taking the wrong approach? Am I missing syntax? I have spent many hours doing research on the matter and have gotten nowhere.
How do I call a defined function, from within a class or otherwise, using cmds.expression
? If that's not possible, is there a similar command that can do the trick?
Upvotes: 1
Views: 2854
Reputation: 58493
Flag s
sets the expression string. This type of expression is undoable, queryable and editable.
You should use it this way:
import maya.cmds as mc
mc.expression( s = 'pSphere1.translateX = cos(time/2)*10' )
But calling Python defined functions you can the same way as described in SO post:
How do I use Def Function strings in maya
Upvotes: 1