wxPython and Maya 8.5

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
  02 February 2007
Originally Posted by FiendishVegan: Here's the Aloys Baillet's code stripped down and functioning in Maya.

To use:
1. save code to a file called WxMaya.py (or whatever you want to call it) and place in scripts directory
2. in Maya type:

import WxMaya
WxMaya.createMayaFrame()



Ok, I've been racking my brain on this. I'm getting so many errors. I realized that I had to copy the site-packages from Python24 dir to Maya's site-packages dir. It's still telling me that it can't find the wx module. Also I needed the win32 site-package as well? Plus the formating got whacked when you added it to this thread. I'm not bitching, I'm totally stoked that this works for someone. Just a few details were left out to make this work. I'm just want to get this work on my end. Can you lend any help with what you had to do for Maya to recognize the wx, win32GUI and any other module needed to get this to work?

This is the current error I get:
# Error: line 0: No module named wx
  # Traceback (most recent call last):
  #   File "<maya console>", line 1, in ?
  # ImportError: No module named wx # 
  


Thanks,
-Sean
__________________
===============
snolan.net

 
  02 February 2007
doh, double post...rrr.
__________________
===============
snolan.net


Last edited by NolanSW : 02 February 2007 at 06:48 AM.
 
  02 February 2007
You have to change your PYTHONPATH to something like this:


   PYTHONPATH = C:\Python24\Lib\site-packages;C:\Python24\Lib\site-packages\wx-2.8-msw-unicode;C:\Python24\Lib\site-packages\win32
  


I think it's better to put python modules in default locations and change PYTHONPATH accordingly.
 
  02 February 2007
What you need to run wx in Maya 8.5:
wxPython
http://sourceforge.net/project/down...mirror=internap
Python Extensions for Windows
https://sourceforge.net/projects/pywin32/

Installing these modules will place them in your python site-packages folder(C:\Python24\Lib\site-packages). From there you SHOULD be able to update your PYTHONPATH environment variable like marcinm said. However, I have had poor luck so far getting maya to use PYTHONPATH even though the docs say it should. I have been manually copying modules over to the maya site-packages folder(C:\Program Files\Autodesk\Maya8.5\Python\lib\site-packages). I'm not sure why this didn't work for you NolanSW. Make sure you get all the files that are floating loose in site packages. Failing to copy over wx.pth or pywin32.pth will cause maya to not find the modules.

Sorry about the jacked-up formatting in my post. I will try to post again. If the indenting is still too messed up when you paste it, I can send you the source file if you message me with your email.


 import os
 import wx
 import traceback
 import win32gui, win32process
 
 class MayaSubFrame(wx.Frame):
 	"""
 	CLASS: MayaSubFrame
 	EXTENDS: wx.Frame
 	PURPOSE: Inherit from this class to create a wx frame in maya. See example MayaFrame below.
 	"""
 	_topLvlMayaWinHandle = None
 	
 	@classmethod
 	def create(cls,*args,**kw):
 		app = wx.GetApp()
 		
 		if app is None:
 			app = wx.App(redirect=False)
 			
 		topHandle = MayaSubFrame._getMayaTopWindow()
 		top = wx.PreFrame()
 		top.AssociateHandle(topHandle)
 		top.PostCreate(top)
 		app.SetTopWindow(top)
 		
 		try:
 			frame = cls(top,app,*args,**kw)
 			frame.Show(True)
 			
 		except:
 			print cls.__name__
 			print traceback.format_exc()
 			frame = None
 			
 		top.DissociateHandle()
 		return frame
 	
 	
 	@staticmethod
 	def _getMayaTopWindow():
 		
 		if MayaSubFrame._topLvlMayaWinHandle is not None:
 			return MayaSubFrame._topLvlMayaWinHandle
 		
 		def callback(handle,winList):
 			winList.append(handle)
 			return True
 		
 		wins = []
 		
 		win32gui.EnumWindows(callback, wins)
 		currentId = os.getpid()
 		
 		for handle in wins:
 			tpid,pid = win32process.GetWindowThreadProcessId(handle)
 			if pid == currentId:
 				title = win32gui.GetWindowText(handle)
 				if title.startswith("Autodesk Maya"):
 					MayaSubFrame._topLvlMayaWinHandle = handle
 					return handle
 		return None
 	
 	def __init__(self,parent,app,id, title, pos=(150,150), size=(640,480), style=wx.DEFAULT_FRAME_STYLE | wx.FRAME_FLOAT_ON_PARENT | wx.FRAME_NO_TASKBAR):
 		wx.Frame.__init__(self, parent, id, title, pos, size, style)
 		self.app = app
 		self.panel = None
 		self.Bind(wx.EVT_CLOSE,self.OnClose)
 		
 	def OnClose(self,event):
 		self.Show(False)
 		self.Destroy()
 		win32gui.DestroyWindow(self.GetHandle())
 		win32gui.SetFocus( MayaSubFrame._topLvlMayaWinHandle )
 		
 class MayaFrame(MayaSubFrame):
 	def __init__(self,parent,app):
 		MayaSubFrame.__init__(self,parent,app,-1,"Maya WxFrame",size=(320,240))
 		
 def createMayaFrame():
 	MayaFrame.create()
 	return True
 
__________________
"The secret to creativity is knowing how to hide your sources." -- Albert Einstein
 
  02 February 2007
At the begining I had problem with PYTHONPATH too. PYTHONPATH variable must contain detailed paths to module (not only one directory, but all one by one). So PYTHONPATH have to include not ony \Lib\site-packages, but also \Lib, \Lib\site-packages,\Lib\site-packages\wx-2.8-msw-unicode and so on. It's strange, but when I change PYTHONPATH to \Lib\site-packages it doesn't work.When I changed it to detailed locations it worked.

I hope my instructions are readable, because my English isn't that good
 
  02 February 2007
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
__________________
===============
snolan.net

 
  02 February 2007
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
__________________
===============
snolan.net

 
  01 January 2008
wx working in Maya 8.5

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.
 
  02 February 2008
Good work everyone !

Has anyone tried this on linux ? Are similar work - arounds nescessary ?
 
  02 February 2008
fixing the problem with 'import wx'

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.

Last edited by chapterzero : 02 February 2008 at 05:00 AM.
 
  02 February 2008
http://groups.google.com/group/pyth...cc4300404aa84c4

to get tkinter working in maya, it works perfectly fine unless you open 2, then it crashes but threading would possibly fix it
 
  05 May 2008
wxPython and Threads

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,s  elf.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?
__________________
-Animatix
My Rigging Blog
 
  05 May 2008
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,s  elf.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,))
__________________
-Animatix
My Rigging Blog
 
  05 May 2008
more testing...

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,s  elf.bt_sphere)
 
 	def on_sphere(self,evt):
 		ret = maya.utils.executeInMainThreadWithResult(pymel.pol  ySphere)[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)
 
__________________
-Animatix
My Rigging Blog
 
  05 May 2008
Things i have found

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...
__________________
-Animatix
My Rigging Blog

Last edited by mberglund : 05 May 2008 at 09:26 PM.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 03:38 AM.


Powered by vBulletin
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.