Setting yarn colour in python

General discussion about TexGen.

Moderators: Martin, Developers

Post Reply
ah
Posts: 7
Joined: Mon Sep 24, 2007 11:22 am

Setting yarn colour in python

Post by ah »

I've being trying to create a textile comprising of a sizable number of warps and wefts and I want to specify a specific colour of each yarn. How is this achieved in python?

thanks
Martin
Project Leader
Posts: 70
Joined: Wed Mar 28, 2007 9:05 am
Location: Nottingham
Contact:

Post by Martin »

Currently there isn't any function to set the colour of individual yarns directly. However I think it may be possible to do it in a round-about kind of way. I will try to knock up a python script shortly and report back to you.

I will also add this as a feature request.

Martin.
Martin
Project Leader
Posts: 70
Joined: Wed Mar 28, 2007 9:05 am
Location: Nottingham
Contact:

Post by Martin »

What I had in mind won't work actually. I'm afraid your stuck with the default colouring scheme for the moment. You can set the colour of all yarns as a whole from the "Rendering -> Change Surface Color" menu.
ah
Posts: 7
Joined: Mon Sep 24, 2007 11:22 am

Post by ah »

Has this been changed in any recent updates to the source code. I was thinking of trying to compile the code myself.
Martin
Project Leader
Posts: 70
Joined: Wed Mar 28, 2007 9:05 am
Location: Nottingham
Contact:

Post by Martin »

I just added a function to set the yarn color, of course you'll need to compile the latest SVN version for it to work. Once install you can set the yarn colors from the GUI interactive python console like this:

Code: Select all

rw = GetRenderWindow()
rw.SetYarnColor(0, COLOR(1, 0, 1))
This sets the yarn with index 0 to magenta.
Dr_ajr
Posts: 3
Joined: Wed Nov 07, 2007 3:24 pm

Post by Dr_ajr »

Just trying out this new feature in 3.2.1

You are not limited to the default colours of TexGen (i.e. 0,0,0 = Black, 0,1,0 = Green, 1,1,1 = White etc).

You can actually input RGB values to get the colour you require:

so instead of using:

rw = GetRenderWindow()
rw.SetYarnColor(0, COLOR(1, 0, 1))

use:

rw = GetRenderWindow()
rw.SetYarnColor(0, COLOR(180, 215, 218))

A nice RGB calculator can be found here;

http://www.calculatorcat.com/free_calcu ... ider.phtml

Hope it helps.
Martin
Project Leader
Posts: 70
Joined: Wed Mar 28, 2007 9:05 am
Location: Nottingham
Contact:

Post by Martin »

Yes that's right you're not limited to black, green, white... but the red, green, blue parameters to the COLOR structure should be given in the range from 0 to 1 and not 0 to 255. So the following works:

Code: Select all

rw = GetRenderWindow()
rw.SetYarnColor(0, COLOR(0.7, 0.85, 0.85))
but the following would be recognised as white:

Code: Select all

rw = GetRenderWindow()
rw.SetYarnColor(0, COLOR(180, 215, 218))
(Note: Just divide the value by 255 to get it in the range 0 to 1)
Dr_ajr
Posts: 3
Joined: Wed Nov 07, 2007 3:24 pm

Post by Dr_ajr »

Hi Martin,

I'm trying to write a python script for a specific fabric and want to include the "yarn colour function" directly into the code.

The colour function works great when I use it in the python console of Texgen (for example).

rw = GetRenderWindow()
rw.SetYarnColor(0, COLOR(0.7, 0.85, 0.85))


However, if I include the above code into my python script, I get the error message:

rw.SetYarnColor(1, COLOR(0.7, 0.85, 0.85))
AttributeError: 'NoneType' object has no attribute 'SetYarnColor'

I'd be grateful for any pointers and advice.

Cheers
Martin
Project Leader
Posts: 70
Joined: Wed Mar 28, 2007 9:05 am
Location: Nottingham
Contact:

Post by Martin »

Did you include both lines?

Code: Select all

rw = GetRenderWindow()
rw.SetYarnColor(0, COLOR(0.7, 0.85, 0.85))
Perhaps you could post the entire script so I can see what the problem might be.

In any case the error you are seeing indicates that the call to "GetRenderWindow()" is returning "None". This is probably because the render window doesn't exist yet at the time you called that function. You should make sure this part of the code comes after the AddTextile() call.
Post Reply