object has no attribute 'DiffuseColor' in recomputing BIM project

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
Post Reply
danreb
Posts: 57
Joined: Wed Mar 14, 2018 7:27 pm

object has no attribute 'DiffuseColor' in recomputing BIM project

Post by danreb »

I've got an error on recomputing a project
Traceback (most recent call last):
File "/Users/***/miniconda/envs/freecad_py37/Mod/Arch/ArchBuildingPart.py", line 629, in updateData
colors = self.getColors(obj)
File "/Users/***/miniconda/envs/freecad_py37/Mod/Arch/ArchBuildingPart.py", line 649, in getColors
if len(child.ViewObject.DiffuseColor) == len(child.Shape.Faces):
<class 'AttributeError'>: 'Gui.ViewProviderDocumentObject' object has no attribute 'DiffuseColor'

This is due to WPProxy (the one automaticaly created whit the BIM porject setup tool), labels and text as can be seen from print traces I've put in the code

Code: Select all

BBL .... trace test no DiffuseColor === WPProxy
BBL .... trace test no DiffuseColor === WPProxy001
BBL .... trace test no DiffuseColor === WPProxy002
BBL .... trace test no DiffuseColor === WPProxy003
BBL .... trace test no DiffuseColor === Text
BBL .... trace test no DiffuseColor === Axis
BBL .... trace test no DiffuseColor === WPProxy004
BBL .... trace test no DiffuseColor === WPProxy
BBL .... trace test no DiffuseColor === WPProxy001
BBL .... trace test no DiffuseColor === WPProxy002
BBL .... trace test no DiffuseColor === WPProxy003
BBL .... trace test no DiffuseColor === dLabel002
BBL .... trace test no DiffuseColor === dLabel003
BBL .... trace test no DiffuseColor === dLabel
BBL .... trace test no DiffuseColor === dLabel001
I've modified the code [Mod/Arch/ArchBuildingPart.py] as follow to circumvent this blocking error

BEFORE .....

Code: Select all

    def getColors(self,obj):

        "recursively get the colors of objects inside this BuildingPart"

        colors = []
        for child in Draft.getGroupContents(obj):
            if hasattr(child,'Shape') :
                if len(child.ViewObject.DiffuseColor) == len(child.Shape.Faces):
                    colors.extend(child.ViewObject.DiffuseColor)
                else:
                    c = child.ViewObject.ShapeColor[:3]+(child.ViewObject.Transparency/100.0,)
                    for i in range(len(child.Shape.Faces)):
                        colors.append(c)
        return colors
AFTER ....

Code: Select all

    def getColors(self,obj):

        "recursively get the colors of objects inside this BuildingPart"

        colors = []
        for child in Draft.getGroupContents(obj):
            if hasattr(child,'Shape') and (hasattr(child.ViewObject,"DiffuseColor") or hasattr(child.ViewObject,"DiffuseColor")):
                if hasattr(child.ViewObject,"DiffuseColor") and len(child.ViewObject.DiffuseColor) == len(child.Shape.Faces):
                    colors.extend(child.ViewObject.DiffuseColor)
                else:
                    c = child.ViewObject.ShapeColor[:3]+(child.ViewObject.Transparency/100.0,)
                    for i in range(len(child.Shape.Faces)):
                        colors.append(c)
        return colors
OS: macOS Mojave (10.14)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.19107 (Git)
Build type: Release
Branch: master
Hash: ad952191297fe53593c4d4b0ac80b43fd6664b22
Python version: 3.7.6
Qt version: 5.12.5
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: French/France (fr_FR)
danreb
Posts: 57
Joined: Wed Mar 14, 2018 7:27 pm

Re: object has no attribute 'DiffuseColor' in recomputing BIM project

Post by danreb »

yorik wrote:Hi Yorik what do you think of it? Should the Arch code be corrected?
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: object has no attribute 'DiffuseColor' in recomputing BIM project

Post by Kunda1 »

danreb wrote: Thu Jan 23, 2020 10:05 am
yorik wrote:Hi Yorik what do you think of it? Should the Arch code be corrected?
@danreb u need to add yorik's user_id=68 within the quote bbcode. You can learn a user's user_id by searching their username clicking on their profile and seeing the: u= part of the URL to get the number.
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: object has no attribute 'DiffuseColor' in recomputing BIM project

Post by vocx »

danreb wrote: Sun Jan 05, 2020 8:34 am

Code: Select all

...
            if hasattr(child,'Shape') and (hasattr(child.ViewObject,"DiffuseColor") or hasattr(child.ViewObject,"DiffuseColor")):
                if hasattr(child.ViewObject,"DiffuseColor") and len(child.ViewObject.DiffuseColor) == len(child.Shape.Faces):
I don't understand why you check for DiffuseColor twice.

In the second "if" you don't need to test for DiffuseColor again because we found it in the first "if" already.

But other than that the code looks fine.

Code: Select all

...
            if hasattr(child,'Shape') and hasattr(child.ViewObject,"DiffuseColor"):
                if len(child.ViewObject.DiffuseColor) == len(child.Shape.Faces):
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
danreb
Posts: 57
Joined: Wed Mar 14, 2018 7:27 pm

Re: object has no attribute 'DiffuseColor' in recomputing BIM project

Post by danreb »

vocx wrote: Fri Jan 24, 2020 4:21 am
...
I don't understand why you check for DiffuseColor twice.
...
Hi @vocx sorry for this duplicate entry. You are totaly right, actually the first check is for the attribute "DiffuseColor" and the second should have been for the attribute "ShapeColor" which is requested in the else par of the condition check and I got a null error on this property with one of my test.

the real code should be:

Code: Select all

    def getColors(self,obj):

        "recursively get the colors of objects inside this BuildingPart"

        colors = []
        for child in Draft.getGroupContents(obj):
            if hasattr(child,'Shape') and (hasattr(child.ViewObject,"DiffuseColor") or hasattr(child.ViewObject,"ShapeColor")):
                if hasattr(child.ViewObject,"DiffuseColor") and len(child.ViewObject.DiffuseColor) == len(child.Shape.Faces):
                    colors.extend(child.ViewObject.DiffuseColor)
                else:
                    c = child.ViewObject.ShapeColor[:3]+(child.ViewObject.Transparency/100.0,)
                    for i in range(len(child.Shape.Faces)):
                        colors.append(c)
        return colors
danreb
Posts: 57
Joined: Wed Mar 14, 2018 7:27 pm

Re: object has no attribute 'DiffuseColor' in recomputing BIM project

Post by danreb »

Kunda1 wrote: Thu Jan 23, 2020 1:33 pm @danreb u need to add yorik's user_id
Hi thanks @Kunda1
yorik wrote: ping !
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: object has no attribute 'DiffuseColor' in recomputing BIM project

Post by Kunda1 »

Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
Post Reply