Can we direct call python script directly without GUI?

162 views
Skip to first unread message

Leonard Cheng

unread,
Feb 27, 2023, 10:18:44 AM2/27/23
to OpenPnP
Hello everyone,

I am currently working on Openpnp pick and place machine project in company.
We want to operate machine from our program(python script).
There are several python example in openpnp. Like "move_machine.py".
If I use this example( "move_machine.py" ) from Openpnp GUI. Then the machine can move without any problem.
But I tried to call this py file(move_machine.py ) directly from our script. It shows

   java.lang.NullPointerException: java.lang.NullPointerException

I guess it is because I didn't use Openpnp GUI, therefore, I got this NullPointer exception.
Is it possible to call python script without GUI?

Any advice will be appreciated.
Thank you very much in advance.
move_machine.py

Litterio Andrea Guainella

unread,
Feb 27, 2023, 5:25:42 PM2/27/23
to OpenPnP
Hi,
I hope I'm not talking nonsense.
Python based scripting (inside openpnp) is provided by jython (as you can see here) so you should use module version inside openpnp ("C:\Program Files\openpnp\lib\jython-standalone-2.7.1.jar") then add "openpnp-gui-0.0.1-alpha-SNAPSHOT.jar" to "sys.path". (EX: java -jar "C:\Program Files\openpnp\lib\jython-standalone-2.7.1.jar" -Dpython.path="C:\Program Files\openpnp\openpnp-gui-0.0.1-alpha-SNAPSHOT.jar")
So you can try to launch your script. (Just remember to set and homing machine before launch script) (EX: java -jar "C:\Program Files\openpnp\lib\jython-standalone-2.7.1.jar" -Dpython.path="C:\Program Files\openpnp\openpnp-gui-0.0.1-alpha-SNAPSHOT.jar" "path/your/script.py")
I hope someone more knowledgeable than me can help you.

LAG

Leonard Cheng

unread,
Mar 1, 2023, 5:09:21 AM3/1/23
to OpenPnP
Hello,
thanks for your reply, I tried your suggestion as follows:

java -jar C:\Users\CHH9RT\.m2\repository\org\python\jython-standalone\2.7.1\jython-standalone-2.7.1.jar  -Dpython.path="C:\Users\CHH9RT\AppData\Local\Programs\openpnp\openpnp-gui-0.0.1-alpha-SNAPSHOT.jar"  C:\Users\CHH9RT\.openpnp2\scripts\Examples\Python\move_machine.py

Then I got import error as follows:
                              Traceback (most recent call last):
                                   File "C:\Users\CHH9RT\.openpnp2\scripts\Examples\Python\move_machine.py", line 9, in <module>
                                   from org.openpnp.util.UiUtils import submitUiMachineTask
                              ImportError: No module named util


move_machine.py: is the default py file from openpnp(the path of this file is also default), I can directly use it from opene GUI(Script -> Examples -> Python -> move_machine.py ).
                                  But why I got this error ? All the artifacts(from Maven) should be there in my laptop, otherwise, It wouldn't work with openpnp GUI.

Again, thank you so much for your advices, I am really appreciated. I have stuck here around two weeks. I really need to find out a solution for it.

Best regards,
Leonard

Jason von Nieda

unread,
Mar 1, 2023, 12:34:20 PM3/1/23
to OpenPnP
Hi Leonard,

You can't call OpenPnP scripts outside of the OpenPnP runtime. This is because all of the objects that OpenPnP provides are handed to the Python execution engine at runtime. So, if you try to run one of these scripts that refers to OpenPnP outside of OpenPnP then none of the OpenPnP "stuff" is available.

Can you provide some more detail on what you are trying to do? There may be other ways to do it, but they will most likely require writing a little bit of Java.


Jason
--
You received this message because you are subscribed to the Google Groups "OpenPnP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openpnp+u...@googlegroups.com.

Leonard Cheng

unread,
Mar 2, 2023, 7:14:56 AM3/2/23
to OpenPnP
Hi Jason,

Thanks for your reply.

we want to use this machine for the testing of our sensors in our lab.
All I need now is simply move the machine to the corresponding x,y, and then goes down(Z direction) to touch our devices(no "pick and place" needed, just touch them).
Because we want to measure whether the external pressure will impact the performance of our sensors.

If this way works, then we will do production for this, but operatoring workers in production line, they don't know about the Openpnp GUI.
That is why we want to make it automatically.  We have our program to do testing, but we still want to call/operate this machine to the corresponding positions from our program(without GUI).

Therefore, I think " move_machine.py" is the perfect practice for my trials. Unfortunately, I haven't succeeded yet, that is why I come here to ask for the help.

I also wrote some java code to initialize the Configuration because I got error as follows:
    submitUiMachineTask(move_nozzle)
at org.openpnp.model.Configuration.get(Configuration.java:117)
at org.openpnp.util.UiUtils.submitUiMachineTask(UiUtils.java:100)
at org.openpnp.util.UiUtils.submitUiMachineTask(UiUtils.java:85)
at org.openpnp.util.UiUtils.submitUiMachineTask(UiUtils.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
java.lang.Error: java.lang.Error: Configuration instance not yet initialized.
 
Now I can initialize the Configuration(the error is gone), but then I got this:
   
java.lang.ClassNotFoundException: Configuration
at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at org.openpnp.model.ConfigurationInitialize.loadMachineFromXml(ConfigurationInitialize.java:94)
at org.openpnp.model.ConfigurationInitialize.tryIni(ConfigurationInitialize.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:190)
at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:208)
at org.python.core.PyObject.__call__(PyObject.java:461)
at org.python.core.PyObject.__call__(PyObject.java:465)
at org.python.core.PyMethod.__call__(PyMethod.java:126)
at org.python.pycode._pyx0.move_nozzle$3(C:/Users/CHH9RT/JavaTry/move_machine.py:58)
at org.python.pycode._pyx0.call_function(C:/Users/CHH9RT/JavaTry/move_machine.py)
at org.python.core.PyTableCode.call(PyTableCode.java:173)
at org.python.core.PyBaseCode.call(PyBaseCode.java:119)
at org.python.core.PyFunction.__call__(PyFunction.java:406)
at org.python.pycode._pyx0.main$1(C:/Users/CHH9RT/JavaTry/move_machine.py:24)
at org.python.pycode._pyx0.call_function(C:/Users/CHH9RT/JavaTry/move_machine.py)
at org.python.core.PyTableCode.call(PyTableCode.java:173)
at org.python.core.PyBaseCode.call(PyBaseCode.java:119)
at org.python.core.PyFunction.__call__(PyFunction.java:406)
at org.python.pycode._pyx0.f$0(C:/Users/CHH9RT/JavaTry/move_machine.py:60)
at org.python.pycode._pyx0.call_function(C:/Users/CHH9RT/JavaTry/move_machine.py)
at org.python.core.PyTableCode.call(PyTableCode.java:173)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1703)
at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:297)
at org.python.util.jython.runSimpleFile(jython.java:417)
at org.python.util.jython.runStream(jython.java:449)
at org.python.util.jython.runFile(jython.java:474)
at org.python.util.jython.main(jython.java:678)

Traceback (most recent call last):
  File "C:/Users/CHH9RT/JavaTry/move_machine.py", line 60, in <module>
    main()
  File "C:/Users/CHH9RT/JavaTry/move_machine.py", line 24, in main
    move_nozzle()
  File "C:/Users/CHH9RT/JavaTry/move_machine.py", line 34, in move_nozzle
    nozzle = machine.defaultHead.defaultNozzle
AttributeError: 'NoneType' object has no attribute 'defaultHead'

I am very sorry to paste all the error message here, because I cannot upload the photos nor pictures to here from my company laptop.
Any ideas for this? Or any other ideas are also very welcome and appreciated.
Or maybe we can also use Openpnp GUI in production environment, but fill in or operate everything automatically?
Thanks a lot!!

Leonard
Reply all
Reply to author
Forward
0 new messages