Importing module issue


#1

Hi all,

As a test I’ve created this python module. The ‘print_variable’ function inside it simply takes a number as an argument, and prints it out using ‘mel.eval()’:

# test module
import maya.mel as mel

def print_variable(int_var):
    global py_var
    py_var = int_var
    mel.eval('$mel_var=python("py_var")')
    mel.eval('print $mel_var')
    
    

I want to run this test script and have it print out 34.

# test script
from test_module import *

def call_print_variable():
    print_variable(34)
    
call_print_variable()

However, I find to begin with it comes up with an error saying that ‘name ‘py_var’ is not defined’.

If however I select the script in the script editor and press enter for a second time, it runs fine.
Please can anyone tell me how I can get it to run first time?

Thanks.


#2

Avoid gloabls :wink:

def print_variable(int_var):
	mel.eval('$mel_var={0}'.format(int_var))
	mel.eval('print $mel_var')

#3

Great thanks! It’s good to know how to pass a variable value from python to mel properly. :keenly: