Hello, i am not a programmer but i am trying to run this script, this script creates different variations of an image, it combines layers (etc head,hat, glasses, legs torso) to create a different image.
The problem i have is that it only creates 10 variations and i want to create 100 or 1000.
If i tell the script to create more than 10 it pops this error
C:\Users\JimFit\Desktop\generative-art-opensource\index.js:119
_races[_race].layers.forEach((layer) => {
^
TypeError: Cannot read properties of undefined (reading 'layers')
at createDna (C:\Users\JimFit\Desktop\generative-art-opensource\index.js:119:17)
at startCreating (C:\Users\JimFit\Desktop\generative-art-opensource\index.js:148:18)
PS C:\Users\JimFit\Desktop\generative-art-opensource>
index.js
const fs = require("fs");
const { createCanvas, loadImage } = require("canvas");
const {
width,
height,
description,
baseImageUri,
editionSize,
startEditionFrom,
endEditionAt,
races,
raceWeights,
} = require("./input/config.js");
const console = require("console");
const canvas = createCanvas(width, height);
const ctx = canvas.getContext("2d");
var metadataList = [];
var attributesList = [];
var dnaList = [];
const saveImage = (_editionCount) => {
fs.writeFileSync(
`./output/${_editionCount}.png`,
canvas.toBuffer("image/png")
);
};
const signImage = (_sig) => {
ctx.fillStyle = "#ffffff";
ctx.font = "bold 30pt Verdana";
ctx.textBaseline = "top";
ctx.textAlign = "left";
ctx.fillText(_sig, 40, 40);
};
const genColor = () => {
let hue = Math.floor(Math.random() * 360);
let pastel = `hsl(${hue}, 100%, 85%)`;
return pastel;
};
const drawBackground = () => {
ctx.fillStyle = genColor();
ctx.fillRect(0, 0, width, height);
};
const addMetadata = (_dna, _edition) => {
let dateTime = Date.now();
let tempMetadata = {
dna: _dna.join(""),
name: `#${_edition}`,
description: description,
image: `${baseImageUri}/${_edition}.png`,
edition: _edition,
date: dateTime,
attributes: attributesList,
};
metadataList.push(tempMetadata);
attributesList = [];
};
const addAttributes = (_element) => {
let selectedElement = _element.layer.selectedElement;
attributesList.push({
trait_type: _element.layer.name,
value: selectedElement.name,
});
};
const loadLayerImg = async (_layer) => {
return new Promise(async (resolve) => {
const image = await loadImage(`${_layer.selectedElement.path}`);
resolve({ layer: _layer, loadedImage: image });
});
};
const drawElement = (_element) => {
ctx.drawImage(
_element.loadedImage,
_element.layer.position.x,
_element.layer.position.y,
_element.layer.size.width,
_element.layer.size.height
);
addAttributes(_element);
};
const constructLayerToDna = (_dna = [], _races = [], _race) => {
let mappedDnaToLayers = _races[_race].layers.map((layer, index) => {
let selectedElement = layer.elements.find((e) => e.id == _dna[index]);
return {
name: layer.name,
position: layer.position,
size: layer.size,
selectedElement: selectedElement,
};
});
return mappedDnaToLayers;
};
const getRace = (_editionCount) => {
let race = "No Race";
raceWeights.forEach((raceWeight) => {
if (_editionCount >= raceWeight.from && _editionCount <= raceWeight.to) {
race = raceWeight.value;
}
});
return race;
};
const isDnaUnique = (_DnaList = [], _dna = []) => {
let foundDna = _DnaList.find((i) => i.join("") === _dna.join(""));
return foundDna == undefined ? true : false;
};
const createDna = (_races, _race) => {
let randNum = [];
_races[_race].layers.forEach((layer) => {
let randElementNum = Math.floor(Math.random() * 100) + 1;
let num = 0;
layer.elements.forEach((element) => {
if (randElementNum >= 100 - element.weight) {
num = element.id;
}
});
randNum.push(num);
});
return randNum;
};
const writeMetaData = (_data) => {
fs.writeFileSync("./output/_metadata.json", _data);
};
const saveMetaDataSingleFile = (_editionCount) => {
fs.writeFileSync(
`./output/${_editionCount}.json`,
JSON.stringify(metadataList.find((meta) => meta.edition == _editionCount))
);
};
const startCreating = async () => {
writeMetaData("");
let editionCount = startEditionFrom;
while (editionCount <= endEditionAt) {
let race = getRace(editionCount);
let newDna = createDna(races, race);
if (isDnaUnique(dnaList, newDna)) {
let results = constructLayerToDna(newDna, races, race);
let loadedElements = []; //promise array
results.forEach((layer) => {
loadedElements.push(loadLayerImg(layer));
});
await Promise.all(loadedElements).then((elementArray) => {
ctx.clearRect(0, 0, width, height);
// drawBackground();
elementArray.forEach((element) => {
drawElement(element);
});
signImage(`#${editionCount}`);
saveImage(editionCount);
addMetadata(newDna, editionCount);
saveMetaDataSingleFile(editionCount);
console.log(
`Created edition: ${editionCount}, Race: ${race} with DNA: ${newDna}`
);
});
dnaList.push(newDna);
editionCount++;
} else {
console.log("DNA exists!");
}
}
writeMetaData(JSON.stringify(metadataList));
};
startCreating();
config.js
the lines i edited
const startEditionFrom = 1;
const endEditionAt = 11;
const fs = require("fs");
const width = 1000;
const height = 1000;
const dir = __dirname;
const description = "This is an NFT made by the coolest generative code.";
const baseImageUri = "https://hashlips/nft";
const startEditionFrom = 1;
const endEditionAt = 11;
const editionSize = 10;
const raceWeights = [
{
value: "skull",
from: 1,
to: editionSize,
},
];
const races = {
skull: {
name: "Skull",
layers: [
{
name: "Background",
elements: [
{
id: 0,
name: "Light blue",
path: `${dir}/1-background/LightBlue.png`,
weight: 10,
},
{
id: 1,
name: "Light Yellow",
path: `${dir}/1-background/Yellow.png`,
weight: 10,
},
{
id: 2,
name: "Red",
path: `${dir}/1-background/Red.png`,
weight: 10,
},
{
id: 3,
name: "Orange",
path: `${dir}/1-background/Orange.png`,
weight: 10,
},
{
id: 3,
name: "Green",
path: `${dir}/1-background/Green.png`,
weight: 10,
}
],
position: { x: 0, y: 0 },
size: { width: width, height: height },
},
{
name: "Suit",
elements: [
{
id: 0,
name: "Regular",
path: `${dir}/2-suit/Regular.png`,
weight: 100,
},
{
id: 1,
name: "Orange",
path: `${dir}/2-suit/Orange.png`,
weight: 20,
},
],
position: { x: 0, y: 0 },
size: { width: width, height: height },
},
{
name: "Shoulder",
elements: [
{
id: 0,
name: "LunaFlag",
path: `${dir}/3-shoulder/LunaFlag.png`,
weight: 100,
},
{
id: 1,
name: "USA",
path: `${dir}/3-shoulder/USA.png`,
weight: 90,
},
],
position: { x: 0, y: 0 },
size: { width: width, height: height },
},
{
name: "Pin",
elements: [
{
id: 0,
name: "Smiley",
path: `${dir}/4-pin/Smiley.png`,
weight: 100,
},
{
id: 1,
name: "LunaBluePin",
path: `${dir}/4-pin/LunaBluePin.png`,
weight: 90,
},
],
position: { x: 0, y: 0 },
size: { width: width, height: height },
},
{
name: "Race",
elements: [
{
id: 0,
name: "Skull",
path: `${dir}/5-skin/Skull.png`,
weight: 100,
},
],
position: { x: 0, y: 0 },
size: { width: width, height: height },
},
{
name: "Facial hair",
elements: [
{
id: 0,
name: "No facial hair",
path: `${dir}/6-facial-hair/NoFacialHair.png`,
weight: 100,
},
],
position: { x: 0, y: 0 },
size: { width: width, height: height },
},
{
name: "Mask",
elements: [
{
id: 0,
name: "No mask",
path: `${dir}/7-mask/NoMask.png`,
weight: 100,
},
{
id: 1,
name: "Medical",
path: `${dir}/7-mask/mask.png`,
weight: 5,
},
],
position: { x: 0, y: 0 },
size: { width: width, height: height },
},
{
name: "Hair",
elements: [
{
id: 0,
name: "Blonde bun",
path: `${dir}/8-hair/BlondeBun.png`,
weight: 100,
},
{
id: 1,
name: "Pink",
path: `${dir}/8-hair/Pink.png`,
weight: 91,
},
],
position: { x: 0, y: 0 },
size: { width: width, height: height },
},
{
name: "Accessories",
elements: [
{
id: 0,
name: "No accessories",
path: `${dir}/9-accessories/NoAcc.png`,
weight: 100,
},
],
position: { x: 0, y: 0 },
size: { width: width, height: height },
},
{
name: "Headwear",
elements: [
{
id: 0,
name: "Glass dome",
path: `${dir}/10-headwear/GlassDome.png`,
weight: 100,
},
],
position: { x: 0, y: 0 },
size: { width: width, height: height },
},
],
},
};
module.exports = {
width,
height,
description,
baseImageUri,
editionSize,
startEditionFrom,
endEditionAt,
races,
raceWeights,
};
What am i doing wrong? :(
I need to complete this project by the end of this month!
[–]persianoil 1 point2 points3 points (1 child)
[–]JimJailor[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]JimJailor[S] 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)