How to change material and disable shadows with API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello,
I wrote a script that produces the following 4 views as .pngs given an input .step file:
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()))