diff options
| -rw-r--r-- | lib/elements/element.py | 16 | ||||
| -rw-r--r-- | lib/extensions/base.py | 2 | ||||
| -rw-r--r-- | lib/extensions/params.py | 46 | ||||
| -rw-r--r-- | lib/svg/tags.py | 3 |
4 files changed, 43 insertions, 24 deletions
diff --git a/lib/elements/element.py b/lib/elements/element.py index 84141d4f..b8b25b27 100644 --- a/lib/elements/element.py +++ b/lib/elements/element.py @@ -251,6 +251,22 @@ class EmbroideryElement(object): return self.get_boolean_param('force_lock_stitches', False) @property + @param('random_seed', + _('Random seed'), + tooltip=_('Use a specific seed for randomized attributes. Uses the element ID if empty.'), + type='random_seed', + default='', + sort_index=100) + @cache + def random_seed(self) -> str: + seed = self.get_param('random_seed') + if not seed: + seed = self.node.get_id() or '' + # TODO(#1696): When inplementing grouped clones, join this with the IDs of any shadow roots, + # letting each instance without a specified seed get a different default. + return seed + + @property def path(self): # A CSP is a "cubic superpath". # diff --git a/lib/extensions/base.py b/lib/extensions/base.py index cf94714c..c2f76b27 100644 --- a/lib/extensions/base.py +++ b/lib/extensions/base.py @@ -179,6 +179,8 @@ class InkstitchExtension(inkex.Effect): return nodes def get_nodes(self, troubleshoot=False): + # Postorder traversal of selected nodes and their descendants. + # Returns all nodes if there is no selection. return self.descendants(self.document.getroot(), troubleshoot=troubleshoot) def get_elements(self, troubleshoot=False): diff --git a/lib/extensions/params.py b/lib/extensions/params.py index 7d38b5ff..a568573f 100644 --- a/lib/extensions/params.py +++ b/lib/extensions/params.py @@ -6,11 +6,11 @@ # -*- coding: UTF-8 -*- import os -import random import sys from collections import defaultdict from copy import copy from itertools import groupby, zip_longest +from secrets import randbelow import wx from wx.lib.scrolledpanel import ScrolledPanel @@ -35,6 +35,8 @@ class ParamsTab(ScrolledPanel): def __init__(self, *args, **kwargs): self.params = kwargs.pop('params', []) self.name = kwargs.pop('name', None) + + # TODO: this is actually a list of embroidery elements, not DOM nodes, and needs to be renamed self.nodes = kwargs.pop('nodes') kwargs["style"] = wx.TAB_TRAVERSAL ScrolledPanel.__init__(self, *args, **kwargs) @@ -191,15 +193,21 @@ class ParamsTab(ScrolledPanel): return values - def on_change_seed(self, event): + def on_reroll(self, event): + if len(self.nodes) == 1: + new_seed = str(randbelow(int(1e8))) + input = self.param_inputs['random_seed'] + input.SetValue(new_seed) + self.changed_inputs.add(input) + else: + for node in self.nodes: + new_seed = str(randbelow(int(1e8))) + node.set_param('random_seed', new_seed) - for node in self.nodes: - random.seed() - new_seed = random.randint(1, 10000) - node.set_param("use_seed", new_seed) + self.enable_change_indicator('random_seed') + event.Skip() if self.on_change_hook: self.on_change_hook(self) - event.Skip() def apply(self): values = self.get_values() @@ -377,7 +385,12 @@ class ParamsTab(ScrolledPanel): self.param_inputs[param.name] = input - col4 = wx.StaticText(self, label=param.unit or "") + if param.type == 'random_seed': + col4 = wx.Button(self, wx.ID_ANY, _("Re-roll")) + col4.Bind(wx.EVT_BUTTON, self.on_reroll) + col4.SetBitmap(self.randomize_icon) + else: + col4 = wx.StaticText(self, label=param.unit or "") if param.select_items is not None: input.Hide() @@ -394,16 +407,6 @@ class ParamsTab(ScrolledPanel): box.Add(self.settings_grid, proportion=1, flag=wx.ALL, border=10) - add_seed_button = False - for param in self.params: - if param.name[:6] == "random": - add_seed_button = True - if add_seed_button: - self.change_seed_button = wx.Button(self, wx.ID_ANY, _("Change Seed")) - self.change_seed_button.Bind(wx.EVT_BUTTON, self.on_change_seed) - self.change_seed_button.SetBitmap(self.randomize_icon) - box.Add(self.change_seed_button, proportion=0, flag=wx.ALIGN_CENTER_HORIZONTAL, border=10) - self.SetSizer(box) self.update_choice_widgets() @@ -425,11 +428,6 @@ class ParamsTab(ScrolledPanel): self.param_change_indicators[param].SetToolTip( _('This parameter will be saved when you click "Apply and Quit"')) - self.changed_inputs.add(self.param_inputs[param]) - - if self.on_change_hook: - self.on_change_hook(self) - # end of class SatinPane @@ -636,6 +634,8 @@ class Params(InkstitchExtension): return classes def get_nodes_by_class(self): + # returns embroidery elements (not nodes) by class + # TODO: rename this nodes = self.get_nodes() nodes_by_class = defaultdict(list) diff --git a/lib/svg/tags.py b/lib/svg/tags.py index 54400c4e..6ae3b66a 100644 --- a/lib/svg/tags.py +++ b/lib/svg/tags.py @@ -130,9 +130,10 @@ inkstitch_attribs = [ 'use_seed', # stitch_plan 'invisible_layers', - # Legacy + # All elements 'trim_after', 'stop_after', + 'random_seed', 'manual_stitch', ] for attrib in inkstitch_attribs: |
