Rename component via API

Rename component via API

rbackman07
Enthusiast Enthusiast
1,405 Views
2 Replies
Message 1 of 3

Rename component via API

rbackman07
Enthusiast
Enthusiast

Hi there, I am trying to rename components via the API. this is what I tried but it doesn't seem to work. making a new file with two components  and running the below code gives me this output. any help would be appreciated:

 

found component Component1:1
tried to rename to Comp1
but it is Component1:1
found component Component2:1
tried to rename to Comp2
but it is Component2:1

 

components = adsk.fusion.Design.cast(app.activeProduct).rootComponent.allOccurrences

indx = 1

for c in components:

     cname = c.name

     newName = "Comp{}".format(indx)

     c.name = newName

     indx+=1

     print("found component {} \ntried to rename to {} \nbut it is {}".format(cname,newName,c.name))

 

0 Likes
Accepted solutions (1)
1,406 Views
2 Replies
Replies (2)
Message 2 of 3

BrianEkins
Mentor
Mentor
Accepted solution

The problem is that you're getting occurrences and trying to set the name of each occurrence.  The name property of the Occurrence object is read-only because the occurrence inherits its name from the component it references.

 

There are a couple of alternatives in your case.  You can use the Design.allComponents property to get components instead of occurrences or you can use the component property of the Occurrence object to get the associated component and then change its name.  Just remember that if you have multiple occurrences of the same component, the last one will win since you're just continually changing the name of the same component.

 

To better understand the difference between occurrences and components take a look at the help document this discusses assembly structure.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 3

rbackman07
Enthusiast
Enthusiast

Thanks for the quick feedback Brian and thanks for the learning resource!

0 Likes