Set body appearance

Set body appearance

brad.bylls
Collaborator Collaborator
2,885 次查看
15 条回复
1 条消息(共 16 条)

Set body appearance

brad.bylls
Collaborator
Collaborator

I am trying to set the color of a body in my script.

Some code in this forum I found doesn't work for me.

No error messages, but no change in body color either.

Code below:

        fc = headExt.faces[0]
        bd = fc.body
        bd.name = _inputSelectScrewSize.value + ' Screw'
        appear = appearanceLib.appearances.itemByName('Paint - Powder Coat (Blue)')
        bd.appearance = appear
        appear = design.appearances.itemByName('Paint - Powder Coat (Blue)')
        fc.appearance = appear
Brad Bylls
0 个赞
已接受的解答 (1)
2,886 次查看
15 条回复
回复 (15)
2 条消息(共 16 条)

BrianEkins
Mentor
Mentor

Here's a blog post I wrote a while ago that shows how to create a new appearance and assign it to a body.

https://ekinssolutions.com/setting-colors-in-fusion-360/

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 个赞
3 条消息(共 16 条)

brad.bylls
Collaborator
Collaborator
Thanks for the help.
I've used two lines from your code, but when I run it I get an error 'invalid name' on the second line.
        fusionMaterials = _app.materialLibraries.itemByName('Fusion 360 Appearance Library')
        appear = fusionMaterials.appearances.itemByName('Paint - Powder Coat (Blue)')
Error.png
Brad Bylls
0 个赞
4 条消息(共 16 条)

kandennti
Mentor
Mentor
已接受的解答

Hi @brad.bylls .

 

Is the name specified incorrectly?
I think it's "Powder Coat (Blue)".

The following script writes out the Ids and names of all available appearances.

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()

        for mateLib in app.materialLibraries:
            print('-- MaterialLibrary (Id:Name)--')
            print('{}:{}'.format(mateLib.id,mateLib.name))
            for appe in mateLib.appearances:
                print('    {}:{}'.format(appe.id,appe.name))
            print('\n\n')

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

Those that are not on the list are likely to need to be downloaded in advance.

1.png

0 个赞
5 条消息(共 16 条)

adminTCYL2
Enthusiast
Enthusiast

Hi Brian, 

Your blog-post does not work: 
"There has been a critical error on this website."

So I do not find the relevant code. Can you post it here?

0 个赞
6 条消息(共 16 条)

brad.bylls
Collaborator
Collaborator

This is Brad not Brian.

I got this to work to create a new color from an existing one ('Powder Coat (Blue)'):

    fusionMaterials = _app.materialLibraries.itemByName('Fusion 360 Appearance Library')
    appear = fusionMaterials.appearances.itemByName('Powder Coat (Blue)')
    yellowColor = design.appearances.addByCopy(appear, 'YellowColor')
    colorProp = adsk.core.ColorProperty.cast(yellowColor.appearanceProperties.itemByName('Color'))
    colorProp.value = adsk.core.Color.create(204, 204, 0, 0)
    plateTCPbody.appearance = yellowColor
 
To use an existing one:
   fusionMaterials = _app.materialLibraries.itemByName('Fusion 360 Appearance Library')
    appear = fusionMaterials.appearances.itemByName('Powder Coat (Blue)')
    bd.appearance = appear
 
Hope this helps.
Brad Bylls
0 个赞
7 条消息(共 16 条)

adminTCYL2
Enthusiast
Enthusiast

Thank you Brad, 
I allready found out that it is ruther simple. You can call things by a number or a name. You have to call the Library and in there the appearance: 

 

material = _app.materialLibraries[3].appearances.itemByName('Oak')
rootComp.bRepBodies[5].appearance = material

 

To find out the right name of the appearance you iterate trough all numbers and let outprint the names:

 

j = len(_app.materialLibraries[3].appearances)
for i in range(0,j):   
   appearanceName = _app.materialLibraries[3].appearances[i].name
   print(appearanceName) #if it does not work use messageBox
   #_ui.messageBox(appearanceName)

 

In the sample Skripts of Fusion360 there is one "ApplyAppearanceToSelection" where you can also find the names by simply running the skript. They are listed in the dropdown menues. 

0 个赞
8 条消息(共 16 条)

adminTCYL2
Enthusiast
Enthusiast

I made a little skript to generate Dove-Wood-Joints. But the wood structure should be rotated 90 degree. If it is this way the Joint would brake. Is a rotation of an appearance possible?

adminTCYL2_0-1635062997986.png

 

0 个赞
9 条消息(共 16 条)

MichaelT_123
Advisor
Advisor

Hi Mr AdminTCYL2,

 

Consider to investigate AppearanceTexture.properties. It is likely that the modification of properties:

ID=surface_anisotropy_urn

ID=surface_rotation_urn

ID=texture_WAngle
ID=texture_UScale
ID=texture_VScale
ID=texture_ScaleLock
ID=texture_RealWorldScaleX
ID=texture_RealWorldScaleY

will result in the desired result. If not, investigate other properties IDs (specific for your appearance) by their verbose description and experiment with them. There is no detailed documentation in this area, I know of. 

 

Regards

MichaelT

MichaelT
10 条消息(共 16 条)

adminTCYL2
Enthusiast
Enthusiast
Thanks for your answer and nice to hear that there are texture properties.
But I do not know how to apply them.
Most of my tries ended in the failure: .....object has no attribute....
It accepted lines like these:

material = _app.materialLibraries[3].appearances.itemByName('Oak')
material.surface_rotation_urn=adsk.core.ValueInput.createByReal(18)
new_component.bRepBodies[0].appearance = material

But the propertiy-setting had no influence on the surface.
0 个赞
11 条消息(共 16 条)

MichaelT_123
Advisor
Advisor

Hi Mr AdminTCYL2,

 

The failure is not the end of the road ... it is just the beginning of the new one.

As I indicated in my previous post, the "appearance" subject is documented only sparsely; thus, it is not a surprise that it causes some difficulties similar to blindfolded mole would face

Yes, it is a challenge ... but not unsolvable. I have no time now to crank some code example, but look at:

 

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/can-the-rgb-colour-of-a-brepbody-be-set/m-... 

 

I hope it will give you an idea of how to dig this hole ... even blindfolded.

 

Regards

MichaelT

MichaelT
0 个赞
12 条消息(共 16 条)

adminTCYL2
Enthusiast
Enthusiast

Hi MichaelT, 
Thanks for your afford. 
You link to a forum-post that got a solution that was posted in Brians blog, but this part of his blog is down. 
https://ekinssolutions.com/setting-colors-in-fusion-360/
... so this posting got no solution right now as well.... 
Where have you got this list of apperance-properties from?

0 个赞
13 条消息(共 16 条)

MichaelT_123
Advisor
Advisor

Hi Mr AdminTCYL2,

 

You asked: "Where have you got this list of apperance-properties from?"

 

Do not be nervous ... calm down ... and methodically tackle the problem.

Properties listing is the first step (and not too difficult, docs info should be sufficient) to make on the new road. This will 'fortify' you for successive strides ... to success.

If you are petrified to commit it ...  well, stand still and wait for the free ride.

 

Regards

MichaelT

 

MichaelT
0 个赞
14 条消息(共 16 条)

BrianEkins
Mentor
Mentor

My website is fixed so you can now access the blog post and the associated code sample.

 

Setting Colors in Fusion 360 Using Appearances

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 个赞
15 条消息(共 16 条)

adminTCYL2
Enthusiast
Enthusiast

Thank you Brian. 
MichaelT: Ok, properties-Listing is not too difficult.. There is a nice example here: 

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/where-to-find-specific-id-in-itembyid/m-p/...

Now I have the properties, but how do I set them?
If I do the following code, I get the message 'None' .  There is no Fusion-failure-message. So I guess I called the property in the right way. But there was not any surface_rotation setted, this is why I might get the value "None" in my message. But how do I set a rotation-value?

 

 

 material = _app.materialLibraries[3].appearances.itemByName('Oak')
    new_component.bRepBodies[0].appearance = material
testMessage = material.appearanceProperties.itemByName('surface_rotation')
_ui.messageBox(str(testMessage))   

 

 

The mean lines by setting a new collor for a appearance are in Brians example: 

 

 

colorProp = adsk.core.ColorProperty.cast(newColor.appearanceProperties.itemByName('Color'))
        colorProp.value = adsk.core.Color.create(255, 0, 0, 0)

 

But surface_rotation is an other property object type. Perhaps:    adsk.core.FloatProperty.cast(prop) ??
If I try: 

 

properti = adsk.core.FloatProperty.cast(material.appearanceProperties.itemByName('surface_rotation'))
properti.value = adsk.core.ValueInput.createByReal(18)

 

I get the failure:   'NonType' object has no attribute 'Value'

0 个赞
16 条消息(共 16 条)

adminTCYL2
Enthusiast
Enthusiast

Up there I did a failure in calling 'surface_rotation'. I used 'Name' instead of 'Id'. The call must be: 

 

material = _app.materialLibraries[3].appearances.itemByName('Oak')
new_component.bRepBodies[0].appearance = material
properti = adsk.core.FloatProperty.cast(material.appearanceProperties.itemById('surface_rotation_urn'))
properti.value = adsk.core.ValueInput.createByReal(18)

 

Now the whole thing brings this Fusion-Failure-Message: 
TypeError: in method 'FloatProperty__set_value', argument 2 of type 'double'

 

0 个赞