Python in expression editor...

Python in expression editor...

Anonymous
Not applicable
8,510 Views
4 Replies
Message 1 of 5

Python in expression editor...

Anonymous
Not applicable

hi, it seems that Expression editor does not support python. Is there any way to execute external python script via expression editor?

0 Likes
Accepted solutions (1)
8,511 Views
4 Replies
Replies (4)
Message 2 of 5

s_ehrenhaus
Collaborator
Collaborator
Accepted solution

yep ofcourse,

you have to define your python code in a python tab and obviously source it before running the expression.

Then you can call it inside an expression editor using the python() command.

 

Note that its kinda tricky to pass full variables from mel to python and vice versa, so if you need to process an array of objects try to make your python function work either hardcoded on selection, or have it work on a single object, then run a loop through your array inside the mel expression.

 

any questions?

 

-S

 

 

PS: I still would always try not to use expressions as they make the scene quite slow compared to other nodes such as mayas math utilities.

 

Be Happy 🙂
Message 3 of 5

Anonymous
Not applicable

thanks for your answer.

it works good except that it's very slow.

So I am looking for faster way to execute piece of code when playback starts. Is there any way to have a kind of listener when "play" button is pressed for example? I think about deeper piece of code using C++ and python API for example. Is this something possible ?

0 Likes
Message 4 of 5

s_ehrenhaus
Collaborator
Collaborator

of course, whatever you want to do, you can always compile in into a dedicated c++ node that preforms much faster than a maya expression.

Be Happy 🙂
0 Likes
Message 5 of 5

Anonymous
Not applicable

ok I created 2 buttons that allow to play and stop playback- which works fine.

it becomes more tricky when I try to execute code at each frame when playback is running. Here is my code...I got an infinite loop...and don't really know how to solve it.

 

 

import pymel.core as pm

state = False

def drawUI(): #Function that will draw the entire window
    
    #check to see if window exists
    if pm.window("UI_MainWindow", exists = True):
        pm.deleteUI("UI_MainWindow")
    
    #create actual window
    pm.window("UI_MainWindow", title = "test playback...", w = 150, h = 150, mnb = False, mxb = False, sizeable = True)
    pm.columnLayout("UI_MainLayout", w = 150, h =300)

    pm.button("UI_pbStartButton", label = "Start", w = 150, command=pbStart)
    pm.button("UI_pbStopButton", label = "Stop", w = 150, command=pbStop)  
    pm.showWindow("UI_MainWindow") 

def pbStart(*args):
    pm.play(state=True)
    state = True
    while state:
        doSomethingAtFrame()
            
def pbStop(*args):
    pm.play(state=False)
    state = False
        
def doSomethingAtFrame():
    print pm.currentTime()  
                   
def main():
    drawUI()


main()

 

0 Likes