Change Color of one face >> simple code possible ?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Nanayaw_NEW
Posts: 31
Joined: Sat Sep 24, 2022 3:52 pm

Change Color of one face >> simple code possible ?

Post by Nanayaw_NEW »

Hello All

I have one question is it possible to change the color of a face easier as the code I showed ?

Code: Select all

import FreeCAD
import PySide
from PySide import QtCore, QtGui

Gui.Selection.addSelection('Test_Meassuring','Box001','Face4')

a = FreeCADGui.Selection.getSelectionEx()    
aa = FreeCADGui.Selection.getSelection()     
 
SubElement = FreeCADGui.Selection.getSelectionEx()   
element_ = SubElement[0].SubElementNames[0]             
print(element_)

#FreeCAD.ActiveDocument.getObject(aa[0].Name).ViewObject.DiffuseColor=[(1.0,1.0,0.0)] >> Works but only for the whole object

cols = colors = []
cols = FreeCAD.ActiveDocument.getObject(aa[0].Name).ViewObject.DiffuseColor

for i in aa[0].Shape.Faces:
   colors += [(cols[0])]

for i in range(len(aa)):
     fce = int(a[0].SubElementNames[i][4:])-1 
     colors[fce] = (1.00,1.00,0.00) 
     aa[i].ViewObject.DiffuseColor = colors # >> Colors the selected face as it shows 
To change the color of the complete object the code is very simple that's the reason I ask

Code: Select all

aa = FreeCADGui.Selection.getSelection()     
 
FreeCAD.ActiveDocument.getObject(aa[0].Name).ViewObject.DiffuseColor=[(1.0,1.0,0.0)] #>> Works but only for the whole object
If somebody could give me an idea where I'm wrong it will be greate :lol:

Thanks and Greetings

Nanayaw

Code: Select all

OS: KDE Flatpak runtime (XFCE/xfce)
Word size of FreeCAD: 64-bit
Version: 0.20.1.29410 (Git)
Build type: Release
Branch: (HEAD detached at 0.20.1)
Hash: f5d13554ecc7a456fb6e970568ae5c74ba727563
Python 3.9.9, Qt 5.15.5, Coin 4.0.0, Vtk 8.2.0, OCC 7.6.2
Locale: German/Germany (de_DE)
Installed mods: 
  * BIM 2021.12.0
  * Curves 0.6.1
  * lattice2 1.0.0
  * CurvedShapes 1.0.4
  * FEM_FrontISTR 0.1.0
  * Assembly3 0.11.4
  * workfeature
  * Manipulator 1.4.9
  * Defeaturing 1.2.0
User avatar
Roy_043
Veteran
Posts: 8552
Joined: Thu Dec 27, 2018 12:28 pm

Re: Change Color of one face >> simple code possible ?

Post by Roy_043 »

The DiffuseColor property must be a list with a single tuple, or a tuple for every face in the object. In that sense there is nothing wrong with your first code sample. But it does contain some mistakes: it will only work properly if a single object is selected.

FYI: there is no reason to combine FreeCADGui.Selection.getSelectionEx() and FreeCADGui.Selection.getSelection():

Code: Select all

FreeCADGui.Selection.getSelectionEx()[0].Object
mario52
Veteran
Posts: 4692
Joined: Wed May 16, 2012 2:13 pm

Re: Change Color of one face >> simple code possible ?

Post by mario52 »

Hi

here for separate wire, face or point Macro_Colorize Image
Roy_043 wrote: Fri Dec 02, 2022 11:23 am FYI: there is no reason to combine FreeCADGui.Selection.getSelectionEx() and FreeCADGui.Selection.getSelection():

Code: Select all

FreeCADGui.Selection.getSelectionEx()[0].Object
Couleur différentes sur les faces

all depends of the level of the programmer (good .Object)

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
Nanayaw_NEW
Posts: 31
Joined: Sat Sep 24, 2022 3:52 pm

Re: Change Color of one face >> simple code possible ?

Post by Nanayaw_NEW »

Hello Roy and Mario

Thanks for you support, then it is as I thought not done with a simple line
the color macro and that one from the like i found before.

Thanks and Greertings

Nanayaw
User avatar
onekk
Veteran
Posts: 6208
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Change Color of one face >> simple code possible ?

Post by onekk »

Nanayaw_NEW wrote: Sat Dec 03, 2022 6:21 am ...
No it could not be done with a simple line.

Because what you are seeing in the 3d View is not the "real solid" is the "Coin3D" representation of this solid, so it is managed by a "different engine", something could be done but it is not so trivial to do.

You have to modify the color of the face in the "coin3D" part and to make this you have to pass an entire list of "face colors" to the obj.ViewObject.DiffuseColor variable, so basically you have to:
  • find the face index
  • create an appropriate list that contains the actual color for the solid
  • change the appropriate "list element" that contains the "face color" with the desired one.
This what the Macro will do behind the curtains (It is clear if you read the Macro code)

There were some discussion some time ago about adding some helpers methods, sadly I have not saved the link to the forum thread.

Regards

Carlo D.

EDIT: Amended post as it was not clear: Thanks @Roy_043 for having pointed out.
Last edited by onekk on Sat Dec 03, 2022 6:42 pm, edited 1 time in total.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
Roy_043
Veteran
Posts: 8552
Joined: Thu Dec 27, 2018 12:28 pm

Re: Change Color of one face >> simple code possible ?

Post by Roy_043 »

onekk wrote: Sat Dec 03, 2022 6:21 pm No it could not be done
3 previous posts say that it can be done. Note the use of FreeCADGui in the OP.
User avatar
onekk
Veteran
Posts: 6208
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Change Color of one face >> simple code possible ?

Post by onekk »

Roy_043 wrote: Sat Dec 03, 2022 6:26 pm ...
It was referred to the:
not done with a simple line
on the OP reply.

Changing a face color could be done, but not with a "simple line" in python. Probably the OP want to change as example color of Face6 "directly".

Thanks for pointing out, I've amended my post above.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
Roy_043
Veteran
Posts: 8552
Joined: Thu Dec 27, 2018 12:28 pm

Re: Change Color of one face >> simple code possible ?

Post by Roy_043 »

I think you are referring to a topic about handling view properties if FreeCADGui is not available. What we are dealing with here is not a one-liner, but still pretty trivial.
Post Reply