You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Autodesk Fusion API的findEntityByToken获取参数失败求助

问题:Design.findEntityByToken获取用户参数返回空值的解决方法

我在使用Design.findEntityByToken方法查找参数时遇到困难,已简化代码至最简形式,但仍无法获取返回值。代码中先通过名称找到名为'length'的用户参数,可正常编辑该参数;获取其entityToken后,调用findEntityByToken方法却始终返回空值。

简化后的代码如下:

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

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        design = adsk.fusion.Design.cast(app.activeProduct)
        if not design:
            ui.messageBox('No active Fusion design', 'No Design')
            return
        
        design_parameters = design.userParameters
        
        #get parameter by name
        param_n = design_parameters.itemByName('length')

        #get token from param
        token = param_n.entityToken
        ui.messageBox(f'Token: {token}')

        #find entity from token
        entity = design.findEntityByToken(token) #entity always comes back as empty
    
        #entity is known to be a user parameter -> cast to userParamter
        param = adsk.fusion.UserParameter.cast(entity)

        # Update the user parameter's expression
        param.expression = '5.0'  # Set the new value here as a string
    
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

解决方案

问题出在用户参数的entityToken不属于Design层级的可查找实体范围,Design.findEntityByToken主要用于查找几何实体(如草图、特征、组件等),而用户参数需要通过其所属的Parameters集合来查找。

修改代码中查找实体的部分,将design.findEntityByToken(token)替换为design_parameters.findEntityByToken(token)即可解决问题:

修改后的关键代码片段

#find entity from token
entity = design_parameters.findEntityByToken(token) # 使用Parameters集合的findEntityByToken方法

#entity is known to be a user parameter -> cast to userParamter
param = adsk.fusion.UserParameter.cast(entity)

完整修复后的代码

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

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        design = adsk.fusion.Design.cast(app.activeProduct)
        if not design:
            ui.messageBox('No active Fusion design', 'No Design')
            return
        
        design_parameters = design.userParameters
        
        #get parameter by name
        param_n = design_parameters.itemByName('length')

        #get token from param
        token = param_n.entityToken
        ui.messageBox(f'Token: {token}')

        #find entity from token - 使用Parameters集合的方法
        entity = design_parameters.findEntityByToken(token)
    
        #entity is known to be a user parameter -> cast to userParamter
        param = adsk.fusion.UserParameter.cast(entity)

        # Update the user parameter's expression
        param.expression = '5.0'  # Set the new value here as a string
    
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

原理说明

  • Fusion 360的API中,不同类型的实体归属不同的父集合,用户参数属于UserParameters集合(继承自Parameters),因此必须使用该集合的findEntityByToken方法才能正确匹配对应的token。
  • Design.findEntityByToken仅能查找Design直接包含的几何类实体,无法识别参数类实体的token。

内容的提问来源于stack exchange,提问作者Precast

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 18:43:19