Notifications
Clear all

[Closed] Python + MXS

Maybe this is obvious, but I thought others might have had similar problems.

Don’t be afraid to post the bluntly obvious, it’s sometimes what throws us.

Just a quick post to say Hi and Thanks very much! This has been very interesting to read along, even though I have very little experience with COM, .Net or Python.

One question though: How do you pass variables/data back an fourth when interfacing with Python via COM? Can I ‘send’ an array or a struct to Python?

Cheers
– MartinB

2 Replies
(@joel_hooks)
Joined: 11 months ago

Posts: 0

With the OLE you can only send simple type, INT, String, ect. I don’t know if the same is true for COM. Be cooler if you could trade typed objects!

(@specxor)
Joined: 11 months ago

Posts: 0

Hi, Martin,

Just to elaborate on Joel’s post, OLE objects in Max can receive(and send) arrays, ints, floats, bools, colours, strings and undefined. These are handled with the SafeArrayWrapper. Unfortunately more complex types such as structs or max objects cannot be parsed through COM. COM implements the VARIANT data structure, for instance when you send an int from python via COM it is converted to VARIANT type VT_14 and on the other end Max is able to convert this back into an int.

Just look up SafeArrayWrapper in the max help, there is a pretty good explanation of it, alternatively just search through the MSDN stuff and it will pop up.

Cheers
Dave

Dave & Joel, thanks for the info!

– MartinB

It probably sounds sad, but I just got the first post example running and I felt a little thrill of excitement.
I’m going to buy you a round whenever I see you David.

this work


import win32com.client
       
  
conn = win32com.client.Dispatch("MAX.Application.9")
       
  
conn._FlagAsMethod("DoSomething")

for i in range(1,11):
    
    conn.DoSomething("Box  position:[10,10,10] length:10 Width:10 height:10")
    #print i


but


import win32com.client
       
  
conn = win32com.client.Dispatch("MAX.Application.9")
       
  
conn._FlagAsMethod("DoSomething")

for i in range(1,11):
    
    conn.DoSomething("Box  position:[i*10,i*10,i*10] length:10 Width:10 height:10")
    #print i

doesn’t ??

The problem has to do with how “i” is been converted. From what I can tell, I would say it is a direct conversion to string value (ie “i*10”, not (the value of (i = 2) * 10) [i]= 20)

[/i]I’m not a phython developer, but I think


 conn.DoSomething("Box  position:[" + i*10 + "," + i*10 + "," + i*10 + "] length:10 Width:10 height:10")
 

Should give you the result you after…or something simular to it.

Shane

It’s not sad at all! And if I know the other developers on this forum, they are probably feeling excited with you!! And yes, Dav, we all owe a round!!

Shane

@ RustyKnight


 conn.DoSomething("Box  position:[" + i*10 + "," + i*10 + "," + i*10 + "] length:10 Width:10 height:10")
 

AFAIK … you should use single quotes inside double quotes to avoid conflicts in Python

So , it should go


conn.DoSomething("Box  position:[' + i*10 +',' + i*10 + ',' + i*10 + '] length:10 Width:10 height:10")

however , this either doesn’t work , the problem i see is i’m not sure what’s going on here
i mean starting from
Conn.DoSomething(– aren’t we maxscripting here and not pythonning ?)
and we’re not bothered how python handles conversion to strings ??

Conn.DoSomething(– aren’t we maxscripting here and not pythonning ?)
and we’re not bothered how python handles conversion to strings ??

That’s exactly right and that’s exactly your problem.

Basically what you were saying to maxscript was “i*10” which means nothing to maxscript, “i” is undefined as far as maxscript is concerened (or worse, equal to something you don’t know)!!

You need to evaluate the string so maxscript reads it as;

…assumining i = 10

“Box position:[’ + i10 +’,’ + i10 + ‘,’ + i10 + ‘] length:10 Width:10 height:10″
=”Box position:[’ + 10
10 +’,’ + 1010 + ‘,’ + 1010 + ‘] length:10 Width:10 height:10”
=“Box position:[100,100,100] length:10 Width:10 height:10”

And pass THIS value to maxscript, where it can then evaluate the value.

Previous, as I said eairler, maxscript had no concept of what “i” is or it’s value, it is out side of it’s context, “i” is only valid within python, or if you like, with in the local context of the loop…it is not translated over to max…(this is not totally accurate, but the concept is the same)

Again, I’m not a python developer and I’ve not tried any of this, I just like the idea!

Try this, in maxscript:


 execute "Box  position:[i*10,i*10,i*10] length:10 Width:10 height:10"
 

and see what happens, because that is, in effect, what you are saying.

I hope this makes some kind of scense, not having python in front of me, nor never having done this kinda work, I can only “assume” that this is correct, but as far as I can tell, “i” is not been evaluated as a variable, but a litrial.

Shane

well , eh this is more confusing !

cause … in the first post in this thread , we didn’t define anything but an empty function

and in python we excuted this function and provided the function definition !

i do see that maxscript can’t understand what is (i) , but as i said … this is more confusing

python is passing what to maxscript ? only strings ?
all the benefit of communication is passing the green words as mxs commands ?
cause clearly this is what you tell when you say


Try this, in maxscript:
Code:

 execute "Box  position:[i*10,i*10,i*10] length:10 Width:10 height:10"
 

and see what happens, because that is, in effect, what you are saying.

 
Page 7 / 26