Unity-Technologies / ml-agents

The Unity Machine Learning Agents Toolkit (ML-Agents) is an open-source project that enables games and simulations to serve as environments for training intelligent agents using deep reinforcement learning and imitation learning.
https://unity.com/products/machine-learning-agents
Other
17.2k stars 4.16k forks source link

Unable to establish connection between python and Unity #4490

Closed AlePe93 closed 4 years ago

AlePe93 commented 4 years ago

Hi, first of all i want to explain my goal: what i'm trying to do is making an example that can establish a connection between unity and python in order to exchange messages. Because after that i'd like to make a program in python that, according to the result of the python program something happens in unity ( for instance: if python prompt "a", in unity will appear a red sphere. if python prompt "b" it will appear in unity a blue cube and so on.) First question is: am i following the right path in order to achieve my objective by reading this guide https://github.com/Unity-Technologies/ml-agents/blob/master/docs/Custom-SideChannels.md ?

If yes then i'll explain what i did and what error i got.

Steps i did in Unity:

  1. Create a unity project;
  2. Create under Project -> Assets a class called StringLogSideChannel and copied the code as in the guide;
  3. Create under Project -> Assets a MonoBehaviour class called RegisterStringLogSideChannel and copied the code;
  4. Create in the scene an empty GameObject and add a component script. The script added is the class in step 3.

Steps i did in python:

  1. Create in Anaconda a python 3.8 environment
  2. Install in that environment the package mlagents by doing the "pip install mlagents".
  3. In PyCharm create a project with a main.py class which will use the environment created in step 2;
  4. Copy the python code as exaplined in the guide in the class.

Then, i runned first the python program and then pressed play in Unity and got the error in the python console reported below. I also switched the running order: i first pressed play in Unity then runned the python program.

Thanks in advance for the help

The error i get is this one:


Traceback (most recent call last):
  File "C:/Users/MyName/PycharmProjects/python-unity/main.py", line 35, in <module>
    env = UnityEnvironment(side_channels=[string_log])
  File "C:\Users\MyName\Anaconda3\envs\env_Python-Unity\lib\site-packages\mlagents_envs\environment.py", line 211, in __init__
    aca_output = self._send_academy_parameters(rl_init_parameters_in)
  File "C:\Users\MyName\Anaconda3\envs\env_Python-Unity\lib\site-packages\mlagents_envs\environment.py", line 455, in _send_academy_parameters
    return self._communicator.initialize(inputs)
  File "C:\Users\MyName\Anaconda3\envs\env_Python-Unity\lib\site-packages\mlagents_envs\rpc_communicator.py", line 104, in initialize
    self.poll_for_timeout()
  File "C:\Users\MyName\Anaconda3\envs\env_Python-Unity\lib\site-packages\mlagents_envs\rpc_communicator.py", line 96, in poll_for_timeout
    raise UnityTimeOutException(
mlagents_envs.exception.UnityTimeOutException: The Unity environment took too long to respond. Make sure that :
     The environment does not need user interaction to launch
     The Agents' Behavior Parameters > Behavior Type is set to "Default"
     The environment and the Python interface have compatible versions.

Process finished with exit code 1 
sini commented 4 years ago

If I understand correctly, you do not have an agent in your scene. Is that correct?

Side channel messages are buffered and only sent when an agent step is triggered, in which case it will send any buffered messages. It is not intended for general RPC with Python and does not run in heuristic or model-based modes, only in training -- though a feature request can be made to expand the scope of the API if that's what you're after.

AlePe93 commented 4 years ago

If I understand correctly, you do not have an agent in your scene. Is that correct?

Side channel messages are buffered and only sent when an agent step is triggered, in which case it will send any buffered messages. It is not intended for general RPC with Python and does not run in heuristic or model-based modes, only in training -- though a feature request can be made to expand the scope of the API if that's what you're after.

Hi, thanks for the reply! I appreciate it. Yes i do not have an agent in my project because what i'm trying to do is just connect the two environments: Python and Unity. First i'd like to ask you this: since my goal is to do some basic "if result = a" (in python) then "something happens" (in Unity) could you suggest me what could be a possibile solution?

Second if there's no answer to my first question, i'm gonna make a feature request in order to achieve my goal!

Thanks in advance for feature help.

sini commented 4 years ago

If your goal is simple RPC with a Python component it would probably be best to implement your own socket server and channel. If your objective is just to write logic in Python within your Unity project you might want to follow this project: https://docs.unity3d.com/Packages/com.unity.scripting.python@2.0/manual/index.html

AlePe93 commented 4 years ago

Thank you for the reply. I tried the second example you suggested me but sadly i'm working in python which has some libraries that require version at least 3.6, what you linked works only with python 2. I appreciate anyway the help.

Have a good day!

github-actions[bot] commented 3 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.