Using python and Max

Using python and Max

Anonymous
Not applicable
5,274 Views
14 Replies
Message 1 of 15

Using python and Max

Anonymous
Not applicable

Hello Community,

 

I just started to dig into using python combined with max - which lead me to the modules "MaxPlus" and "pymxs" and just because of curiosity I would like to know if there are any hints or neat things to know about that topic.

 

In some test I did I created 2.5k spheres per script with different approaches. First, I wrote pythonscript and used "MaxPlus" to create those spheres and it took about 2.62 secs. Then I used "pymxs" the same way and it took about 2.65 sec. With the same approach and MaxScript, it took about 3.6 seconds. It looks for me like using pyhton is way faster than maxScript but what would be the benefit of using MaxPlus instead of pymxs? And are there any other usefull things that are hard to discover and good to know?

 

 

Thanks for sharing your knowledge!

 

Best,

Martin

5,275 Views
14 Replies
Replies (14)
Message 2 of 15

Swordslayer
Advisor
Advisor

It really depends on the use case, for example python was more than ten times slower than maxscript with mesh operations last time I checked, yet python is faster with numbers, but the undo in python is mostly broken etc. Pymxs uses the same limited set of commands as maxscript and is more pythonic, MaxPlus is a SDK wrapper so you get less ready-made functions but you get to access some more low-level parts of max. That said, it's incomplete and differs between max versions, plus last time I checked there was no way to access overloaded methods - C# is slightly better but still you are likely to stumble upon something that you won't be able to do there no matter how hard you try (C++ is the only first-class citizen in this area). 

Message 3 of 15

denisT.MaxDoctor
Advisor
Advisor

@Anonymous wrote:

In some test I did I created 2.5k spheres per script with different approaches. First, I wrote pythonscript and used "MaxPlus" to create those spheres and it took about 2.62 secs. Then I used "pymxs" the same way and it took about 2.65 sec. With the same approach and MaxScript, it took about 3.6 seconds. It looks for me like using pyhton is way faster than maxScript but what would be the benefit of using MaxPlus instead of pymxs? 

 

in general pymxs can't be faster than mxs  

0 Likes
Message 4 of 15

Anonymous
Not applicable

Thanks for the input, swordslayer - What denisT mentioned was what astonished me, too, pymax was faster in my tests than maxscript. I expect pymax to have a lot of wrapped methods and functions and thus doing more calculations in the background than mxs and be slower at the end... Anyways, its tempting to use pymxs since its usage has a lot of similarity to mxs and is easier to learn at the beginning. And it should do most of the tasks for the pipeline of a smaller company where you need to do a lot of 4-to-5-button-tools for other artists. So what topics would preferebly be solved with MaxPlus?

 

0 Likes
Message 5 of 15

Swordslayer
Advisor
Advisor

The speed difference might be due to the python's crippled undo - that's also a word of warning, try undoing stuff you do with python before deciding (especially since you want to create tools for other artists who are used to being able to undo stuff). But it has a nicer UI designer so that would be a plus if you're spending a lot of time on UI. If you want to create scripted plugins, helpers, modifiers, controllers etc, you're out of luck with python, too. As for MaxPlus... leave that for later, if you're not sure now what you could use it for, that won't change any time soon.

Message 6 of 15

Anonymous
Not applicable

This is good to know, especially when a deadline approaches! ... Any hints concerning UI? I did some tests with pyside and I liked it, but I am far from having a solid opinion about that, so are there any other modules that you would recomment to use?

0 Likes
Message 7 of 15

Swordslayer
Advisor
Advisor
0 Likes
Message 8 of 15

malcomarmstrong
Advocate
Advocate

Well, there is woefully inadequate documentation for pymxs, so if you are new to python and maxscript, you may have a issue finding out how to do things. If you have some experience of using BlurPython, you may fare better.

 

MaxPlus has documentation and seems more supported at this point. Why there has to be two versions of python in Max is beyond me as they should have focused on one implementation rather than two, but that's what we have.

 

I have used both and find the MaxPlus a ballache as there are so many dot attributes to connect to make a command string that the code gets cluttered quickly. I could write a module of my own to hold those I suppose, but its more work on top of everything just to make things clear.

 

pymxs seems more usable to me as I have used BlurPython for a few years and its currently more transferable to pymxs, yet pymxs still has its limitations it seems.

 

using pymxs is still mostly a wasted amount of time using trial and error to find out how do higher level actions such as assigning constraints, building rigs and so on. I am sure all the access (or most of it) is there in some form and I don't really want to have to use maxplus and pymxs in one script. Its two sets of learning, which for some may put them off of having a go with coding in Max.

 

By the by, if anyone has found out how to assign a position constraint to an object and add targets, I would like to have a pointer as currently I have spent half hour trawling through different auto completes and have the following, which seem the right way until you get to the last part where you have to set a property which is not described well in its description

 

rt.AddConstraint.setmxsprop

 

if I type:

 

help(rt.AddConstraint.setmxsprop)

 

I get back:

 

Help on built-in function setmxsprop:

setmxsprop(...)
    M.setmxsprop(key, value) -- Explicitlly set a MXS property. Useful for accessing MXS nested property.

 

 

So I am a bit stumped at the mo, but if and when I find out, I will post it here or possibly in a new thread called, "Pymxs snippets and examples" or something similar.

 

 

 

 

0 Likes
Message 9 of 15

malcomarmstrong
Advocate
Advocate

I came up with this to add a constraint but couldn't work out how to add targets. I thought it was what I gave in the code, but it didn't work and I could not work out (yet) how to access the positin constraint directly on the object. Will try again tmrw.

"""
Written by Malcom Armstrong
Jan 2018

NOTES:
First create 3 point objects at different positions using normal max creation methods

For better understanding, execute the lines in order to see some feedback in the listener which may help.

Now select point001, point001 and then point003 in that order, try not to
"""

# ADDING A POSITION CONSTRAINT
import pymxs
rt = pymxs.runtime
#Run this line first
thePoints = list(rt.selection)
print thePoints[1]
anObj = thePoints[0] # cast object to have constraint to a variable
#Run this line next (check listener)
print rt.selection[0].controller
#Run this line to create a contraint object
myPosCon = pymxs.runtime.Position_Constraint()
#In maxsript, to add a target is as follows: $Point001.controller[1].controller.appendTarget $Point002 50.0
#but using the setmxsprop is only one key, one value at a time so...
#The way to add targets below does not work
	#myPosCon.setmxsprop("appendTarget", thePoints[1])
	#myPosCon.setmxsprop("appendTarget", thePoints[2])

#getmxsprop and setmxsprop.
# These properties are a key value pairing, as follows

anObj.setmxsprop("name", "conTgt")
# this does change the name, so......
# Set some other properties for the point helper (anObj)
anObj.Box=True
anObj.cross=False
print anObj.wirecolor

#Run the following lines
print anObj.getmxsprop("name")
print anObj.getmxsprop("position")
#Run this line to check the current position controller
print anObj.getmxsprop("position.controller")

#Now we can add the constraint object to the object cast to a variable
anObj.setmxsprop("position.controller", myPosCon)

print anObj.getmxsprop("position.controller")

# Accessing the position constraint to get or set targets etc.

# In Maxscript, if point one had a Position Constraint between points 2 and 3, the way to check is this: $Point001.controller[1].controller.getNumTargets()

#slight issue here at the mo as...

print rt.getPropNames(myPosCon)
# only returns what appears to be a max array: #(#weight, #relative)

0 Likes
Message 10 of 15

Swordslayer
Advisor
Advisor

AppendTarget is a method, not property. The 'help' command won't help you here, pymxs calls the runtime MAXScript engine, there're no static properties or methods (other than the property getter/setter), either query the methods in MAXScript or consult the MAXScript reference. Btw. you were adding the constraint targets before assigning the controller, unless the controller 'new' flag is cleared before assinging it, the controller will be cleared upon assignment so that its value matches the current controller value it is replacing. Append the targets after assigning the controller. The whole script would look something like this:

 

p1 = rt.getNodeByName('Point001')
p2 = rt.getNodeByName('Point002')
p3 = rt.getNodeByName('Point003')
posCtrl = rt.position_constraint()

p1.setmxsprop('position.controller', posCtrl)
posCtrl.AppendTarget(p2, 1.0)
posCtrl.AppendTarget(p3, 1.0)
Message 11 of 15

malcomarmstrong
Advocate
Advocate

That's great, thanks very much. I thought I may need to preload the targets and then add it, which didn't really make that much sense, I was just trying different approaches. Its currently some trial and error with some object assignation and editable attributes that I am resolving through research. 

 

I like this implementation over maxPlus, seems more natural, due to blurPython experience and i`m hoping its development and place in Max is maintained and evolved. As I have said previously, its release with little to no documentation is what may put some off, which would be a shame as it lends itself to be developed further.

 

Thank you for your simple and well explained reply.

 

 

0 Likes
Message 12 of 15

Swordslayer
Advisor
Advisor

The 'little to no documentation' is because apart from the property setters/getters and special treatment of name values, there's nothing specific to it. It calls MAXScript, so the documentation you should use is the MAXScript documentation - just like there's no specific documentation for the .NET API and you're supposed to use the C++ documentation instead. As long as new features will be added to MAXScript, you will have new features in pymxs by extension - which applies also to any MAXScript function exposed by a 3rd party tool or script.

Message 13 of 15

malcomarmstrong
Advocate
Advocate

Just a note to say that where you had rt.position_constraint, it did not work for me until i set it to rt.Position_Constraint

 

Other than that, working great. I can progress onto the next parts. 

 

Hopefully, others will see the usage and be able to adapt it to other assets/attributes.

 

Cheers

0 Likes
Message 14 of 15

Swordslayer
Advisor
Advisor

@malcomarmstrong wrote:

Just a note to say that where you had rt.position_constraint, it did not work for me until i set it to rt.Position_Constraint


Some other issue at play probably. Inlike python, MAXScript is case insesitive (and pymxs respects that); pOsItIoN_cOnStRaInT will work just as well (not that I'd recommend it Smiley Wink).

Message 15 of 15

malcomarmstrong
Advocate
Advocate

What the Hell! I just tried again and it worked. Probably a typo. I am working with a busted right hand and typing with my left and trying to use mouse with the left as well, so its more than likely I did that.

 

I knew MAXScript was case insensitive, should do after all these years!

 

Thought now that it was Pythonised, it would not be like that but no bigee. I just saw it pop up in the auto complete as being camel cased, so just assumed the syntax was incorrect. Not sure what I did to sort it, but I rewrote some code, so it was probably in that chunk.

 

Capture.PNG 

Already things are working better with just a little more insight and help. 

 

Cheers

0 Likes