wxPython and Maya 8.5


#21

Cool. Thanks for the suggestions. I’ll give that a go. Yea, I had done the PYTHONPATH thing before I posted but I’ll confirm that I put in the full paths.

-Sean


#22

Sweetness! Success. However to show the window I had to use:


import WxMaya
WxMaya.MayaFrame.create()

For some reason I got an error when I used your example of


import WxMaya
WxMaya.createMayaFrame()

I’d get this error:


# Error: 'module' object has no attribute 'createMayaFrame'
# Traceback (most recent call last):
#   File "<maya console>", line 2, in ?
# AttributeError: 'module' object has no attribute 'createMayaFrame' # 

But still, this is a great template. Thanks for the help.

-Sean


#23

I got a wx window opening in Maya 8.5 just now. Its screwing up the viewport a bit because it won’t refresh properly when I select objects, only when I zoom in and out. This is probably solved by doing the frame override trick mentioned earlier.

Here are my steps:

downloaded wxPython 2.4 Unicode (wxPython2.8-win32-unicode-2.8.7.1-py24.exe)

A dialog warned me that it couldn’t find Python 2.4 installed and that I’d have to enter a PYTHONPATH pointing to where I’d be installing it to.

On the Destination Location page I set the install folder to: C:\Program Files\Autodesk\Maya8.5\Python\lib\site-packages

On the Select Components page I kept the “Manifest file for XP Theme LnF” checked.
I Unchecked “Make this install be the default wxPython”, since this is specifically for Maya.

I continued through the wizard until it finished installing. It complained that one of my paths was invalid but that’s because the installer itself didn’t pick up on my custom folder.

I then edited my Maya.env file and set the following PYTHONPATH


 PYTHONPATH = C:\Program Files\Autodesk\Maya8.5\Python\Lib\site-packages;C:\Program Files\Autodesk\Maya8.5\Python\Lib\site-packages\wx-2.8-msw-unicode;C:\Program Files\Autodesk\Maya8.5\Python\Lib\site-packages\win32
 

I started Maya.

In the script editor, I created a new Python tab and entered the following code


 import wx
 
 class MyApp(wx.App):
 	def OnInit(self):
 		frame = wx.Frame(None, -1, "Hello from wxPython")
 		frame.Show(True)
 		self.SetTopWindow(frame)
 		return True
 
 app = MyApp(0)
 app.MainLoop()
 

A “Hello from wxPython” window opened. But my viewport started responding incorrectly until I closed the window. Still, this is about 90% of the way there.


#24

Good work everyone !

Has anyone tried this on linux ? Are similar work - arounds nescessary ?


#25

To clarify… Maya does not correctly add any modules on the PYTHONPATH that use a .pth file.
You will have the same problem on linux.

Normally if you have the wx modules and the wx.pth on your PYTHONPATH (in python<version>/site-packages for example) and you run python and ‘import wx’ it will work.

The maya python interpreter will not ‘add’ modules that use .pth files (Numeric.pth, HTMLgen.pth, pygtk.pth, etc) so you have to do it yourself with the ‘site’ module.

Note that any modules that follow the normal package conventions and do not use a .pth file will already import correctly in maya. (ie .py files in a PYTHONPATH dir and subdirs with init.py).

Add the code I’ve provided below to your userSetup.py and you should be good to go. It will handle any .pth files under paths that contain the string ‘site-packages’. No need to add each module directory to the PYTHONPATH as was suggested before.

userSetup.py


 import site
 import os
 
 pythonPaths = []
 try:
 	pythonPaths = os.environ['PYTHONPATH'].split(':')
 except KeyError:
 	pass
 
 for pythonPath in pythonPaths:
 	if pythonPath.count('site-packages'):
 		site.addsitedir(pythonPath)
 

If for some reason you have .pth files in a directory without ‘site-packages’ in the name you can just remove that check. I’ll be contacting Autodesk about this issue.


#26

http://groups.google.com/group/python_inside_maya/t/7cc4300404aa84c4

to get tkinter working in maya, it works perfectly fine unless you open 2, then it crashes but threading would possibly fix it


#27

Has anyone gotten wxPython to work without the viewport refresh getting all messed up?
This is what i have been trying, but the refresh is still messed.


import wx
import pymel
import maya
import maya.utils
from MayaSubFrame import *

def run():	
	class MayaFrame(MayaSubFrame):
		def __init__(self,parent,app):
			MayaSubFrame.__init__(self,parent,app,-1,"Maya Layer Editor",size=(320,240))
			self.bt_sphere = wx.Button(self,label='press to create Sphere')		
			
			#Bind events
			self.bt_sphere.Bind(wx.EVT_BUTTON,self.on_sphere,self.bt_sphere)
			
		def on_sphere(self,evt):
			maya.utils.executeInMainThreadWithResult( pymel.polySphere )
			#pymel.polySphere()
	if (__name__ == '__main__'):
		app = wx.App()
		frame = MayaFrame.create()
		app.MainLoop()
		
maya.utils.executeInMainThreadWithResult( run )

Any ideas how to get this working?


#28

Well I think i got it working! Woot! heres my code, if anyone has a better idea please let me know.


import wx
import pymel
import maya
import maya.utils
from MayaSubFrame import *
import thread


class MayaFrame(MayaSubFrame):
	def __init__(self,parent,app):
		MayaSubFrame.__init__(self,parent,app,-1,"Maya Layer Editor",size=(320,240))
		self.bt_sphere = wx.Button(self,label='press to create Sphere')		
		
		#Bind events
		self.bt_sphere.Bind(wx.EVT_BUTTON,self.on_sphere,self.bt_sphere)
		
	def on_sphere(self,evt):
		maya.utils.executeInMainThreadWithResult( pymel.polySphere )	    
	def on_close(self,evt):
		exit()
		self.Destroy()
def run(something):		
	if (__name__ == '__main__'):
		app = wx.App()
		frame = MayaFrame.create()
		app.MainLoop()
		
thread.start_new_thread(run,(0,))


#29

So after some testing with python threads i think this is the most stable. Although i am having a problem with a print statements. They dont print in maya’s script edior, a python list box pops up displaying your print statement. not sure how to fix this. But heres the code, let me know if it helps?


 import wx
 import pymel
 import maya.cmds
 import maya.utils
 from MayaSubFrame import *
 import thread
 
 ui = None
 
 class MayaFrame(MayaSubFrame):
 	def __init__(self,parent,app):
 		MayaSubFrame.__init__(self,parent,app,-1,"Maya Layer Editor",size=(320,240))
 		self.bt_sphere = wx.Button(self,label='press to create Sphere')		
 
 		#Bind events
 		self.bt_sphere.Bind(wx.EVT_BUTTON,self.on_sphere,self.bt_sphere)
 
 	def on_sphere(self,evt):
 		ret = maya.utils.executeInMainThreadWithResult(pymel.polySphere)[0]			
 		#maya.cmds.python('print %s'%ret)
 	def on_close(self,evt):
 		ui.exit()
 		self.Destroy()
 
 if (__name__ == '__main__'):
 	app = wx.App()
 	frame = MayaFrame.create()
 	#app.MainLoop()
 	args = tuple()
 	ui = thread.start_new_thread(app.MainLoop,args)
 

#30

I was able to get wx.python working in maya by spawning a thread for the ui mainLoop(). But when i used a large script >1000 lines of python code. the UI was really sluggish. So i descided to go a different route. At the Studio I work at we use python with 3dsMax. and basically what we do is create the UI with wx.python and anytime we need info from 3dsMax or need to send commands, send them via a COM interface. So my goal was to do the same thing with maya.

I Ended up creating a new MayaSubFrame class that was striped down a bit more. That when i ran the code through python (not the python in maya) it was attached to the maya window and would respond like a child window of maya. I also wrote another class that connects to maya’s commandPort and was able to send Mel commands. you can actually send python code by using python(…); but only a single line at a time. To get past the single line at a time limitation with the command port, I would take a multi-line string, write it to a file,then call

“source “%s”;%(filename)”

Maya would source the file of mel commands and run it. And it works fairly well. to send multi-line python is a bit more tricky. i was using the same write-to-file technique, but would send file using mapy.exe. a free program that sends python commands. i had a hard time getting return data back though, still messing with this.So now the wx.python ui functions independently of Maya, it doesn’t interfer with Maya’s main thread and when i need to info i send commands. so far it is working really well.

I hope this helps…


#31

Interesting work so far, mberglund!
I had thought about using commandPort to communicate maya with wxPython too. This by far seems to be the most reliable approach to me. How’s the respond time between maya and external python and wxpython?
mapy also plays trick on me. i cant even get it to work with my own text editor for some reason. I will keep playing with it.


#32

The response so far has been great. I haven’t detected any visble lag. The commands that i am sending it are so small anyways.


  cmd = """
$selectedObjects = `ls -sl`;				
 """
  obj = self.com.run_mel_command(cmd)
  

It took a little bit of work to get mapy working but with python i use:


  cmd="""
  a = [1,2,3]
  a.append(4)
  print a
  """
  # write cmd to a file
  
  os.system('path/to/mapy/mapy.exe %s'%(file))
  

Getting maya to return the right data has been a little tricky and for the most part I think i have it. I generally have to parse the return thoroughly to get what I need.


#33

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.