Fusion keep changing Object name that crashes script

Fusion keep changing Object name that crashes script

spamme.post
Explorer Explorer
904 Views
4 Replies
Message 1 of 5

Fusion keep changing Object name that crashes script

spamme.post
Explorer
Explorer

Hello.

I have a tank model with simulated volume of liquid inside. I made a script that changes the parameters of the liquid height, then exports the volume of the liquid. This works fine to a point, but after a while (larger value) the span of the for loop gets smaller and smaller before it crashes.

 

From the code below, this works fine:

 

 

 

 

for i in range(1, 2000):

 

 

 

 

Exports from 1 to 1999 with no problem. Truncated txt file below:

 

 

 

 

1993 : rVolumeM3 := 6.602282964358926;
1994 : rVolumeM3 := 6.606232602450154;
1995 : rVolumeM3 := 6.610182532180514;
1996 : rVolumeM3 := 6.614132753560784;
1997 : rVolumeM3 := 6.618083266601713;
1998 : rVolumeM3 := 6.622034071314081;
1999 : rVolumeM3 := 6.625985167708646;

 

 

 

 

 

This does not work:

 

 

 

 

for i in range(2000, 3000):

 

 

 

 

 

I start the Script with this name on the object:

spammepost_0-1721805257611.png

The script runs for some minutes, then I get this error. But you can see the object changed name. I dont have any code that changes the name.

spammepost_0-1721805448963.png

This works:

 

 

for i in range(2000, 2100):

 

 

 

This crashes:

 

 

for i in range(2100, 2200):

 

 

 

This works:

 

for i in range(2100, 2150):

 

 

This crashes:

 

for i in range(2150, 2200):

 

Script:

 

 

 

import adsk.core, adsk.fusion

# 2024-07 Karstein Kvistad
# Export Volume in increments for tank sounding in PLC
# The tank level volume Body must be called "Volum" in Fusion 360

try:
    # Access the active design
    app = adsk.core.Application.get()
    design = app.activeProduct

    # Specify the desired file path and name
    file_path = "C:/temp/ensillasje6.txt"

    # Access the active component
    root_component = design.rootComponent

    # Access the parameter collection
    parameters = design.allParameters

    # Find the parameter named "TankHeight"
    tankheight_param = parameters.itemByName("TankHeigh")

    # Create and open the text file
    with open(file_path, "w") as file:
        adsk.core.Application.get().userInterface.messageBox("Export started - this might take several minutes!")
        for i in range(2000, 3000):
            # Access the solid bodies in the component and read the volume
            volumBody = root_component.bRepBodies.itemByName("Volum")
            if volumBody is None:
                raise ValueError("The 'Volum' body does not exist.")

            # Increment the parameter value
            tankheight_param.value = i / 10
            newVolume = volumBody.physicalProperties.volume
            volume_m3 = newVolume * 0.000001
            file.write(str(i) + " : " + "rVolumeM3 := " + str(volume_m3) + ";\n")

    # Display a success message
    adsk.core.Application.get().userInterface.messageBox("Export completed successfully.")

except Exception as e:
    # Return the error message in case of an exception or crash
    adsk.core.Application.get().userInterface.messageBox("An error occurred: " + str(e))

 

 

 

0 Likes
Accepted solutions (1)
905 Views
4 Replies
Replies (4)
Message 2 of 5

Jorge_Jaramillo
Collaborator
Collaborator
Accepted solution

Hi,

 

Effectively the name of the body changes when the parameter is set to 2156.

 

I was able to track it updating the parameter as the first instruction in the loop and then taking the body object as the second body in the collection instead with its name as you can see in the following code:

 

 

    # Create and open the text file
    with open(file_path, "w") as file:
        app.log("Export started - this might take several minutes!")
        for i in range(2150, 2200):
            # Increment the parameter value
            tankheight_param.value = i / 10
            adsk.doEvents()

            # Access the solid bodies in the component and read the volume
            # volumBody = root_component.bRepBodies.itemByName("Volum")
            volumBody = root_component.bRepBodies[1]
            if volumBody is None:
                raise ValueError("The 'Volum' body does not exist.")

            newVolume = volumBody.physicalProperties.volume
            volume_m3 = newVolume * 0.000001
            app.log(str(i) + " : " + "rVolumeM3 := " + str(volume_m3) + "     " + volumBody.name)
            file.write(str(i) + " : " + "rVolumeM3 := " + str(volume_m3) + ";\n")

 

 

 

This are the results I got:

 

 

 Export started - this might take several minutes!
 2150 : rVolumeM3 := 7.305292262562127     Volum
 2151 : rVolumeM3 := 7.309323358256362     Volum
 2152 : rVolumeM3 := 7.313354748581711     Volum
 2153 : rVolumeM3 := 7.317386433548954     Volum
 2154 : rVolumeM3 := 7.3214184131688524     Volum
 2155 : rVolumeM3 := 7.3254506874521725     Volum
 2156 : rVolumeM3 := 7.329483244063971     Body5
 2157 : rVolumeM3 := 7.333515935768438     Body5
 2158 : rVolumeM3 := 7.337548566950882     Body5
 2159 : rVolumeM3 := 7.341581028294201     Body5
 2160 : rVolumeM3 := 7.345613317229574     Body5
 2161 : rVolumeM3 := 7.349645433760686     Body5
 2162 : rVolumeM3 := 7.353677377891222     Body5
 2163 : rVolumeM3 := 7.357709149624869     Body5
 2164 : rVolumeM3 := 7.361740748965311     Body5
 2165 : rVolumeM3 := 7.365772175916233     Body5
 2166 : rVolumeM3 := 7.369803430481324     Body5
 2167 : rVolumeM3 := 7.3738345126642715     Body5
 2168 : rVolumeM3 := 7.377865422468746     Body5
 2169 : rVolumeM3 := 7.381896159898459     Body5
 2170 : rVolumeM3 := 7.385926724957067     Body5
 2171 : rVolumeM3 := 7.389957117648285     Body5
 2172 : rVolumeM3 := 7.393987337975779     Body5
 2173 : rVolumeM3 := 7.398017385943241     Body5
 2174 : rVolumeM3 := 7.402047261554355     Body5
 2175 : rVolumeM3 := 7.406076964812807     Body5
 2176 : rVolumeM3 := 7.410106495722288     Body5
 2177 : rVolumeM3 := 7.414135854286474     Body5
 2178 : rVolumeM3 := 7.418165040509056     Body5
 2179 : rVolumeM3 := 7.422194054393725     Body5
 2180 : rVolumeM3 := 7.426222895944155     Body5
 2181 : rVolumeM3 := 7.430251565164042     Body5
 2182 : rVolumeM3 := 7.434280062057068     Body5
 2183 : rVolumeM3 := 7.438308386626919     Body5
 2184 : rVolumeM3 := 7.44233653887728     Body5
 2185 : rVolumeM3 := 7.446364518811831     Body5
 2186 : rVolumeM3 := 7.450392326434273     Body5
 2187 : rVolumeM3 := 7.454419961748279     Body5
 2188 : rVolumeM3 := 7.458447424757536     Body5
 2189 : rVolumeM3 := 7.462474715465731     Body5
 2190 : rVolumeM3 := 7.466501833876559     Body5
 2191 : rVolumeM3 := 7.470528779993692     Body5
 2192 : rVolumeM3 := 7.474555553820818     Body5
 2193 : rVolumeM3 := 7.478582155361633     Body5
 2194 : rVolumeM3 := 7.482608584619814     Body5
 2195 : rVolumeM3 := 7.486634841599042     Body5
 2196 : rVolumeM3 := 7.490660926303014     Body5
 2197 : rVolumeM3 := 7.494686838735407     Body5
 2198 : rVolumeM3 := 7.498712578899919     Body5
 2199 : rVolumeM3 := 7.50273814680022     Body5
 Export completed successfully.

 

 

 

Very extrange how it changes, but at least with this change you can complete your measurements.

I was able to run it up to 3000 without any issue.

 

Finally, it's a good practice to call adsk.doEvents() inside a loop when the model is updated, just to help the user interface to be updated with the changes made.

 

Regards,

Jorge Jaramillo

 

Message 3 of 5

spamme.post
Explorer
Explorer

Yeah, still changes. Did not know you could address the object like arrays - nice! Tried to export from 10 - 4400. Still crashed at 2176,00 mm. Weird. Started a new one from 2176 - 4400 that completed.

spammepost_0-1721971235969.png

This one was also practical as I can see the realtime filling of the tank, instead of Fusion 360 half blacked out looking like it would crash any minute.

 

adsk.doEvents()

 

(view in My Videos)
 

0 Likes
Message 4 of 5

Jorge_Jaramillo
Collaborator
Collaborator
In my new test with range 10 - 4400 it run without any issue.
Did you remove the body reference by its name at all?

I also notice that the value 2156 is when it reaches the middle of barrel, just when the external profile changes its curvature.

Regards,
Jorge Jaramillo
0 Likes
Message 5 of 5

spamme.post
Explorer
Explorer

Yeah, the code is like this now - and I started the script with Body[1] called Body8 i Fusion.
:

 

    # Create and open the text file
    with open(file_path, "w") as file:
        adsk.core.Application.get().userInterface.messageBox("Export started - this might take several minutes! \n If it crashes, rename the export file and start a new export from the line that crashes.")
        for i in range(10, 4400):
            # Access the solid bodies in the component and read the volume
            volumBody = root_component.bRepBodies[1]
            #volumBody = root_component.bRepBodies.itemByName("Volum")
            if volumBody is None:
                raise ValueError("The Volum body does not exist.")

            # Increment the parameter value
            tankheight_param.value = i / 10

            # Updates the Fusion 360 screen
            adsk.doEvents()

            # Change scale to m3
            newVolume = volumBody.physicalProperties.volume
            volume_m3 = newVolume * 0.000001

            # Write calculation to file
            file.write(str(i) + " : " + "rVolumeM3 := " + str(volume_m3) + ";\n")

 

0 Likes