I'm really hoping someone can verify this is a bug, or even better point me in the right direction!
After too many hours doing experiments, I have failed find any API that works to get a parameter value from a derived model (meaning, a model inserted via insert-derive). Even though the Fusion UI shows the parameters (marked as favorite in the base model, and even in the inheriting model), and I can type the value into a field with auto-completion, I am unable to access the values from an API.
The core problem comes down to the following:
1) The "<activeProduct>.design.allParameters" collection does not include the insert-derive model parameters
2) Using <design>.allComponents[...] you can find the model, and access items such as bodies and sketches, but the modelParameters collection is empty
3) I've not found a way to access a Design of a component, and it is Design that has userParameters and allParameters (components only have modelParameters). If I use component.parentDesign, it points to the base design (same as <activeProduct>.design.allParameters) not a design object that reflects the insert-derive component.
Maybe not worth much, but if the base derive model is (or was recently) open, you can find it under <app>.documents[...].products[...] and in that case, allParameters[...] works fine. That of course means you not only have to have the derive document open, you also would need to make sure it is the same version. Not really a viable solution. Likelywise, you can use adsk.core.dataFile to locate the various components (once saved) but no access to models/parameters.
The problem can be seen using the attached model, and the following code. There are two models:
- DeriveBase (which is the object that will get inserted into the second model) that has many parameters of different types (user, model, favorite and not).
- ContainsDeriveModel, which simply has DeriveBase inserted into it (using insert-derive), along with a single User Parameter just to make sure the code is working.
With ContainsDeriveModel open, run the script and you will see the popups showing that the derive model / user parameters are not available. The user parameter directly attached to ContainsDeriveModel appears, but nothing else.
import adsk.core, adsk.fusion, adsk.cam, traceback
def run(context):
global ui, app, design
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
design = adsk.fusion.Design.cast(app.activeProduct)
showCollection('design.appParameters', design.allParameters)
for component in design.allComponents:
showCollection('Component {}'.format(component.name), component.modelParameters)
# Directly access parameters - uncomment to test
# checkParam('LocalUserParameter')
# checkParam('DerivedFavorite_Ref')
# checkParam('DerivedModelParameter_Ref')
# checkParam('DerivedFavoriteModelParameter_Ref')
# checkParam('DerivedUserParameter_Ref')
# checkParam('d5_Ref')
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def checkParam(name):
try:
param = design.allParameters.itemByName(name)
ui.messageBox(param.name + ': ' + param.expression)
except:
ui.messageBox("Unable to locate {}".format(name))
def showCollection(name, collection):
first = True
message = '{} ({} items): '.format(name, collection.count)
for item in collection:
message += ('' if first else ', ') + item.name
first = False
ui.messageBox(message)
Thanks in advance for any help,
Chris