Unity Community


Page 1 of 3 123 LastLast
Results 1 to 20 of 47

  1. Location
    Chicago
    Posts
    19

    Max/MSP communication?

    I've seen a couple posts relating to this, but seemingly no clear answer. Has anyone figured out an easy way to communicate data between Unity and Max/MSP? Perhaps OSC? The UDP method looks quite complicated-at least to me.

    Thanks!
    Brian

  2. Volunteer Moderator



    Location
    Copenhagen
    Posts
    5,299
    Yep, there's a few people using Unity on this forum that would pee their pants with glee if an OSC or UDP plug would be written for Unity to talk with Max or SuperCollider.

    Anyone holding out?


  3. Posts
    32
    Haven't persuaded myself to buy Max/MSP yet, but this quick example worked with Puredata.

    Just stick the following code on an empty Gameobect and for the PD server wire up a 'netreceive' on port 32000 feeding out to an 'OSCroute'. (I used pd-extended don't know if the base version has this)

    Code:  
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4. using System.Text;
    5. using System.Net.Sockets;
    6.  
    7. public class PDCall : MonoBehaviour {
    8.  
    9.     private TcpClient client;
    10.     private Stream stream;
    11.     private ASCIIEncoding asen;
    12.  
    13.     void Start () {
    14.         client = new TcpClient();
    15.         client.Connect("127.0.0.1", 32000);
    16.         stream = client.GetStream();
    17.         asen= new ASCIIEncoding();
    18.         InvokeRepeating( "SoundUpdate", 1, 0.3f );
    19.     }
    20.    
    21.     void SoundUpdate () {
    22.         string msg = "/note " + (60+(int)(Random.value * 20)) + ";";
    23.         byte[] ba = asen.GetBytes(msg);
    24.         stream.Write( ba, 0, ba.Length );
    25.     }
    26. }

    Only one way communication here but enough to get you started.

    Joc


  4. Location
    Vamdrup, Denmark
    Posts
    108
    thank you Joc. This was just what I needed to get started.

    I had a lot of problems with the netreceive object crashing Max/Msp and had no luck using the mxj net.tcp.recv object.

    But after a lot of trial and error I've managed to rewrite your code from using Tcp to Udp. I also changed your code from C# to javascript.

    Code:  
    1. import System.Collections;
    2. import System.IO;
    3. import System.Text;
    4. import System.Net.Sockets;
    5.  
    6.  
    7.  
    8. private var client;
    9. private var asen;
    10.  
    11. private var msg : String;
    12.  
    13. function Start(){
    14.     client = new UdpClient();
    15.     client.Connect("127.0.0.1", 32000);
    16.     asen= new ASCIIEncoding();
    17.     InvokeRepeating( "SoundUpdate", 1, 0.5 );
    18. }
    19.  
    20. function Update(){
    21.     //
    22. }
    23.  
    24. function SoundUpdate(){
    25.     msg = "val0 " + Random.value * 20 + " : ";
    26.     msg += "val1 " + Random.Range(50,100) + " : ";
    27.     var ba : byte[] = asen.GetBytes(msg);
    28.     client.Send( ba, ba.Length );
    29. }

    If anybody is interested I have include Max/Msp code

    Code:  
    1. max v2;
    2. #N vpatcher 339 116 1081 609;
    3. #P window setfont "Sans Serif" 9.;
    4. #P window linecount 1;
    5. #P newex 59 110 25 196617 iter;
    6. #P newex 59 157 20 196617 t b;
    7. #P newex 59 131 30 196617 sel :;
    8. #P newex 59 185 61 196617 zl group 10;
    9. #P number 105 244 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
    10. #P number 59 244 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
    11. #P newex 59 215 102 196617 route val0 val1;
    12. #P window linecount 2;
    13. #P comment 412 112 171 196617 only one receiving object can be bound to any one port.;
    14. #P window linecount 1;
    15. #P newex 59 88 150 196617 mxj net.udp.recv @port 32000;
    16. #P connect 0 0 8 0;
    17. #P connect 8 0 6 0;
    18. #P connect 6 0 7 0;
    19. #P connect 7 0 5 0;
    20. #P fasten 6 1 5 0 84 179 64 179;
    21. #P connect 5 0 2 0;
    22. #P connect 2 0 3 0;
    23. #P connect 2 1 4 0;
    24. #P pop;

    Now I just need to make it receive data from Max.


  5. Location
    Chicago
    Posts
    19

    any more luck?

    Hi Guys,

    Just wondering if you had any more luck communicating between Max and Unity. My specific purposes would be to send data from Max to control a Unity environment, most likely the POV of the world. Any thoughts or ideas?? I'm Max proficient, but not Java or C or anything else unfortunately.

    By the way, here's what we're working on:
    www.m1interactive.net

    My goal is to add a full 3d game engine into this, with the ability to control it all without touching a control, just moving your body, etc. Any help would be much appreciated. As soon as I get this communication working, I'll be needing some guys/gals to help come up with cool new content for it as well.

    Thanks!
    Brian


  6. Location
    Vamdrup, Denmark
    Posts
    108
    Hi Brian

    I found a server script on the wiki ( http://www.unifycommunity.com/wiki/i...p?title=Server ) It works ok for my current needs, which seems to be basically identically to yours. I use the mxj net.tcp.send object for sending data from Max -> Unity


    Bjerre


  7. Location
    Chicago
    Posts
    19

    Thanks everyone, but help.

    Thanks for the great comments and suggestions/help everyone. Bjerre, any chance you can talk me through how you made this work? The force is not strong with me in this stuff. Any help would be much appreciated!


  8. Location
    Vamdrup, Denmark
    Posts
    108
    Hi Brian

    I have made this simple setup showing how i use the server script and Max/Msp
    http://www.nonrelated.com/stuff/Max2Unity.unityPackage

    Im not sure its the best way to do this, because im not exactly a Jedi when it comes to C# and .Net, im properly more of a Youngling. But it works for me. I have just finished a 10 weeks robot workshop at school where this setup was used and it worked like a dream.

    the simplified Max/Msp patch.

    Code:  
    1. max v2;
    2. #N vpatcher 6 47 660 696;
    3. #P origin 53 -16;
    4. #P window setfont "Sans Serif" 9.;
    5. #P window linecount 1;
    6. #P newex 240 144 27 196617 t b i;
    7. #P number 240 122 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
    8. #P newex 201 144 27 196617 t b i;
    9. #P number 201 122 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
    10. #P newex 164 144 27 196617 t b i;
    11. #P number 164 122 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
    12. #P newex 127 144 27 196617 t b i;
    13. #P number 127 122 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
    14. #P newex 89 144 27 196617 t b i;
    15. #P number 89 122 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
    16. #P newex 89 181 180 196617 sprintf %i:%i:%i:%i:%i;
    17. #P comment 220 251 123 196617 number of active packets;
    18. #B frgb 157 157 157;
    19. #P comment 172 268 125 196617 there is a bug somewhere;
    20. #B frgb 157 157 157;
    21. #P comment 104 288 160 196617 we successfully transmitted data;
    22. #B frgb 157 157 157;
    23. #P number 183 249 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
    24. #P newex 258 232 54 196617 print info;
    25. #P newex 108 266 63 196617 print failure;
    26. #P newex 33 286 69 196617 print success;
    27. #P newex 33 210 236 196617 mxj net.tcp.send @address 127.0.0.1 @port 9349;
    28. #P window setfont "Sans Serif" 24.;
    29. #P comment 29 58 153 196632 net.tcp.send;
    30. #P window setfont "Sans Serif" 9.;
    31. #P comment 29 93 132 196617 sends TCP packets to Unity;
    32. #P fasten 10 0 2 0 94 203 38 203;
    33. #P connect 2 0 3 0;
    34. #P connect 11 0 12 0;
    35. #P fasten 12 0 10 0 94 171 94 171;
    36. #P fasten 12 1 10 0 111 171 94 171;
    37. #P fasten 14 0 10 0 132 171 94 171;
    38. #P fasten 16 0 10 0 169 171 94 171;
    39. #P fasten 18 0 10 0 206 171 94 171;
    40. #P fasten 20 0 10 0 245 171 94 171;
    41. #P connect 2 1 4 0;
    42. #P connect 13 0 14 0;
    43. #P fasten 14 1 10 1 149 171 136 171;
    44. #P connect 15 0 16 0;
    45. #P fasten 16 1 10 2 186 171 178 171;
    46. #P connect 2 2 6 0;
    47. #P connect 17 0 18 0;
    48. #P fasten 18 1 10 3 223 171 220 171;
    49. #P connect 19 0 20 0;
    50. #P fasten 20 1 10 4 262 171 262 171;
    51. #P connect 2 3 5 0;
    52. #P pop;

    btw. I just added to line of code to the server.cs script, well two if you count comments. Check line 274 to see and line 273 to read.

    Bjerre


  9. Location
    Vamdrup, Denmark
    Posts
    108
    Hi again

    Just updated the the unity package. This should be easier to understand and it is more well documented. The link is the same.

    *Bjerre


  10. Location
    Chicago
    Posts
    19

    Thanks!

    Hey guys thanks so much for the help. Sorry for the lengthy wait to say that. I got sidetracked and haven't had much time to play lately. I'll keep reply when I have some news of some actual progress though.
    Brian


  11. Location
    Chicago
    Posts
    19

    Using the variables from Max?

    OK so I got my max patch up and running, plus the Unity project package you provided. Sorry to be ill-informed here.

    My goal is to send basically a start/stop message, plus x-y coordinates from Max. So if I send 3 variables from max, and they are received by Unity, how do I use the variables? Lets say I want an fps prefab to look left-right, up-down based on the x-y coords coming from max, and activate a particle system on the start/stop or whatever.

    I'm confused as to what the scripts do or how I should use them. I spent all weekend on this and it's driving me batty.

    Any help would be great!
    Thanks!!
    Brian


  12. Location
    Vamdrup, Denmark
    Posts
    108
    If you are using the Unity package I made, the way to access the variables is through the GetVariable function located in the ServerRead script.

    Again if you use the above package find the GameObject named getVar. If you click on the attached script named GetVariables I have made an example where if you push the up key it will print the first value sent from Max/Msp, the line you are looking for is this:

    script.GetVariable(0);

    If you want the second value from Max you simply write something like script.GetVariable(1); and so on.

    hope this information was useful, if not feel free to send an e-mail.

  13. jbm jbm is offline

    Posts
    2
    Hello, All.

    I've been working for a while on an interactive music composition app in java and max/msp. Lately I've been doing some interface work in Processing, but I'm kind of interested in something really immersive for the visual environment. It seems like a lot of the ideas I'm interested in would be a lot simpler to do in Unity than in Processing. Do any of you know whether Unity would be a good platform to create a visual environment from which to control a max/msp-based application?

    I guess I'm just asking how much you think I could do in terms of two-way communication between Unity and Max. I'm guessing that, since it's just like any app that communicates with its gui over the network, that it's the same deal in Unity... But not being a Unity user, yet, I'm looking to find out whether this might an avenue worth pursuing.

    Any and all thoughts appreciated.

    cheers,

    James.


  14. Location
    Chicago
    Posts
    19

    Interactive

    Sounds like you might be better using Jitter. Use it to get data from a camera or sensor, then apply it to a n MSP synth patch or something. Funny, I'm trying to go the other direction, sending motion data, extracted from camera movement to Unity. This way I can use Unity's graphic engine to build 3d worlds and have them controlled through Jitter (Max/MSP)

    Now if we could only get motion tracking data directly through Unity itself...

  15. jbm jbm is offline

    Posts
    2
    Well, I'm using Processing right now, which can communicate with Max over the network. It's going okay this way, but it just sounds as though there would be a lot more "free" structure provided by Unity - everything has to be done basically by hand, from scratch, in Processing.

    Besides that, I'm really interested in taking some of the general paradigms from game design and applying them to music representation and control. I think there's a lot of untapped potential there, but I just need to find the right platform for the job.

    If anybody else has any comments or thoughts, I'd appreciate it. I think I'll download the trial anyway, but whatever info could help my decision would be great.

    J.


  16. Location
    Antwerp
    Posts
    19
    Are there any developments on this since the last post?

    I've tried Bjerre's example project and it works, though after some time I get an error, and unity crashes. It says "SocketException: Too many open files"... Can someone shed some light on this?

    cheers,
    brecht.


  17. Location
    Zürich, Switzerland
    Posts
    26,572
    you can not have more than 255 open files at a time so files that are not needed anymore must be closed again


  18. Location
    Antwerp
    Posts
    19
    I don't really know all the much of networking, and I am a newbie with unity (going through my 3rd day of the trial).
    The project uses the server.cs script to handle the connection, but I don't understand which "files" are open?
    When, indeed, 255 messages are sent from max to unity, the crash occurs.


  19. Location
    Chicago
    Posts
    1
    brecht
    I just joined the forum (this is my first post!) and thought I'd just chime in here...
    TCP/UDP sockets are handled as files
    This http://www.amk.ca/python/howto/sockets/ from Python (which of course Boo is based on) might help explain a few things.


  20. Posts
    10
    Hi,

    I have also been toying around with max/msp and unity, and getting them to connect to each other.

    To circumvent the problem with the 255 sockets, I used socket.close(); to properly close off the sockets after use. However, while this does mean that max runs for longer, it never quite fixes the problem. Instead, the connection seems to die after a few minutes.

    I am using the C# server posted on the wiki (that Bjerre refered to) for this.

    When I set the game to output the debug info, it appears to be using a whole bunch of ports, and I assume it dies when it comes across a port it is no longer capable of using? After a while, when the server dies, I get the "Error in Server :: DoIO()" error. I've attached a picture displaying the debug info. Anyone have any idea on how to, perhaps, reuse the old ports?

    I have also tried using socket.Disconnect(true) after closing the socket, but it just seems to force the error to appear quicker.

    Man, I hate networking.
    Attached Images  

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •