Update Max with new objects from outside 3d Package running Python?

Update Max with new objects from outside 3d Package running Python?

Anonymous
Not applicable
782 Views
7 Replies
Message 1 of 8

Update Max with new objects from outside 3d Package running Python?

Anonymous
Not applicable
So I'm a fairly technical artist, although not a hardcore programmer, coming from another software package learning the technical aspects of Max. Just for practice, I wrote a script in Softimage that acts like a “Send to 3ds Max” button. Within Softimage, it saves selected objects in an FBX file, creates a max script which is saved to hard drive, then opens a new instance of Max with an argument to open said maxscript which of course imports the FBX file into a clean scene. All works great. But what would be cool is if I could update an already open Max scene with new objects sent from Softimage.

Here's what I'm wondering. Is Max or Maxscript open in such a way that I am able to send objects to an already open version Max from an outside Python process running in Softimage (or any other package)? Or is this something lower level that would be accessed through Max's SDK in C++?

Any hints on how this is done would be appreciated.

FYI, if any of you are thinking, "There already is a send to Max button in Soft", Well, yeah there is, but it only sends particles unfortunately.

Cheers,
Paul
0 Likes
783 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
How about this (abstract):
From an open 3dsmax session you monitor a specific export-folder.
If something is exported from SI in this folder, it gets imported into 3dsmax.
0 Likes
Message 3 of 8

Anonymous
Not applicable
Monitor through Maxscript? Or is there a way to set up max to do it automatically? I checked to see if there were any maxscript events that can monitor a folder, but haven't seen any so far.
0 Likes
Message 4 of 8

Anonymous
Not applicable
You can use Max OLE Automation feature.

Windows (from 95 to the last) come with native script engine WSH (Windows Scripting Host) that allow running JScript and VBScript (without installing whatever).

So here is what I use in VBScript:
REM ------------------------------------------------
'
' Running MAXScript using OLE Automation
'
' Author: P. Karabakalov (project3d.narod.ru)
'
' In the MAXScript Reference READ this topics:
' * Setting Up MAXScript OLE Automation
' * Exposing MAXScript Functions
'
' Be SURE you expose at least Mxs FileIn function:
' for example:
' registerOLEInterface #(Execute, FileIn)
'
'
REM ------------------------------------------------

Option Explicit

Dim MXSObj, MSFile

With WScript.Arguments

'** break if no argument '
If .Count <> 1 Then WScript.Quit(-1)

'** store the file name '
MSFile = .Item(0)

With CreateObject("Scripting.FileSystemObject")

'** break if file not exist '
If Not .FileExists(MSFile) Then WScript.Quit(-2)

'** break if not a .MS file '
If .GetExtensionName(MSFile) <> "ms" Then WScript.Quit(-3)

End With

End With

On Error Resume Next

'** connect to last COM registered Max version '
Set MXSObj = CreateObject("MAX.Application")

'** if you have not at least one 3ds Max registered as OLE server '
'** an automation error will arise '
If Err Then
MsgBox Err.Description, , "Error # " & CStr(Err.Number)
WScript.Quit(-4)
End If

Err.Clear ' end of error handle '

'** we`re here if everything is fine '

'** if 3ds Max is not running then VB built-in IsObject function '
'** will pause the script until Max load '
'** (it`s my own discovery though) ;) '
If IsObject(MXSObj) Then MXSObj.FileIn(MSFile)

'** clean the OLE object (that not close Max) '
Set MXSObj = Nothing

If you prefer to rewrite this as python script, then as am not work with it I can post only this snippet code:
from win32com.client import Dispatch

MXSObj = Dispatch("MAX.Application")

MXSObj._FlagAsMethod("Execute")
MXSObj.Execute("Box()")

MXSObj._FlagAsMethod("FileIn")
MXSObj.FileIn("script.ms")
0 Likes
Message 5 of 8

Anonymous
Not applicable
Hey, thanks for the time and effort Anubis. Very informative post.

This definitely points me in the right direction I think. I did a little research online regarding using OLE/COM to talk to Max. Seems some guys are getting good results using socket listener/server setup too after reading this interesting thread on the subject.
http://tech-artists.org/forum/showthread.php?2723-Python-to-MaxScript-to-Python
0 Likes
Message 6 of 8

Anonymous
Not applicable
Glad to know that I helps with something 🙂
0 Likes
Message 7 of 8

Anonymous
Not applicable
As you mention socket, maybe you`re after two-sided connection?
If you only need to get the result back from Max to your script,
I think that the clipboard (setclipboardText) is a good approach.

An example MXS function:
fn ExecuteEx cmd = (
global gvResult = (execute cmd) as string
setclipboardText gvResult
globalVars.remove #gvResult
)

And of course in your py code should make some wait-loop.
I can show only how it looks like in vbs --
Dim retData, iRetry : iRetry = 0
With New Clipboard 'Clipboard is my own class '
.Clear
If IsObject(MXSObj) Then MXSObj.ExecuteEx("2+2")
Do Until Len(retData) <> 0
WScript.Sleep 100 'time in millisec. '
retData = .Data
iRetry = iRetry + 1
If iRetry > 100 Then Exit Do
Loop
End With
MsgBox retData 'display return result '
0 Likes
Message 8 of 8

Anonymous
Not applicable
Hey Thanks Anibus.
Yes, 2 way is way more sexy than one way (no pun intended). This isn't something I need to do now, but possibly in the months to come and wanted to at least get an idea on how to think about the problem in the mean time. I have some pretty pressing things to learn at the moment.

Regarding how Max, Maya and Softimage communicate now, it's default and nativly coded for 2 way communication. I wonder if it's safe to assume they are using old school tcp sockets, or something fancy and propitiatory.
0 Likes