Color yarns based on orientation

General discussion about TexGen.

Moderators: Martin, Developers

Post Reply
htl320
Posts: 5
Joined: Thu Apr 04, 2019 12:48 pm

Color yarns based on orientation

Post by htl320 »

Hello All,

As this is my first post I would like to say Hello to everyone and a big thanks to the developers and users of TexGen :)

I am trying to write a script to color the individual yarns by yarn orientation, but i cannot figure out how to access the information about the orientation of each yarn. I want to write a loop which iterates through each yarn, obtains the orientation and then changes the color of the yarn appropriately. The code below is how I imagine it could work.

-------------------------------------------------------------------------

# get handle
rw = GetRenderWindow()
tl = GetTextile()
# get number of yarns
n_yarns = tl.GetNumYarns()
# loop
for i in range(n_yarns):
### CODE TO OBTAIN YARN ORIENTATION (0 or 1) ###
# boolean
if "orientation" == 0:
rw.SetYarnColor(i, COLOR(1,0,0)) # red
else:
rw.SetYarnColor(i, COLOR(0,0,1)) # blue

-------------------------------------------------------------------------

By the way, I have noticed that when trying to get the number of layers and yarns of a woven ply (like here http://texgen.sourceforge.net/phpBB3/vi ... tate#p6604) through GetNumLayers() or GetNumYarns() the output is incorrect and only shows the number of layers and yarns for the initial weave. When I inquire as to the type of textile the answer is a weave rather than a" LayeredTextile(Layers:2)" as shown in the GUI. This issue would have an effect on the number of yarns that are supposed to be colored in the script above and I'd like to ask if there is a way to get the right values.

I have attached a model of my two layer ply.

Thanks for the help in advance :)
Attachments
5harness_2layers.zip
5 harness 2 layer model
(53.84 KiB) Downloaded 1186 times
htl320
Posts: 5
Joined: Thu Apr 04, 2019 12:48 pm

Re: Color yarns based on orientation

Post by htl320 »

Hello again,

just an additional comment. What I'm trying to do is essentially this, as shown on the main wiki page:

Image

Thanks for the help!
louisepb
Project Leader
Posts: 998
Joined: Tue Dec 08, 2009 2:27 pm
Location: Nottingham

Re: Color yarns based on orientation

Post by louisepb »

Hi,

I'm afraid that there isn't a straightforward way of working out which are the weft and warp yarns in a layered textile. In the textiles generated by the 2d weave there is information about the yarn configuration but this is lost when it's added to a layered textile - it simply has the information about the individual yarns.
The picture you show from the wiki page is just a plain weave with four yarns but the domain has been set to show 3 repeats of the yarns in both the x and y directions. So each red yarn you see is just a repeat of the same yarn and is therefore displayed in the same colour.

The question about getting the number of layers: You need to get the layered textile weave - so something like
weave = GetTextile().GetLayeredTextile()
Unfortunately the Python wrapping can't interpret the overloaded classes in the underlying C++ code so the function above will get the pointer to the correct, layered, weave type. If you use this then the GetNumLayers and GetNumYarns functions should work.

Hope that helps,
Louise
htl320
Posts: 5
Joined: Thu Apr 04, 2019 12:48 pm

Re: Color yarns based on orientation

Post by htl320 »

Hello Louise,

Thanks for the reply. You are right, perhaps it is better to color the yarns based on warp/weft criteria and not according to orientation. Nonetheless, I managed to write a script which does the latter, if anyone is interested. I obtain the yarn orientation using the first two nodes of each yarn. The script - coloring.py - is attached to this post.

However, I have one more question. After creating a layered textile (using a script - dup_rot.py - which is also attached), and issuing the GetTextile().GetLayeredTextile() command the result is 'None', but issuing GetTextile().GetWeave() works, meaning that indeed there is a problem with the classes. When I save and reload the file, however, the GetTextile().GetLayeredTextile() command works and I can obtain the number of layers, yarns, etc. Is there a command which resets the textile information, so I don't have to close and open the file?

Thanks again.
Attachments
dup_rot.zip
(922 Bytes) Downloaded 1365 times
coloring.zip
(600 Bytes) Downloaded 1456 times
louisepb
Project Leader
Posts: 998
Joined: Tue Dec 08, 2009 2:27 pm
Location: Nottingham

Re: Color yarns based on orientation

Post by louisepb »

Hi,

Thanks for sharing your scripts. i haven't tried running them because of the hard-coded paths but it looks like you create two different layered weaves?

If you then used the GetTextile() command with no parameter it will get the first in the textile list. This, I think, would still be the original 2D weave which is why the GetLayeredTextile() will return 'None'. If you have several textiles open and want to retrieve a specific one then you need to specify the textile name as a parameter in the GetTextile command.

Hope that helps,
Louise
htl320
Posts: 5
Joined: Thu Apr 04, 2019 12:48 pm

Re: Color yarns based on orientation

Post by htl320 »

Hello again Louise,

I removed the hardcoded file save and file open lines from the coloring.py script and added the 'LayeredTextile(Layers:2)' string in the GetTextile() command, but the executing didn't run as expected. Upon closing and reopening the file, however, the script runs properly. It seems that there is a deeper issue, so for now I will just save the textiles and reopen them as it is not too big of a deal.

I have added the updated coloring.py script as an attachment.

In the future I plan on modifying it so that the orientations don't necessarily need to be parallel to the axes. The numpy.unique function should help with that.

Thanks,

Hristo
Attachments
coloring.zip
(505 Bytes) Downloaded 1206 times
louisepb
Project Leader
Posts: 998
Joined: Tue Dec 08, 2009 2:27 pm
Location: Nottingham

Re: Color yarns based on orientation

Post by louisepb »

Hi,

I don't think there is a problem with getting the layered textile etc. You need to be careful about that name that you are using to get the textile/weave. If you use the GetTextile().GetName() function then it will return the first in the list (ie the name in the left hand tab on the GUI if you have several textiles loaded).

If you close TexGen and reopen your layered textile then it will be the first in the list, you will have the correct name and therefore everything works.

Hope that helps,
Louise
binyang
Posts: 1
Joined: Sun Mar 22, 2020 6:32 pm
Location: Montreal

Re: Color yarns based on orientation

Post by binyang »

Hi Louise,

It seems that all the attachments in this thread were disappear. Would it be possible to fix these broken links to make the codes downloadable again?

Many thanks.

Bin Yang
louisepb
Project Leader
Posts: 998
Joined: Tue Dec 08, 2009 2:27 pm
Location: Nottingham

Re: Color yarns based on orientation

Post by louisepb »

Hi Bin Yang,

I think that this must be something to do with the recent forum update. I will contact Sourceforge to see if there's something that I need to do to get these links working again.

Best wishes,
Louise
Post Reply