Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to change material and disable shadows with API

3 REPLIES 3
Reply
Message 1 of 4
addoagrucu
342 Views, 3 Replies

How to change material and disable shadows with API

Hello,

 

I wrote a script that produces the following 4 views as .pngs given an input .step file:


Front_render.pngTop_render.pngRight_render.pngIso_render.png

 

I am having trouble programmatically changing the material properties of this object to produce the render I want. (RGB Color, Roughness). I am also having trouble programmatically turning off the shadow on the ground in the isometric view, and the shadows that exist in all pictures near bends and edges.

Here's my full script. I would be very thankful if anyone could provide some guidance on my issue. Thank you very much.

 

 

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

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

        # Define file path and output directory
        file_path = r'pathto.step'
        output_folder = r'pathtoutputfolder'

        # Import the STEP file
        stepOptions = importManager.createSTEPImportOptions(file_path)
        rootComp = design.rootComponent
        importManager.importToTarget(stepOptions, rootComp)

        # Load a local material library and get an appearance
        materialLibs = app.materialLibraries
        matLib = materialLibs.load(r'pathto\APISampleMaterialLibrary2.adsklib')
        appear = matLib.appearances.itemByName("Steel - Satin")  # Adjust appearance name as necessary

        # Copy the appearance into the design
        newAppear = design.appearances.addByCopy(appear, f'{appear.name}_Copied')

        # Apply the appearance to all bodies
        allBodies = rootComp.bRepBodies
        for body in allBodies:
            body.appearance = newAppear

        # Setup camera
        camera = app.activeViewport.camera
        camera.cameraType = adsk.core.CameraTypes.OrthographicCameraType

        # Define views
        views = {
            'Front': adsk.core.ViewOrientations.FrontViewOrientation,
            'Top': adsk.core.ViewOrientations.TopViewOrientation,
            'Right': adsk.core.ViewOrientations.RightViewOrientation,
            'Iso': adsk.core.ViewOrientations.IsoTopRightViewOrientation
        }

        # Adjust scene settings for no specular reflections and no shadows
        renderManager = design.renderManager
        sceneSettings = renderManager.sceneSettings
        sceneSettings.backgroundSolidColor = adsk.core.Color.create(255, 255, 255, 0)  # White background
        sceneSettings.ambientLightIntensity = 0.8
        sceneSettings.reflectionsEnabled = False
        sceneSettings.shadowsEnabled = False

        # Process each view
        for view_name, orientation in views.items():
            camera.viewOrientation = orientation
            app.activeViewport.camera = camera
            app.activeViewport.refresh()
            app.activeViewport.fit()

            sceneSettings.shadowsEnabled = False

            # Save images
            image_path = os.path.join(output_folder, f"{view_name}_render.png")
            app.activeViewport.saveAsImageFile(image_path, 1920, 1080)

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

 

Labels (6)
3 REPLIES 3
Message 2 of 4
kandennti
in reply to: addoagrucu

Hi @addoagrucu -San.

 

That sounds like an interesting attempt.

The viewOrientation property has been unresponsive for a long time and still seems to be unfixed.
Fixing it to use a vector to change the camera orientation instead seems to give the desired result.

・・・
        # Define views
        views = {
            'Front': {
                "up": (0,0,1),
                "front": (0,1,0)
            },
            'Top': {
                "up": (0,1,0),
                "front": (0,0,-1)
            },
            'Right': {
                "up": (0,0,1),
                "front": (-1,0,0)
            },
            'Iso': {
                "up": (0, 4.440892098500626e-16,1),
                "front": (-0.577350269189626, 0.5773502691896257, -0.5773502691896257)
            },
        }

        # Adjust scene settings for no specular reflections and no shadows
        renderManager = design.renderManager
        sceneSettings = renderManager.sceneSettings
        sceneSettings.backgroundSolidColor = adsk.core.Color.create(255, 255, 255, 0)  # White background
        sceneSettings.ambientLightIntensity = 0.8
        sceneSettings.reflectionsEnabled = False
        sceneSettings.shadowsEnabled = False

        # Process each view
        for view_name, orientationVec in views.items():
            camera.upVector = adsk.core.Vector3D.create(
                *orientationVec["up"]
            )
            frontVec: adsk.core.Vector3D = adsk.core.Vector3D.create(
                *orientationVec["front"]
            )
            pnt: adsk.core.Point3D = camera.eye.copy()
            pnt.translateBy(frontVec)
            camera.target = pnt
            app.activeViewport.camera = camera

            app.activeViewport.refresh()
            app.activeViewport.fit()

            # Save images
            image_path = os.path.join(output_folder, f"{view_name}_render.png")
            app.activeViewport.saveAsImageFile(image_path, 1920, 1080)
・・・
Message 3 of 4
addoagrucu
in reply to: addoagrucu

Dear @kandennti -San

 

Thank you for your response. I did have some problems with the viewOrientation route, although the version of the code I've posted does seem to work on it's own. Although this is the case, I will keep your solution in mind if I encounter further difficulties with the viewOrientation route. I did try your code, and I get the equivalent of my code's output.

 

Are you perhaps familiar with setting colors/materials through the API? I identify brep bodies and loop through them, assigning a material to them just like in this example:
https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-d00ba5c2-d3f0-11e6-bd5a-989096c57042

 

This seems to have no effect. So does turning off shadows.

 

If you know about this domain, I would very much be interested to know what you know.

PS. I am attaching the .step file I'm using:
https://filebin.net/3z0baa056u7urhff

 

Thank you  

Message 4 of 4
BrianEkins
in reply to: addoagrucu

Maybe this will help with assigning materials.

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

 

The RenderManager you use to change the settings is only used when ray tracing in the Render workspace. The settings in the picture below are not directly exposed by the API.

BrianEkins_0-1714178209982.png

However, this is a workaround where you can use the API to drive the user interface and modify these settings. For example to turn off "Ground Shadow" you can use the following:

 

# Get the command that defines the view effects.
cmd = ui.commandDefinitions.itemById('ViewEffectCommand')

# Get the control definition associated with the command.
viewEffectsCntrlDef = cmd.controlDefinition

# The ControlDefinition, in this case, is a ListControlDefinition, and each
# item in the list is a view effect setting. Each item has a name and their
# order will be the same as in the UI, so it should be consistent. This gets
# the third item (0 is the first), which controls if there is a ground 
# shadow or not.
groundShadowListItem = cntrlDef.listItems[2]

# Turn off ground shadows.
groundShadowListItem.isSelected = False

 

You can modify the other settings in the same way.

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report