Python Script - Set Part Colour Without Selecting The Body First

Python Script - Set Part Colour Without Selecting The Body First

isocam
Collaborator Collaborator
636 Views
1 Reply
Message 1 of 2

Python Script - Set Part Colour Without Selecting The Body First

isocam
Collaborator
Collaborator

Can anybody help?

 

Please see the attached Python script.

 

It works fine, but I have to manually select the part firstly!

 

Can anybody modify the Python script so, when I run the script, the part is automatically selected rather than me having to manually select it?

 

Many thanks in advance!

 

Darren

0 Likes
Accepted solutions (1)
637 Views
1 Reply
Reply (1)
Message 2 of 2

Rushikesh.kadam
Autodesk
Autodesk
Accepted solution

@isocam I assume you want to apply color to all the bodies. If yes, you need to loop using bRepBodies. Refer to the below code. The code will apply color to all the bodies under the root component only. You need to do a similar loop if the bodies are under the component.

If want to apply to the specific body under the root component then uncomment line 27 and replace the number with the correct body number, and comment the for loop.

 

import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    ui = None
    try:
        PartColour = "Bright Reduuu"

        Red = 255

        Green = 0

        Blue = 0

        SetPartColour(PartColour, Red, Green, Blue)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def SetPartColour(PartColour, Red, Green, Blue):
    app = adsk.core.Application.get()

    ui  = app.userInterface

    design = adsk.fusion.Design.cast(app.activeProduct)

    # if you want to select specific body replace the number.
    # design.rootComponent.bRepBodies.item(0)

    # using for loop to apply color to all body present under root component. 
    for body in design.rootComponent.bRepBodies:

        favoriteAppearances = design.appearances

        try:
            myColor = favoriteAppearances.itemByName(PartColour)
        except:
            myColor = None

        if myColor:
            body.appearance = myColor
        else:
            fusionMaterials = app.materialLibraries.itemByName('Fusion 360 Appearance Library')

            yellowColor = fusionMaterials.appearances.itemByName('Paint - Enamel Glossy (Yellow)')

            newColor = design.appearances.addByCopy(yellowColor, PartColour)

            colorProp = adsk.core.ColorProperty.cast(newColor.appearanceProperties.itemByName('Color'))

            colorProp.value = adsk.core.Color.create(Red, Green, Blue, 0)

            body.appearance = newColor

 

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

If my reply was helpful, please click the "Accept as Solution" button. It helps others get the answer quickly! A "Like" is always welcomed.




Rushikesh Kadam
Senior QA Engineer
Quality Assurance
Autodesk, Inc.


0 Likes