Downloading specific tool library versions isn't working in script

Downloading specific tool library versions isn't working in script

alexeiE6G4T
Contributor Contributor
229 Views
0 Replies
Message 1 of 1

Downloading specific tool library versions isn't working in script

alexeiE6G4T
Contributor
Contributor

So, I have this script that goes to the CAMTools folder (Home > Assets > CAMTools) and allows a user to select a library to view a diff (comparison) of what changed from one version to another.

Screenshot 2024-04-25 at 12.25.19 PM.png Screenshot 2024-04-25 at 12.28.32 PM.png

 

However, when the versions selected are downloaded, they both turn out to be identical. In fact, they are only ever the most recent version in spite of selecting earlier (but different) versions. I expected that listing through previous versions, represented by `adsk.core.DataFile`, the download method would give me that version of the (*.json) file. I'm I misunderstanding something about this the download method?

 

I'll include the relevant parts of the script if someone want's to test this:

 

def getToolsFolder():
    for dp in app.data.dataProjects:
        if dp.name == 'Assets':
            return dp.rootFolder.dataFolders.itemByName('CAMTools')
    return None

toolsFolder = None
toolBucketNameList = []
toolBucketDropdown: adsk.core.DropDownCommandInput = None
toolBucketVersionADropdown: adsk.core.DropDownCommandInput = None
toolBucketVersionBDropdown: adsk.core.DropDownCommandInput = None
toolBucketSelected: adsk.core.DataFile = None
toolBucketIdByVersion = {}
toolVersionASelection: adsk.core.DataFile = None
toolVersionBSelection: adsk.core.DataFile = None
previousToolBucket: str = None

# Executed when add-in is run.
def start():
    global toolBucketNameList, toolsFolder
    toolsFolder = getToolsFolder()
    toolBucketNameList = [t.name for t in toolsFolder.dataFiles]

    # Create a command Definition.
    cmd_def = ui.commandDefinitions.addButtonDefinition(CMD_ID, CMD_NAME, CMD_Description, ICON_FOLDER)

    # Define an event handler for the command created event. It will be called when the button is clicked.
    futil.add_handler(cmd_def.commandCreated, command_created)

    # ******** Add a button into the UI so the user can run the command. ********
    # Get the target workspace the button will be created in.
    workspace = ui.workspaces.itemById(WORKSPACE_ID)

    # Get the panel the button will be created in.
    panel = workspace.toolbarPanels.itemById(PANEL_ID)

    # Create the button command control in the UI after the specified existing command.
    control = panel.controls.addCommand(cmd_def, COMMAND_BESIDE_ID, False)

    # Specify if the command is promoted to the main toolbar. 
    control.isPromoted = IS_PROMOTED

def command_created(args: adsk.core.CommandCreatedEventArgs):
    global toolBucketVersionADropdown, toolBucketVersionBDropdown, toolBucketDropdown
    futil.log(f'{CMD_NAME} Command Created Event')

    inputs = args.command.commandInputs
    toolBucketDropdown = inputs.addDropDownCommandInput('tool_lib', 'Library', adsk.core.DropDownStyles.TextListDropDownStyle)
    for t in toolBucketNameList:
        toolBucketDropdown.listItems.add(t, False)
    
    toolBucketVersionADropdown = inputs.addDropDownCommandInput('tool_version_a', 'Version A', adsk.core.DropDownStyles.TextListDropDownStyle)
    toolBucketVersionADropdown.isEnabled = False
    toolBucketVersionBDropdown = inputs.addDropDownCommandInput('tool_version_b', 'Version B', adsk.core.DropDownStyles.TextListDropDownStyle)
    toolBucketVersionBDropdown.isEnabled = False

    futil.add_handler(args.command.execute, command_execute, local_handlers=local_handlers)
    futil.add_handler(args.command.inputChanged, command_input_changed, local_handlers=local_handlers)
    futil.add_handler(args.command.validateInputs, command_validate_input, local_handlers=local_handlers)
    futil.add_handler(args.command.destroy, command_destroy, local_handlers=local_handlers)

def command_execute(args: adsk.core.CommandEventArgs):
    # General logging for debug.
    futil.log(f'{CMD_NAME} Command Execute Event')

    if toolVersionASelection and toolVersionBSelection:
        toolVersionAName = toolVersionASelection.name.split('.json')[0]
        toolVersionAPath = os.path.join(os.path.dirname(os.path.abspath(__file__)), f'{toolVersionAName}_{toolVersionASelection.versionNumber}.json')
        toolVersionASelection.download(toolVersionAPath, None) # these appear to download the latest NOT the specific version I want

        toolVersionBName = toolVersionBSelection.name.split('.json')[0]
        toolVersionBPath = os.path.join(os.path.dirname(os.path.abspath(__file__)), f'{toolVersionBName}_{toolVersionBSelection.versionNumber}.json')
        toolVersionBSelection.download(toolVersionBPath, None) # these appear to download the latest NOT the specific version I want


def getToolFileByName(name):
    for t in toolsFolder.dataFiles:
        if t.name == name:
            return t
    return None

def command_input_changed(args: adsk.core.InputChangedEventArgs):
    global toolBucketVersionADropdown, toolBucketVersionBDropdown, previousToolBucket, toolBucketSelected, toolVersionASelection, toolVersionBSelection
    valueInput = args.input

    if not valueInput:
        return
    
    if valueInput.id == 'tool_lib': # handles thee selection of the tool library
        if valueInput.selectedItem is not None and previousToolBucket != valueInput.selectedItem:
            if valueInput.selectedItem.name not in toolBucketIdByVersion:
                toolBucketIdByVersion[valueInput.selectedItem.name] = {}
            toolsByVersionObj = toolBucketIdByVersion[valueInput.selectedItem.name]
            toolSelection = valueInput.selectedItem.name
            previousToolBucket = toolSelection
            toolBucketSelected = getToolFileByName(toolSelection)
            toolsByVersionObj[toolBucketSelected.versionNumber] = toolBucketSelected.id
            if toolBucketSelected is not None:
                toolBucketVersionADropdown.listItems.clear()
                toolBucketVersionBDropdown.listItems.clear()
                lastVersion = toolBucketSelected.versionNumber
                for v in range(10): # the last 10 only, in case you have a lot of versions
                    version = lastVersion - v
                    if version > 0:
                        toolBucketVersionADropdown.listItems.add(str(version), False)
                        toolBucketVersionBDropdown.listItems.add(str(version), False)
                        
    elif valueInput.id == 'tool_version_a': # handles the selection of the tool version A
        futil.log(f'{CMD_NAME} Input Changed Event fired from a change to {valueInput.id}')
        for a in toolBucketSelected.versions:
            if str(a.versionNumber) == valueInput.selectedItem.name:
                futil.log(f'{CMD_NAME} Selected version: {a.versionNumber}')
                toolVersionASelection = a
                break

    elif valueInput.id == 'tool_version_b': # handles the selection of the tool version B
        futil.log(f'{CMD_NAME} Input Changed Event fired from a change to {valueInput.id}')
        for b in toolBucketSelected.versions:
            if str(b.versionNumber) == valueInput.selectedItem.name:
                futil.log(f'{CMD_NAME} Selected version: {b.versionNumber}')
                toolVersionBSelection = b
                break

def command_validate_input(args: adsk.core.ValidateInputsEventArgs):
    global toolBucketVersionADropdown, toolBucketVersionBDropdown

    inputs = args.inputs
    
    tool_lib = inputs.itemById('tool_lib')
    tool_version_a = inputs.itemById('tool_version_a')
    tool_version_b = inputs.itemById('tool_version_b')
    
    if tool_lib.selectedItem is not None:
        toolBucketVersionADropdown.isEnabled = True
        toolBucketVersionBDropdown.isEnabled = True
    else:
        toolBucketVersionADropdown.isEnabled = False
        toolBucketVersionBDropdown.isEnabled = False

    if tool_lib.selectedItem is not None and tool_version_a.selectedItem is not None and tool_version_b.selectedItem is not None:
        args.areInputsValid = True
    else:
        args.areInputsValid = False

 

 

This script should download the files right next to the file this is executed from (`commands/commandDialog/entry.py`)

 

Does this seem like a bug, or an undocumented feature?

0 Likes
230 Views
0 Replies
Replies (0)