EmbroidePy / pyembroidery

pyembroidery library for reading and writing a variety of embroidery formats.
MIT License
195 stars 35 forks source link

Added Patterns don't Maintain Order #136

Closed ajquick closed 2 years ago

ajquick commented 2 years ago

First of all, I love this library! I am attempting to make something to automate file creation, so far so good, but I have a problem with the order in which patterns are being generated for stitching.

I deliberately stitch words as groups of letters by starting from the middle and working my way out to avoid any excessive pulling during embroidery. I have successfully added my patterns (individual letters) in the correct order with correct spacing.

This is the order that the letters are added and the order in which they are expected to be stitched. They are all the same color:

Patterns-Added

You can see it adds 1 first, and that's the first one to be stitched, it goes to 2 and then 3 and so on. The patterns are added correctly in this order.

What ultimately happens is that all the patterns get combined (because of color) and a new sewing order is generated like this:

Pattern-Stitched

It always goes from left to right.

I have tried everything I can think of without any success. Changing to blocks from patterns, adding color stops, treating duplicate colors as stops.. etc. I have not yet found the correct set of commands to preserve the order in which they are stitched.

I know that I probably need to set each pattern (letter) as a color block to maintain separation and then convert those the same color, but nothing is working. Is there an example of adding say 3 patterns with the same color and maintaining a custom sew order (the order in which the patterns were added). Then combining the colors and adding trims while still maintaining previous sew order?

A good example would be 3 patterns with the same color, stitching from right to left.

tatarize commented 2 years ago

There is nothing in the code that should ever reorder them. There is especially nothing in code that would sort them right to left, or based on the block geometry. The code stores things in literal stitch order and doesn't rearrange the blocks.

You could try exporting them into a lossless .csv file and look at the locations. It should be the case that they can't rearrange themselves and something else is resulting in this effect.

ajquick commented 2 years ago

You are correct. It ultimately came down to how I was adding the letters. I guess I was always adding them to to the left, even when the new letter appeared on the right.

One thing to note, this function caused me a lot of headaches: move_center_to_origin

I had a hard time figuring out why it wasn't placing the origin in the center, then I realized I probably wanted something like: "move_origin_to_center" and not the other way around!

Another thing that was causing trouble was the MATRIX_SCALE function being default and not MATRIX_SCALE_ORIGIN. After I figured out these problems, the math and offsets started to work correctly.

tatarize commented 2 years ago

Yeah, I'm not sure what I was thinking with MATRIX_SCALE and then adding in a function after that that correctly did with MATRIX_SCALE should have done. It ended up getting sort of baked in there wrongly. I guess I could make a couple other commands like just SCALE and TRANSLATE and deprecate the other ones and use they correctly as the affine transformations they should have been from the start.