Message 1 of 10
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Trying to write myself a script that will read a CSV file that contains a list of components - and their desired Appearances and then traverse a model to apply the desired Appearance.
I have hit a bit of a snag as follows:
* once the Appearance is applied to an occurrence, I attempt to read back the occurrence.appearance.name I receive an error AttributeError: 'NoneType' object has no attribute 'name'
Refer approx line 71 in code below.
The API manual suggests an occurrence has an appearance property - that returns an appearance object - which has a corresponding name. Note the processing of setting the new apperance functions successfully.
import adsk.core, adsk.fusion, adsk.cam, traceback
app = None
ui = None
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
fileDialog = ui.createFileDialog()
fileDialog.title = "Select Appearance File"
fileDialog.isMultiSelectEnabled = False
result = fileDialog.showOpen()
if result != adsk.core.DialogResults.DialogOK:
return
else:
filename = fileDialog.filename
result = ui.messageBox('Yes = Create points from file\nNo = Update ponits from file',
'Control Points From File',
adsk.core.MessageBoxButtonTypes.YesNoCancelButtonType,
adsk.core.MessageBoxIconTypes.QuestionIconType)
if result == adsk.core.DialogResults.DialogCancel:
return
elif result == adsk.core.DialogResults.DialogYes:
ProcessAppearanceFile(filename)
elif result == adsk.core.DialogResults.DialogNo:
#UpdatePoints(filename)
return
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def ProcessAppearanceFile(filename):
try:
app = adsk.core.Application.get()
ui = app.userInterface
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
root = design.rootComponent
occs = []
ui.palettes.itemById('TextCommands').writeText("Debugging")
occs = root.allOccurrences
doc = app.activeDocument.name
if len(occs) == 0:
ui.messageBox('There are no components in this design.')
return
# Open the appearances file
with open(filename) as f:
for line in f:
line = line.strip()
appearances = line.split(',')
componentName = appearances[0]
componentAppearance = getAppearance(appearances[1])
for occ in occs:
if occ.component.name == componentName:
occ.appearance = componentAppearance
# THIS IS LINE THAT FAILS
ui.palettes.itemById('TextCommands').writeText("New appearance " + occ.appearance.name)
# END OF LINE THAT FAILS
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def getAppearance(appearanceName):
app = adsk.core.Application.get()
ui = app.userInterface
materialLibs = app.materialLibraries
appearance = None
for materialLib in materialLibs:
appearances = materialLib.appearances
try:
appearance = appearances.itemByName(appearanceName)
ui.palettes.itemById('TextCommands').writeText(appearance.name)
except:
pass
if appearance:
break
return appearance
Solved! Go to Solution.