This tutorial will walk you through some of the basics of using Python in 3dsMax.
Py3dsMax is the term that we use to refer to Python in 3dsMax. Its technically a plugin installed to:
MAX_PATH/plugins/blur/blurPython.dlx
And is a C/C++ plugin that creates a mapping between a basic Maxscript Value class and a basic Python PyObject class.
Essentially, its just a way to convert a Maxscript value to Python, and vice versa.
This allows us to directly access information in max - including plugins and dynamically created maxscripts, as well as all Python modules, including compiled pyd files without having to work through COM, .Net or ActiveX.
The first example we'll do will be written in maxscript, and will use python modules, so - open the maxscript listener and type:
maxscript code
re = python.import "re"
What we have just done is import the Python regular expression module (re) as a Maxscript value.
Now type:
maxscript code
results = re.match "Sc(\d+)" "Sc001" print (results.groups()[1])
You should see an output of 001
This is just a simple example of running some python code through 3dsMax. In this fashion, you can import and use any standard Python module in your maxscript tools - Python has a lot of very powerful built-in libraries that can really enhance your programming.
BUT...the far better way is to actually shift to Python programming all together...
The greater trick than running Python in Maxscript, is actually running Maxscript in Python.
In the above example, your main code would still be written in Maxscript, and then invoking python calls when useful or necessary.
The way that we are developing moving forward is the opposite - we are developing in Python, and invoking maxscript calls when useful or necessary.
This allows us to take advantage of the python language as a whole - not just useful modules.
This includes Object-Oriented Programming, the Qt toolkit, native Database connections, more powerful syntaxes and more.
At this point you should have noticed that there is a new Python
menu in your 3dsMax window. If you don't see that, make sure that you have properly installed your BlurOffline code.
Once you see the Python menu, choose Python >> Show Logger
This will bring up the interactive Python console - the same one you've seen in the Blur IDE, but now running as a child window of 3dsMax and running natively within the 3dsMax event loop.
Type:
python code
>>> from Py3dsMax import mxs
Theoretically, this should not give you any errors - if you do get any errors, make sure that you have everything installed correctly and is for the proper version (x32/x64 & release) of 3dsMax.
Assuming that import was successful, what you have just done is imported all of Maxscript.
We didn't wrap any code or modify anything - so all of your maxscript commands that you would type are simply accessible through this new mxs
module.
For instance, if I wanted to create a new sphere in maxscript, I would type in the maxscript listener
maxscript code
Sphere name:"MySphere"
In the Python Logger, I would type:
python code
>>> mxs.Sphere( name = "MySphere" )
All this is doing, is looking up the 'Sphere' command in the maxscripts globals, and calling it, passing in the optional parameter 'name'. Same exact process as the maxscript code, simply with Pythonic syntax.
This is true for every maxscript command written.
It even works on dynamically created commands.
Try this, in the maxscript listener type:
maxscript code
function foo = ( print "bar" )
and then in the Python logger type:
python code
>>> mxs.foo()
You should see "bar" print in the Maxscript listener. This is important to note - maxscript's print
command will log to the listener, while python's print
command will log to the logger.
So - what you have now is a Pythonic entrance to 3dsMax.
Any code you see in the Maxscript docs can and will work in Python, however, some things about the way maxscript was programmed make it not translate to Python properly.
One of the differences in Maxscript that does not translate to Python is the way that Maxscript works with type conversion.
maxscript code
"1" as integer 1
Invoking the as
command will convert a one variable to another type. In Python there is no as
command, rather you actually instantiate
a variable type with another.
python code
>>> int("1") 1
Base types (int,float,bool,list/array,string,etc.) will be automatically converted between the two languages.
More complex variables (classes,modules,structs,etc.) will be wrapped in a language specific accessor.
Something like a Maxscript Name however, will be automatically converted to a Python string.
In Maxscript its easy to convert a string to a Name type:
maxscript code
"testing" as name #testing
In Python however, this is not as easy to manage. The convention as
in Maxscript is a Parser level command - meaning there is no programmatic way to access it.
Also, a Maxscript Name class does not support instantiation like Python would.
python code
>>> mxs.Name("testing") ERROR
What we have done in these cases is provide a maxscript struct called the PyHelper struct. It can be found at:
MAX_PATH/scripts/startup/init_pyhelper.ms
This provides utility methods to maxscript that can be invoked in Python to get around some of Maxscripts quirks. The above problem would then be solved by writing:
python code
>>> mxs.pyhelper.namify("testing") #testing
There is another concept that defies common programmatic syntax, and this was found buried in the Maxscript SDK called nested parameters
.
This is essentially a programmatic shortcut written directly into the Maxscript language.
For instance, if you were to do:
maxscript code
>>> s = Sphere() >>> s.position [0,0,0] >>> s.position.controller Controller
There would be no problem.
However, if you were to do:
maxscript code
>>> s = Sphere() >>> pos = s.position >>> pos.controller ERROR
You would run into an error. Logically, this is not intuitive. In most programming syntaxes, the line s.position.controller
implies that controller
is a property of s.position
.
In Maxscript, this is not actually the case however. Instead, Maxscript implemented a concept called nested parameters
, by which they register the property position.controller
to the Sphere
object.
So, how does this relate to Python?
The way we have implemented the Python code, is that it accesses properties in a standard programmatic fashion. Meaning when you access s.position
, you have already interpreted the property of the Sphere
instance. If you were then to try to access the controller
parameter, the code would fail, as controller
is not a property of s.position
.
The bad news is that there is nothing in the documentation that will aide you in finding these cases. The good news is there is always an alternative way to access the information.
Instead of calling:
maxscript code
s.position.controller
You can call:
maxscript code
getPropertyController s.controller "position"
So, Python's equivalent would be:
python code
>>> mxs.getPropertyController( s.controller, "position" )
It may not be that intuitive...but it works.
A more complete example:
>>> s = mxs.getPropertyController( obj.controller, 'scale') >>> obj = mxs.selection[0] >>> obj $Teapot:Teapot001 @ [-3.051827,10.207859,0.000000] >>> s = mxs.getPropertyController( obj.controller, 'scale') >>> s.X_scale 50.0 >>> s.X_scale = 100.0 >>> p = mxs.getPropertyController( obj.controller, 'position') >>> p.X_position -3.05182695389 >>> p.X_position = 0
We're actually in the process of abstracting this entire system - not just for 3dsMax but for all 3d Applications. For more information about that, look at the blur3d system.
Commands like "at time" do not translate to python. We have made a wrapper for this function in Py3dsMax.AtTime
from Py3dsMax import AtTime at = AtTime() at(10) # All commands run now will return results at frame 10 at(20) # All commands run now will return results at frame 20 del(at) # remove the at time functionality. As long as the at object exists it will be applied.
This system will probably be replaced by using python's with call, but we haven't had time to implement it yet.
As you saw in the PyQt tutorial, you can use the Blur IDE to generate Tools.
If you run your Treegrunt in 3dsMax (MainMenu >> Python >> Treegrunt
), you'll be able to find your SampleTool available to you.
This is becuase when you ran the tutorial, you registered the Tool to the Studiomax Application.
Wiki: BlurIDE
Wiki: Py3dsMax
Wiki: PyQt
Wiki: SideBar
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: looca...@gmail.com
Hi there,
first off, amazing job guys! Really great to see this for Max! THANKS
Now for my problem :D I managed to make all this run under Vista Business x64, however I upgraded to Windows 7 Professional x64, did the very same install (except for Python 2.6.4) on it, but I can't seem to be able to get the blurPython26.dlx loaded in Max 2009 x64. It's strange as it used to work on Vista, but doesn't on W7.
Could you, please, let me know where might be the problem? Is it the libs not being loaded in Windows, or the plugin just doesn't work all of a sudden?
Thanks a lot in advance, cheers.
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: villageh...@gmail.com
Looks cool
Can you provide the source for the plugins please. You have released the binaries under the GNU General License and as far as I understood that means you need to provide the source too. Or am I just being dumb about where the source is held?
Cheers
Greg
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: damian.f...@gmail.com
Are there plans for max2010 release? we've already swtiched up at the studio I work at.
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: eric.hul...@gmail.com
We're going to be releasing the source code, but right now its kind of a mess with no documentation, we're in the process of cleaning it up. If you desperately want the source code...email me and we can see what we can do.
We just released a 2010 build for everyone.
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: jian...@gmail.com
Hi,how can i use paramters of maxscript in py3dsmax ?,the code like this:
maxscript: render renderType:#bakeSelected vfb:off progressBar:true outputSize:[512, 512]
this code snippet: renderType:#bakeSelected
i do not know how to writ these code in python.
thanks.
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: eric.hul...@gmail.com
hey guys - new stable builds are up for all versions of this project, as well as the source code for the blurPython.dlx.
@ jianbox, your code would look like this:
mxs.render( renderType = mxs.namify( 'bakeSelected' ) )
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: michael....@gmail.com
hi! first of all thank you very much for this plugin, it is very useful! and now some (probably) problems: trying to import some modules:
1) PIL >>> from PIL import image #OK >>> im = Image.open("C:/Mandril.jpg") #OK >>> im.show() long error ... : .... raise ImportError?("The _imaging C module is not installed") ImportError?: The _imaging C module is not installed
2) import pygame error: from pygame.base import ImportError?: DLL load failed: The specified module could not be found.
everythig is OK with this modules in the standard python. python v26_64, all modules taken from here: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyopengl (64 bit versions)
tell me please what can i do in this situation? i need them =) Thank you!
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: eric.hul...@gmail.com
@ michael.levi.fx
Hey man, I just downloaded both of those installers from the web and they work just fine for me...I'm running 3dsMax 2010 x64 on a Windows XP system...I don't know - what versions are you trying against so I can check?
View and moderate all "wiki Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Wiki"
Originally posted by: michael....@gmail.com
thanks for fast reply, Eric! 3dsMax version 2009 x64, windows XP..