Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

[Script] PathEditor - Move/add/delete pts, split paths, and more!

1651537051

Edited 1652306224
David M.
Pro
API Scripter
Summary: Have you ever made a complex dynamic lighting path and realized after the fact that you wanted to tweak it, or you made some mistake that needed correcting after the path was already committed? Previously, your options included (1) delete the path and start over, or (2) live with it. [Sad trombone] This simple path editing tool allows GMs to alter the characteristics of an already existing path directly on the VTT! A chat menu interface (a macro that is installed automatically) contains all of the commands necessary to edit your paths. Code: The code is currently found here for manual install: &nbsp; <a href="https://github.com/djmoorehead/PathEditor/blob/main/PathEditor.js" rel="nofollow">https://github.com/djmoorehead/PathEditor/blob/main/PathEditor.js</a> !!Note!!: due to some annoying interaction with object creation and the object change listener, when you first install it you will get a pink error message in your api console. Just restart the sandbox and should go away and never return. I suspect this is a timing issue, but I'm currently stumped. If one of the script gurus has an idea, I'm all ears! Example Animation: Here's an example of the script in action with a simple path element (click to play gif) How it works "under the hood": On install, the script will create a character named PathEditPt , and a collections macro named PathEditMenu . The character has a multi-sided default token (black &amp; white circles), and a bunch of macro abilities that control script behavior. When you select an existing path and click on the " Edit " button in the chat menu, the script identifies all of the vertices of the path and spawns copies of the PathEditPt token at each point. (values are written tot he state object that links the path with its associated control points). The color (side) of the point is chosen automatically based on the color of the path for enhanced contrast. The script then listens for movement of these control points and/or the path itself and redraws a new path with the new point locations. This is required because the actual "path" property of a path object is only editable at object creation and read-only any time later. When you are done editing, just click the " Done " button and the control points will be deleted. Note: before somebody asks about Bezier curves, they unfortunately aren't supported by the Roll20 Dynamic Lighting engine as of now. Ovals are the only exception (UDL only). However, Oval editing is not currently supported by this script! &nbsp;If you try, the script will turn them into straight-edged polygons based on the bezier pt locations. You have been warned. Note 2: Technically the script will work with freehand paths, but I wouldn't recommend it. Freehand paths create&nbsp; a lot &nbsp;of points! It will be difficult to manipulate the right point even at the smallest control pt size. That gives me an idea for a future feature, though: A way to simplify a path by reducing the number of points by some amount or percentage. Hmm... Features: Add point &nbsp;- Select any two adjacent control pts and click " Add " Delete point &nbsp;- Select one or more control pts and click " Delete " Split path - Two modes: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For a closed path (start pt connected to end pt), select any two control pts and click " Split ". &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For an open path (start pt not connected to end pt), you can click any number of control pts and click " Split ". Control point management&nbsp; - &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp; Resize - the " Shrink ", " Grow ", and " Set Size " buttons do the work. The latter is in pixels. The default size is 20 pixels. Shrink &amp; grow alter the size by 10px up or down. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp; Toggle Color - This button flips the side of all control pts to show black or white circles &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp; Toggle Grid Snapping - Turning on grid snapping will make the control pts snap to grid intersections when moved . Note this setting is stored in the state object and will be remembered across sessions! Open/close Polygon &nbsp;- This will attach/detach the first and last points of the path. It may not be obvious at first when opening a path, as the control pts will overlap each other. This feature is useful for the current LDL bug that causes a phantom diagonal line on the map when closed paths are used. That's about it! There are a host of api commands that run this show, but I won't go into them quite yet. I'll reserve the next post to include at a later date if needed. I think the chat menu is going to be the easiest way to navigate though.&nbsp; This has been a fun project. I hope somebody gets some use out of it! Version log: v0.1 - Initial release v0.2 - prevents ctrl pt resizing, deleting path deletes ctrl pts, entering edit mode for new path while editing old path will delete ctrl pts of old path editing v0.3 &nbsp;-&nbsp;added path simplification capability. Useful for freehand paths with a ton of pts!
1651537061
David M.
Pro
API Scripter
[Reserved]
Really cool ! and so useful !
1651581792

Edited 1651582384
David M.
Pro
API Scripter
I forgot to mention this in my original post, but technically the script will work with freehand paths. I just wouldn't recommend it. Freehand paths create&nbsp; a lot &nbsp;of points! Note that the slower you paint, the more points there will be. It will likely be difficult to manipulate the intended point even at the smallest control pt size. And at some point it is possible that it may bog down or cause a false "infinite loop" timeout error. That gives me an idea for a future feature, though: A way to simplify a path by reducing the number of points by some amount or percentage. Hmm... Original post has been updated with this info.
1651729434
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
This is pretty dang sweet, David! I will add this to my home game tool box. I sent you a PM with some suggestions.
1652095167
David M.
Pro
API Scripter
Version update - v0.2 - found here A couple of minor revisions based on input from Keith: When a path is being edited and you attempt to start editing a new path, the script now deletes the control pts of the old path (previously you needed to click "done" on the old path first) Prevents the accidental resizing of control pts when trying to move them When you delete a path while it is being actively edited, the control pts are also deleted
1652102638
David M.
Pro
API Scripter
Side note: I ran my game last Friday and decided to try out this script to implement a magically shifting hedge maze style puzzle encounter (dynamic lighting only, not tied to graphics so we had to use our imaginations, gasp). Went pretty smoothly - I created the original maze as a bunch of separate paths (~2-6 vertices each) snapped to grid. I kept GM view on the DL layer most of the time so I could easily edit the paths while I could still see the translucent pc tokens. Kept the grid snapping option on so things would line up without unintended gaps. Think it went over well. Would have been easier if multiple paths could have been edited simultaneously - when the players were rolling in chat I had to keep refreshing the editor chat menu. I may look into that in a future version.
1652103995
Kraynic
Pro
Sheet Author
With something like that, it may be beneficial to store your commands on a handout, or trigger macro mule commands from a handout.&nbsp; Then it doesn't matter how fast chat moves.
1652106351
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Kraynic said: With something like that, it may be beneficial to store your commands on a handout, or trigger macro mule commands from a handout.&nbsp; Then it doesn't matter how fast chat moves. That's probably too specific a use to qualify for a Stupid Trick, but it should be one. I use that approach as an option for the Report Script.
1652306122
David M.
Pro
API Scripter
Version update - v0.3 - Path Simplification! - found&nbsp; here This new feature (the "SIMPLIFY" button at the bottom of the PathEditMenu macro) uses a polyline simplification algorithm (Ramer-Douglas-Peucker: thanks to TheAaron for directing me to this algorithm!) which has the potential to dramatically reduce the number of points in a given path while preserving the basic shape of that path. It probably won't do that much for paths drawn with the polygon/line tool, but freehand drawings will see a huge drop. The new button will query for a "tolerance" (with a default value of 5 pixels). Roughly, any "wiggles" in a curve that vary from a straight line by less than this amount will be straightened out. If you input a lower number, you will get a smoother "curve" with more points and vice versa. Note that if the freehand path has lots of tight radius curves, there is the potential for the new path to curve on itself if the tolerance is too high, so I'd start with the default.&nbsp;&nbsp; Here's an example of a complex freehand curve being simplified. In this example I copied the curve, showed the huge number of points in the original (takes a couple secs to load), and then simplified the copy. Click to play animated gif. The chat menu macro and PathEditPt character sheet abilities should automatically update themselves if you already have the script installed.
1652309824
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
This just keeps getting better!
1652321045
The Aaron
Roll20 Production Team
API Scripter
Next up, Bézier curve fitting and control points... &nbsp;=D
1652323165
David M.
Pro
API Scripter
Bezier curves were on my initial radar, but it doesn't seem like they are actually fully supported by dynamic lighting :/&nbsp; Looks like only ovals are supported (and only by UDL). In testing I created a simple curve with the following path coords: [[\"M",0,0],[\"Q\",140,70,70,140],[\"L\",210,210]] Dynamic lighting did not follow the actual curve and instead treated the path as straight line segments based on the bezier control pts.&nbsp;
1652324550
The Aaron
Roll20 Production Team
API Scripter
Well, you could provide the controls for a fit&nbsp;Bézier curve for manipulation, then create it just like a freehand line behind the scenes. =D
1652326093
David M.
Pro
API Scripter
[covers ears] Lalalalalalala I can't hear you lol