Script for nozzle changer

111 views
Skip to first unread message

svetozar161

unread,
Jun 25, 2026, 5:35:30 PMJun 25
to OpenPnP
Hello. I'm having a problem changing nozzles in the nozzle changer. Since I can't fully immerse the changer in the table due to the table's design, moving the head may cause collisions with other nozzles. I need a script that would send the head to the middle of the X and Y coordinates before changing and then move it to change the nozzles, where there are no obstacles. Unfortunately, I'm not a programmer and can't write the script. Can anyone help me? I've tried using AI, but it doesn't work. I've already broken my head over this. Thank you.

svetozar161

unread,
Jun 26, 2026, 10:18:48 AMJun 26
to OpenPnP
This is script is not worked

// NozzleTip.BeforeUnload.js 
var safeX = 270;
var safeY = 100;
var feedRate = 1000;
var gcode = "";
gcode += "G1 X" + safeX + " Y" + safeY + " F" + feedRate + "\n";
try
{ var driver = machine.getDriver();
driver.sendGCode(gcode);
}
catch (e)
 { print("Error sending G‑code: " + e.toString()); }  
пятница, 26 июня 2026 г. в 00:35:30 UTC+3, svetozar161:

Toby Dickenson

unread,
Jun 26, 2026, 10:50:46 AMJun 26
to ope...@googlegroups.com
You dont want to be sending GCode directly - that is far too low level and will confuse openpnp if you move the machine behinds it back. Your script needs to be using the openpnp api to move the head.

Openpnp comes with some sample scripts. One of those is called move_machine.py, or move_machine.js depending on your language preference. That already does more than what you need:

1. That script is intended to be triggered from a menu, so it has code to switch into the machine control thread. Your NoddleTip.BeforeUnload event script will already be in the machine thread, so it can delete that code

2. That script finds the existing nozzle position, and adds an offset. If your script uses a fixed location then it does not need this calculation.

Something like this should work:

from org.openpnp.model import LengthUnit, Location
nozzle = machine.defaultHead.defaultNozzle
location = Location(LengthUnit.Millimeters, 270, 100, None, None)
nozzle.moveTo(location)


--
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.
To view this discussion visit https://groups.google.com/d/msgid/openpnp/09eaff7a-8d21-490e-9bca-007250209ee1n%40googlegroups.com.

svetozar161

unread,
Jun 26, 2026, 2:23:55 PMJun 26
to OpenPnP

Good. I will try this script and write the result
пятница, 26 июня 2026 г. в 17:50:46 UTC+3, to...@tarind.com:

svetozar161

unread,
Jun 26, 2026, 3:37:07 PMJun 26
to OpenPnP
I created a script named NozzleTip.BeforeUnload.1.js and saved it in the Events folder
The script is as follows:
load(scripting.getScriptsDirectory().toString() + '/Events/NozzleTip.BeforeUnload.1.js');
var imports = new JavaImporter(org.openpnp.model, org.openpnp.util);
with (imports) {
task(function() {
var nozzle = machine.defaultHead.defaultNozzle;
var location = nozzle.location;
                location = location.add(new Location(LengthUnit.Millimeters, 270, 100, 0, 0));
nozzle.moveTo(location);
});
}
but when I click the "unload nozzle" button, nothing happens. Here is the log file:

2026-06-26 22:30:45.991 ReferenceNozzle DEBUG: N2.unloadNozzleTip(): Start
2026-06-26 22:30:45.991 Scripting TRACE: Scripting.on NozzleTip.BeforeUnload
2026-06-26 22:30:45.992 Scripting TRACE: Scripting.on found NozzleTip.BeforeUnload.1.js
2026-06-26 22:30:45.992 Scripting TRACE: org.openjdk.nashorn.api.scripting.NashornScriptEngine@651d0f68 scripting engine borrowed from pool in 0.0594 milliseconds
2026-06-26 22:30:46.535 Scripting TRACE: Script NozzleTip.BeforeUnload.1.js executed in 539.3156 milliseconds
2026-06-26 22:30:46.535 AbstractMachine TRACE: Machine entering idle state.
Is there a mistake somewhere?
пятница, 26 июня 2026 г. в 21:23:55 UTC+3, svetozar161:

bert shivaan

unread,
Jun 29, 2026, 7:50:22 AMJun 29
to ope...@googlegroups.com
Are you positive you are allowed to add the .1 to the script name? I assume you can since it appears to have tried to run it. Just checking the silly things.

Toby Dickenson

unread,
Jun 29, 2026, 9:10:39 AMJun 29
to ope...@googlegroups.com
On Mon, 29 Jun 2026 at 12:50, bert shivaan <bert.s...@gmail.com> wrote:
>
> Are you positive you are allowed to add the .1 to the script name? I assume you can since it appears to have tried to run it. Just checking the silly things.

Yes, as of openpnp 2.6 your script event files can be named
NozzleTip.BeforeUnload.ANYTHING.YOU.WANT.IN.HERE.js, and it will run
every matching script it finds. This is great for modularity and
cohesion; if you have two different reasons to catch the same
scripting event, then each feature can be in a separate script file.

On Fri, Jun 26, 2026 at 3:37 PM svetozar161 <sveto...@gmail.com> wrote:

> task(function() {

The "task" function is for running the action in the machine thread.
You dont need that.

> load(scripting.getScriptsDirectory().toString() + '/Events/NozzleTip.BeforeUnload.1.js');

Your line beginning "load" was copied from the examples directory. In
that example it was referencing the Utility.js script, here you
changed it to reference your own script. Utility.js is what provides
the task() function, so we dont need any of thise.

>> var location = nozzle.location;
>> location = location.add(new Location(LengthUnit.Millimeters, 270, 100, 0, 0));

Here you are trying to move the machine to a location (270,100)
**relative to its current location**
I strongly suspect that you want (270,100) as an absolution location. Right?

This is entirely untested, and I am not too familiar with writing
openpn scripts in js. But I think you need something like this:

var imports = new JavaImporter(org.openpnp.model, org.openpnp.util);
with (imports) {
var nozzle = machine.defaultHead.defaultNozzle
var location = new Location(LengthUnit.Millimeters, 270, 100, 0, 0);
nozzle.moveTo(location);
}
Reply all
Reply to author
Forward
0 new messages