Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Failed: Traceback ... InternalValidationError: renameTarget

1 REPLY 1
SOLVED
Reply
Message 1 of 2
Anonymous
173 Views, 1 Reply

Failed: Traceback ... InternalValidationError: renameTarget

Hi, I created an assembly that I want to suppress some groups of the assembly based on the parameters.

Here is the full code:

#Author-Jeronimo Fernandez
#Description-

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

app = None
ui  = None
rowNumber = 0
widhValue = 0.0
heightValue = 0.0
divisionsWidh = 0
divisionsHeight = 0
supportsWidh = 0
supportsHeight = 0
numArtVal = 0
numHooksVal =0

# Global set of event handlers to keep them referenced for the duration of the command
handlers = []

def run(context):
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface

        des :adsk.fusion.Design = app.activeProduct
        
        #Definiendo globales
        global widhValue
        global heightValue
        global divisionsWidh
        global divisionsHeight
        global supportsWidh
        global supportsHeight
        global numArtVal
        global numHooksVal
    
        # Getting the current values of the parameters
        widhValue = des.userParameters.itemByName('widh').value
        heightValue = des.userParameters.itemByName('height').value
        divisionsWidh = des.userParameters.itemByName('divisionWidh').value
        divisionsHeight = des.userParameters.itemByName('divisionHeight').value
        supportsWidh = des.userParameters.itemByName('supportWidh').value
        supportsHeight = des.userParameters.itemByName('supportHeight').value
        numArtVal = des.userParameters.itemByName('numArt').value
        numHooksVal = des.userParameters.itemByName('numHooks').value

        #Creating input
        # Get the existing command definition or create it if it doesn't already exist.
        cmdDef = ui.commandDefinitions.itemById('M-BOX Parameters')
        if not cmdDef:
            cmdDef = ui.commandDefinitions.addButtonDefinition('M-BOX Parameters', 'M-BOX Parameters', 'To input the parameters of the M-BOX')
        # Connect to the command created event.
        onCommandCreated = MyCommandCreatedHandler()
        cmdDef.commandCreated.add(onCommandCreated)
        handlers.append(onCommandCreated)

        # Execute the command definition.
        cmdDef.execute()

        # Prevent this module from being terminated when the script returns, because we are waiting for event handlers to fire.
        adsk.autoTerminate(False)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def findByLikeName(
    tl :adsk.fusion.Timeline,
    name :str) -> list:

    import re

    lst = []
    tlo = adsk.fusion.TimelineObject.cast(None)
    for tlo in tl:
        if re.search(name, tlo.name):
            lst.append(tlo)

    return lst

# Event handler that reacts when the command definitio is executed which
# results in the command being created and this event being fired.
class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            app = adsk.core.Application.get()
            ui  = app.userInterface

            # Get the command that was created.
            cmd = adsk.core.Command.cast(args.command)

            # Connect to the command destroyed event.
            onDestroy = MyCommandDestroyHandler()
            cmd.destroy.add(onDestroy)
            handlers.append(onDestroy)

            # Get the CommandInputs collection associated with the command.
            inputs = cmd.commandInputs

            # Create the inputs.
            inputs.addValueInput('Widh', 'Widh', 'mm', adsk.core.ValueInput.createByReal(widhValue))
            inputs.addValueInput('Height', 'Height', 'mm', adsk.core.ValueInput.createByReal(heightValue))
            inputs.addValueInput('divWidh','Number of cuts, Widh','',adsk.core.ValueInput.createByReal(divisionsWidh))
            inputs.addValueInput('divHeight','Number of cuts, Height','',adsk.core.ValueInput.createByReal(divisionsHeight))
            inputs.addValueInput('supWidh','Number of supports, Widh','',adsk.core.ValueInput.createByReal(supportsWidh))
            inputs.addValueInput('supHeight','Number of supports, Height','',adsk.core.ValueInput.createByReal(supportsHeight))
            inputs.addValueInput('NumArt','Number of Art-Foot at lower Frame Profile','',adsk.core.ValueInput.createByReal(numArtVal))
            inputs.addValueInput('NumHooks','Number of Hooks at upper Frame Profile','',adsk.core.ValueInput.createByReal(numHooksVal))

        except:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

# Event handler that reacts to when the command is destroyed. This terminates the script.            
class MyCommandDestroyHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            app = adsk.core.Application.get()
            ui  = app.userInterface

            # Definiendo variables globales
            global widhValue
            global heightValue
            global divisionsWidh
            global divisionsHeight
            global supportsWidh
            global supportsHeight
            global numArtVal
            global numHooksVal

            # When the command is done, terminate the script
            # This will release all globals which will remove all event handlers
            # Getting the values
            widhValue = adsk.core.Command.cast(args.command).commandInputs.itemById('Widh').value
            heightValue = adsk.core.Command.cast(args.command).commandInputs.itemById('Height').value
            divisionsWidh = adsk.core.Command.cast(args.command).commandInputs.itemById('divWidh').value
            divisionsHeight = adsk.core.Command.cast(args.command).commandInputs.itemById('divHeight').value
            supportsWidh = adsk.core.Command.cast(args.command).commandInputs.itemById('supWidh').value
            supportsHeight = adsk.core.Command.cast(args.command).commandInputs.itemById('supHeight').value
            numArtVal = adsk.core.Command.cast(args.command).commandInputs.itemById('NumArt').value
            numHooksVal = adsk.core.Command.cast(args.command).commandInputs.itemById('NumHooks').value
            
            # Executing the original script
            mannageParameters()

            adsk.terminate()
        except:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

# Manejando los parametros
def mannageParameters():
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface

        des :adsk.fusion.Design = app.activeProduct

        prmWidh :adsk.fusion.UserParameter = des.userParameters.itemByName('widh')
        prmHeight :adsk.fusion.UserParameter = des.userParameters.itemByName('height')
        prmDivisionWidh :adsk.fusion.UserParameter = des.userParameters.itemByName('divisionWidh')
        prmDivisionHeight :adsk.fusion.UserParameter = des.userParameters.itemByName('divisionHeight')
        prmSupportWidh :adsk.fusion.UserParameter = des.userParameters.itemByName('supportWidh')
        prmSupportHeight :adsk.fusion.UserParameter = des.userParameters.itemByName('supportHeight')
        prmNumArt :adsk.fusion.UserParameter = des.userParameters.itemByName('numArt')
        prmNumHooks :adsk.fusion.UserParameter = des.userParameters.itemByName('numHooks')

        # get timeline
        tl :adsk.fusion.Timeline = des.timeline

        # Get the group with the name of Key
        # Supports
        # Support W
        key = 'Support W Group'
        wSupportGroup = findByLikeName(tl, key)
        # Support H
        key = 'Support H Group'
        hSupportGroup = findByLikeName(tl, key)

        # Divisions
        # Divisions W
        key = 'Division W Group'
        wDivisionGroup = findByLikeName(tl, key)
        # Division H
        key = 'Division H Group'
        hDivisionGroup = findByLikeName(tl, key)

        # Hooks and Art
        # Art
        key = 'Art Group'
        artGroup = findByLikeName(tl, key)
        # Division H
        key = 'Hooks Group'
        hooksGroup = findByLikeName(tl, key)
        
        # Conditionals Supports
        # Supports W
        if supportsWidh == 0:
            wSupportGroup[0].isSuppressed = True
            prmSupportWidh.value = supportsWidh
        else:
            prmSupportWidh.value = supportsWidh
            wSupportGroup[0].isSuppressed = False
        # Supports H
        if supportsHeight == 0:
            hSupportGroup[0].isSuppressed = True
            prmSupportHeight.value = supportsHeight
        else:
            prmSupportHeight.value = supportsHeight
            hSupportGroup[0].isSuppressed = False
            
        
        # Conditionals Divisions
        # Divisions H
        if divisionsHeight == 0:
            hDivisionGroup[0].isSuppressed = True
            prmDivisionHeight.value = divisionsHeight
        else:
            prmDivisionHeight.value = divisionsHeight
            hDivisionGroup[0].isSuppressed = False
        # Divisions W
        if divisionsWidh == 0:
            wDivisionGroup[0].isSuppressed = True 
            prmDivisionWidh.value = divisionsWidh
        else:
            prmDivisionWidh.value = divisionsWidh
            wDivisionGroup[0].isSuppressed = False
        
        # Conditionals Hooks and art
        # Art
        if numArtVal == 0:
            artGroup[0].isSuppressed = True
            prmNumArt.value = numArtVal
        else:
            prmNumArt.value = numArtVal
            artGroup[0].isSuppressed = False
        # Hooks
        if numHooksVal == 0:
            hooksGroup[0].isSuppressed = True 
            prmNumHooks.value = numHooksVal
        else:
            prmNumHooks.value = numHooksVal
            hooksGroup[0].isSuppressed = False
        
        # Definiendo los nuevos parametros
        prmWidh.value = widhValue
        prmHeight.value = heightValue

        adsk.doEvents()

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

When I run the code I get this error window:

jeronimofernandez95_0-1625189801734.png

 

I've tried changing the name of the group, changing the name of the variable and using another similar code, but nothing seems to work. I always get the error when trying to use the "findByLinkName" function.

 

I hope you're able to help me and thank you in advance.

Labels (3)
1 REPLY 1
Message 2 of 2
Anonymous
in reply to: Anonymous

Hi again,

 

I just solved the problem. My code was right, the problem was in my assembly. It appears that you can use long names for the components, I had a component of a subassembly that was really long, as soon as I changed it the code worked.

 

The previous name was: "_WUERTH 00896 14 counter sunk screw DIN 7991 M6x14 10.9 BLK"

And I changed it to: "Counter sunk screw DIN 7991 M6x14 10.9 BLK"

I don't know which is the limit, but this amount of characters worked for me.

 

I hope this post make sense and helps others with a similar issue.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report