Python printing????


#1

Hi all,

I’ve just started to learn Python in maya and have hit a small problem, for most of my mel scripts I nearly always get to a point where I need to print a message to the user, normally into the maya status line (the box next to the command line) So for example if someone tries using a tool I’ve made but they haven’t selected anything I print “Nothing Selected” and this appears in the status line.

My problem is that when using the print command in Python the only output you get is in the script editor history window, which is not very helpful for the artist who doesn’t know why the tool isn’t working! So does anyone here know how this is achieved? I tried running the print command as mel like this: "mc.print(“Something is wrong…”) however this just errors out, any ideas??

Many thanks, James.


#2

Try something like that (shows a window in maya):


 dWin = mc.window(title="myWindow", wh = "640,480")
 mc.showWindow(dWin)

I think the code is self explainatory, if not feel free to ask.


#3

or:

import sys
sys.stdout.write('hello
')

problem is mayas stupid method to add
and \r at the
end of the string so it skips too much


#4

Thanks for your help guys,

kojala’s code does exactely what I need, my only issue with this is that it seems rather long winded when compared to mel, I mean typing:

print “blah blah blah”;

couldn’t be more straight forward, however in Python it’s going to look like this:

import sys
sys.stdout.write(“blah blah blah”)

I know you would only import the sys module once, probably at the start of the script, but importing an entire module just to print a one line piece of text seems crazy??

Is this the only was of doing it?


#5
mc.confirmDialog( t = "msg", message = "Splash me with an Error message!!!", button = ["OK"])

Ok the above was more of a joke :slight_smile:

Well Python in Maya right now is… ok we all know how it’s implemented.
Try PyMel - it’s the thing that is missing in Maya.


#6

or you can make your own print def
create a file named utilities.py
and paste this to there

import sys

def mPrint(message):
sys.stdout.write(message + ’
')

then when its been needed
import blaa.blaa.utilities as u

and then
u.mPrint(‘hello’)

and you have to do the import thing only once in the beginning in the file needed


#7

Yeah I’ve just stumbled across PyMel, looks interesting, I think for now though I’ll stick to just learning the basics of Python until I can understand properly why PyMel offers an advantage & go from there.

kojala, I like your idea of creating your own print def, so just to be sure, if I were to make my own def would work like this:

  1. Save a file out called “myPrintDef.py” containing:

      import sys
    
      def myPrint(message):
      sys.stdout.write(message + '
    

')

  1. Then import the def at the start of your script, so:

      import myPrintDef as u
    
  2. u.MyPrint(‘hello’)

The section I dont understand is where you wrote “import blaa.blaa.utilities as u”

I can see “utilities” is the name of the file but what should the “blaa.blaa.” bit of been?

Also where would you normally save the .py file to? Is there a default folder that maya looks in when importing scripts i.e like the scripts folder for mel.

Thanks again for your help, great stuff :slight_smile:


#8

in your Maya.env file you should have line:

PYTHONPATH = C:/Python

and put to that path your python folder path. Im not quite sure how python works
but ive managed it to work like this:

then in your python folder and every other folder inside python folder you should have a file named:
init.py

and you can leave that file empty!! …weird isnt it :slight_smile:

and then when importing something from your python folder, like you have my_stuff.py
directly inside python folder then you can do it like this:
import my_stuff as m (as an example “m”, you can use there anything you like)

or if you have under python folder another folder named modelling and it has file my_stuff.py inside it and the empty init.py file then do it like this:
import modelling.my_stuff as m

so you use “.” as separating hierarchies.
if somebody has a better solution please let me know :slight_smile:


#9

You can simply do:

print ‘Please Select Something’,

Just By Adding that comma at the end, it will print to the command line window


#10

whow… thanks a lot.
:slight_smile:


#11

Brilliant, well you couldn’t ask for easier way of doing it :slight_smile: Thanks!

Out of interest, does anyone know why this works and what the comma is actually doing?


#12

Am I missing something but I dont think this print with comma thing is working as it should.
heres a test:

you enter this:
print ‘test’,

you get:
print ‘test’,
test

then you enter this:
print ‘test2’,

you get:
testprint ‘test2’,
test2

It shouldnt do this.


#13

hey,

No that is perfectly normal, it will do the same if you were to write it in mel, I think the issue is that the script editors history panel is a bit messy and simply doesn’t seperate out the new command from the previous result of the print statement.

However if your reading the output from the status line (at the bottom of the main maya window) the print output displays as you would expect.

Edit: Actually thinking about it in mel you could write:

print “Hello\r”;

the “\r” would then mean there would be a carridge return(new line) under the printed statement, so then when you print you next command they’ll be seperated out nicely and not just stuck together. If you can do something similar in Python then that could work?


#14

Ive done always in mel like this: print "hello
";
the problem is that python interpreter adds automatically print ‘hello
\r’
this:
sys.stdout.write('hello
')
does exactly like mel:
print "hello
";


#15

hmm, I’m not quite sure I fully understand what your saying? I tried this in mel:

I typed:

print "hello
";

I get:

print "hello
";
hello

then I typed:

print "hello2
";

My history window now looks like this:

print "hello
";
hello

[b]print "hello2
";
hello2

[/b]Now I tried the same in Python, so:

[b]print 'hello
',

[/b]I get:

[b]print 'hello
',
hello

[/b]then:

[b]print 'hello2
',

[/b]My history window will now read:

print 'hello
',
hello

[b]print 'hello2
',
hello2

[/b]As you can see its working in exactely the way as Mel, I even tried this using the sys.stdout.write('hello
')
command and this works in exactely the same way to.
If you ommit the (
) it will add the echoed command to the previously printed line, which is what you don’t wont.

So by using a (
) in any of your print statments you should get the correct output no matter what method you use, Mel print, Python print or Python sys.stdout.write method.


#16

but it wont show in the command response line. that was the problem in the beginning
python always jumps ahead. then it stays if you put comma after the message, but then its the same as mel printing with no
in the end and everythin printed after comes to same line
sys.stdout.write('hello
') is the only way to get it as mel print "hello
"; it stays in the command response line and prints after goes to another line


#17

Ok, I see what your saying, but this doesn’t seem to happen for me, mine displays fine in the command line, when I use Python print with (
) and a comma at the end of the line it works exactely the same as mel, the (
) seperates the lines in the history window properly and the comma prints the string in the command line perfectly?? I tried it about 10 times with both Mel and Python and they both work the same for me?

I’m using maya 2008, not sure if older versions make a difference or not?


#18

and the comma at the end

agh… I didnt test this print 'hello
',
that works ok.


#19

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.