-
-
Save mattexdee/19f5ae486ad00c33dd3d1508278bd2c9 to your computer and use it in GitHub Desktop.
A macro script to convert polygonal drawings to walls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let drawings = canvas.drawings.controlled; | |
drawings = drawings.map(drawing =>{ | |
switch (drawing.data.type) { | |
case "f": | |
case "p": { | |
let { _id, points, rotation, x, y, width, height } = drawing.data; | |
return { id: _id, valid: true, points, rotation, x, y, width, height }; | |
} | |
case "r": { | |
let { _id, rotation, x, y, width, height, strokeWidth } = drawing.data; | |
const points = [ | |
[0 + (strokeWidth / 2), 0 + (strokeWidth / 2)], | |
[width - (strokeWidth / 2), 0 + (strokeWidth / 2)], | |
[width - (strokeWidth / 2), height - (strokeWidth / 2)], | |
[0 + (strokeWidth / 2), height - (strokeWidth / 2)], | |
[0 + (strokeWidth / 2), 0 + (strokeWidth / 2)] | |
]; | |
return { id: _id, valid: true, points, rotation, x, y, width, height }; | |
} | |
default: | |
return { id: drawing.data._id, valid: false }; | |
} | |
}).filter(drawing => { | |
if (!drawing.valid) { | |
ui.notifications.warn(`Drawing "${drawing.id}" is not a valid drawing type!`); | |
return false; | |
} | |
return true; | |
}); | |
if (drawings.length) { | |
const newWalls = drawings.flatMap((drawing) => { | |
const { x, y, width, height } = drawing; | |
const xCenterOffset = width / 2; | |
const yCenterOffset = height / 2; | |
const θ = Math.toRadians(drawing.rotation); | |
const cosθ = Math.cos(θ); | |
const sinθ = Math.sin(θ); | |
const points = drawing.points.map((point) => { | |
const offsetX = point[0] - xCenterOffset; | |
const offsetY = point[1] - yCenterOffset; | |
const rotatedX = (offsetX * cosθ - offsetY * sinθ); | |
const rotatedY = (offsetY * cosθ + offsetX * sinθ); | |
return [rotatedX + x + xCenterOffset, rotatedY + y + yCenterOffset]; | |
}); | |
return points | |
.slice(0, points.length - 1) | |
.map((point, i) => ({ c: point.concat(points[i + 1]) })); | |
}); | |
canvas.scene.createEmbeddedDocuments("Wall", newWalls); | |
canvas.walls.activate(); | |
} else { | |
ui.notifications.error("No drawings selected!"); | |
} |
@KageJittai Had the same problem. Reworked script a little bit:
https://gist.github.com/mrjackphil/49a435432458f7fcf7844a405dae0d59
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This macro no longer works on Foundry v10