[pymxs] Threading and Modify tab == CRASH

[pymxs] Threading and Modify tab == CRASH

Anonymous
Not applicable
1,375 Views
4 Replies
Message 1 of 5

[pymxs] Threading and Modify tab == CRASH

Anonymous
Not applicable

Hello! 

 

pymxs includes a specific context (mxstoken) that allow us to synchronize calls between different worker threads. 

 

From documentation:

You cannot call pymxs.runtime commands directly on a worker thread, as this will cause a runtime exception. However, pymxs has a mxstoken() function to support multi-threading by providing a synchronization mechanism between worker threads.

 And here and example of its usage:

https://help.autodesk.com/view/3DSMAX/2017/ENU/?guid=__py_ref_demo_m_x_s_token_8py_example_html

 

It works fine, but I have found that 3ds Max crashes if I try to call a function from pymxs.runtime that interacts with 3ds Max modify panel. Super basic code:

 

import pymxs
import threading
import time

def callMXSEntry():
    with pymxs.mxstoken():
		pymxs.runtime.modPanel.addModToSelection(pymxs.runtime.Unwrap_UVW())


def main():
    evt = threading.Event()
    t1 = threading.Thread(target=callMXSEntry)
    t1.start()

main()

 

If you execute this code, 3ds max will crash (tested in 3ds Max 2018 and 2020).

 

I can imagine that this is happening because that call tries to update Modify Tab UI and 3ds Max UI can only be updated/refreshed in 3ds Max main thread but I'm a not sure about it.

 

Do you know a workaround/solution for this?

 

Thank you so much!

0 Likes
1,376 Views
4 Replies
Replies (4)
Message 2 of 5

attilaszabo
Alumni
Alumni

Hi @Anonymous ,

 

We are sorry for the problems you are experiencing. 

 

During the last year, the 3ds Max team has revisited this topic and concluded that the pymxs.mxstoken() is in fact not a solution for a fundamental design limitation of 3ds Max, which is: the "core" of 3ds Max is not thread safe.

Therefore, until this underlying limitation is addressed, we have updated the multithreading related topic in our Python documentation to state that pymxs.mxstoken() should be considered deprecated.

Please see http://help.autodesk.com/view/MAXDEV/2021/ENU/?guid=Max_Python_API_threading_html for more details.

 

I hope this helps, and I'm truly sorry for this.

 

Attila Szabo
Product Owner, 3ds Max
Autodesk
0 Likes
Message 3 of 5

Anonymous
Not applicable

Hi @attilaszabo thank you so much for the info.

 

I have another question more or less related with the same topic.

 

Doy you know if 3ds Max Python APIs expose any kind of function to force the execution of a function in its main thread?

 

For example, Maya exposes this kind of functions executeInMainThreadWithResult.

 

Thank you so much!

0 Likes
Message 4 of 5

denisT.MaxDoctor
Advisor
Advisor

@Anonymous wrote:

Doy you know if 3ds Max Python APIs expose any kind of function to force the execution of a function in its main thread?


What is the 3ds Max Python API?

 

Python itself can work in MAX on any thread. But it can only interact with MAX objects (both system and UI) on the main MAX thread. But if you need to do some calculations, for example, using multithreading, you can do it.

0 Likes
Message 5 of 5

Anonymous
Not applicable

@denisT.MaxDoctor wrote:

@Anonymous wrote:

Doy you know if 3ds Max Python APIs expose any kind of function to force the execution of a function in its main thread?


What is the 3ds Max Python API?


Oh, sorry for the bad usage of the API word here.

 

I was referring to MaxPlus and pymxs.

 

Thank you!

0 Likes