HashLips / generative-art-opensource

Create generative art by using the canvas api and node js, feel free to contribute to this repo with new ideas.
MIT License
1.36k stars 695 forks source link

Can't fix problem #68

Open Skulletje opened 2 years ago

Skulletje commented 2 years ago

I've tried everything mentioned in posts like changing the rarityweights, checking if a layer is empty, changing the editionsize or having the addraritypercentforlayer add up to 100. It just doesn't work and i get this code. Any help would be appreciated

##################

Generative Art

- Create your NFT collection

##################

start creating NFTs.

creating NFT 1 of 1000

Ponda1 commented 2 years ago

If you dont mind could you send an image of your config file?

Skulletje commented 2 years ago

Here's the code and the images are at the bottom

/**

// adds a rarity to the configuration. This is expected to correspond with a directory containing the rarity for each defined layer // @param _id - id of the rarity // @param _from - number in the edition to start this rarity from // @param _to - number in the edition to generate this rarity to // @return a rarity object used to dynamically generate the NFTs const addRarity = (_id, _from, _to) => { const _rarityWeight = { value: _id, from: _from, to: _to, layerPercent: {} }; return _rarityWeight; };

// get the name without last 4 characters -> slice .png from the name const cleanName = (_str) => { let name = _str.slice(0, -4); return name; };

// reads the filenames of a given folder and returns it with its name and path const getElements = (_path, _elementCount) => { return fs .readdirSync(_path) .filter((item) => !/(^|\/).[^\/.]/g.test(item)) .map((i) => { return { id: _elementCount, name: cleanName(i), path: ${_path}/${i} }; }); };

// adds a layer to the configuration. The layer will hold information on all the defined parts and // where they should be rendered in the image // @param _id - id of the layer // @param _position - on which x/y value to render this part // @param _size - of the image // @return a layer object used to dynamically generate the NFTs const addLayer = (_id, _position, _size) => { if (!_id) { console.log('error adding layer, parameters id required'); return null; } if (!_position) { _position = { x: 0, y: 0 }; } if (!_size) { _size = { width: width, height: height } } // add two different dimension for elements: // - all elements with their path information // - only the ids mapped to their rarity let elements = []; let elementCount = 0; let elementIdsForRarity = {}; rarityWeights.forEach((rarityWeight) => { let elementsForRarity = getElements(${dir}/${_id}/${rarityWeight.value});

elementIdsForRarity[rarityWeight.value] = [];
elementsForRarity.forEach((_elementForRarity) => {
  _elementForRarity.id = `${editionDnaPrefix}${elementCount}`;
  elements.push(_elementForRarity);
  elementIdsForRarity[rarityWeight.value].push(_elementForRarity.id);
  elementCount++;
})
elements[rarityWeight.value] = elementsForRarity;

});

let elementsForLayer = { id: _id, position: _position, size: _size, elements, elementIdsForRarity }; return elementsForLayer; };

// adds layer-specific percentages to use one vs another rarity // @param _rarityId - the id of the rarity to specifiy // @param _layerId - the id of the layer to specifiy // @param _percentages - an object defining the rarities and the percentage with which a given rarity for this layer should be used const addRarityPercentForLayer = (_rarityId, _layerId, _percentages) => { let _rarityFound = false; rarityWeights.forEach((_rarityWeight) => { if (_rarityWeight.value === _rarityId) { let _percentArray = []; for (let percentType in _percentages) { _percentArray.push({ id: percentType, percent: _percentages[percentType] }) } _rarityWeight.layerPercent[_layerId] = _percentArray; _rarityFound = true; } }); if (!_rarityFound) { console.log(rarity ${_rarityId} not found, failed to add percentage information); } }

/**

// image width in pixels const width = 1000; // image height in pixels const height = 1000; // description for NFT in metadata file const description = "This is an NFT made by the coolest generative code."; // base url to use in metadata file // the id of the nft will be added to this url, in the example e.g. https://hashlips/nft/1 for NFT with id 1 const baseImageUri = "https://hashlips/nft"; // id for edition to start from const startEditionFrom = 1; // amount of NFTs to generate in edition const editionSize = 2500; // prefix to add to edition dna ids (to distinguish dna counts from different generation processes for the same collection) const editionDnaPrefix = 0

// create required weights // for each weight, call 'addRarity' with the id and from which to which element this rarity should be applied let rarityWeights = [ addRarity('super_rare', 1, 250), addRarity('rare', 251, 1250), addRarity('original', 1251, 2500) ];

// create required layers // for each layer, call 'addLayer' with the id and optionally the positioning and size // the id would be the name of the folder in your input directory, e.g. 'ball' for ./input/ball const layers = [ addLayer('ball', { x: 0, y: 0 }, { width: width, height: height }), addLayer('eye color'), addLayer('iris'), addLayer('shine'), addLayer('bottom lid'), addLayer('top lid') ];

// provide any specific percentages that are required for a given layer and rarity level // all provided options are used based on their percentage values to decide which layer to select from addRarityPercentForLayer('super_rare', 'ball', { 'super_rare': 20, 'rare': 30, 'original': 50 }); addRarityPercentForLayer('super_rare', 'eye color', { 'super_rare': 20, 'rare': 30, 'original': 50 }); addRarityPercentForLayer('original', 'eye color', { 'super_rare': 20, 'rare': 30, 'original': 50 });

module.exports = { layers, width, height, description, baseImageUri, editionSize, startEditionFrom, rarityWeights, };

dddd ddd

Ponda1 commented 2 years ago

Everything in the code looks fine to me. Are you using the layers and images which come with the generator(the ones made by HashLips)? Because if you are using your own layers and images then you have to change the names of the layers in addLayer. Otherwise I have no idea

BitBarbarian97 commented 2 years ago

I actually had this same issue, but here is what I changed and it seemed to correct the issue for me.

After changing these things, the code seems to have stopped throwing this error at me.