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: 

Python script returns the same id for each member of an array

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
zhaodad
437 Views, 6 Replies

Python script returns the same id for each member of an array

I'm working on a plug-in to digest a BRep model using python scripts. I was confused to find out that the address (or id) for each member in an entity collection (e.g., BRepBodies, BRepShells, BRepFaces, BRepCoEdges, BRepEdges etc.) is the same. Details are presented below for a simple script:

 

 

for obj in objs:
    print('obj id = ' + str(id(obj)))

The result from running the above-mentioned script is like:

 

object id = 1556313504904
object id = 1556313504904
...

Is there someone can tell me why the id returns the same address? How to get the unique address for each individual object (e.g., body, shell, face, coedge, edge and vertex etc.)?

 

 

Thanks

 

6 REPLIES 6
Message 2 of 7
ekinsb
in reply to: zhaodad

The Python id function returns an id that represents the Python wrapper.  As noted in this post on Stackoverflow, this value is only unique for objects that exist at the same time and the number can be re-used for objects that don't exist at the same time. The important thing is that is NOT a Fusion object id but a Python object id. In your for loop the variable obj is created to represent a Fusion object and you get the id of the Python object. In the next iteration the previous Python object is destroyed (the Python object, not the Fusion object) and the variable obj is reassigned a new Python object.  Because this doesn't exist at the same time as the previous object it's OK that it can have the same id.

 

for obj in objs:
    print('obj id = ' + str(id(obj)))

 

 

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 7
zhaodad
in reply to: ekinsb

Thanks Brian for your explanation. I actually also had the same guess as the id will only be unique during the one-time iteration for the python wrapper. Now my second question is how to retrieve a unique id for each geometric element, considering that faces may share the same surface, CoEdges may share the same edge etc. I've also noticed that in Fusion 360, some topology objects (such as body, lump, shell, CoEdge) and geometry objects (such as surface, curve3D, curve2D and point) don't have tempId, only face, edge and vertex have this property. Does it mean that objects without tempId property do not share with others?

Message 4 of 7
ekinsb
in reply to: zhaodad

The API does not currently expose any unique ID's for any entities.  The only workaround for this is to add an attribute to the entity you want to track.  You're essentially assigning your own id.

 

Regarding the tempId property, Face, edge, and vertex are the key B-Rep objects and you can derive other entities using a combination of those.  For example if you want a specific co-edge you can get it if you know the face and edge, having a face is enough to get a body, lump, or shell.  And the tempId only applies to B-Rep entities.  All geometry objects are transient so it doesn't make sense for them to have an id.  They only exist for as long as there is a live reference to them.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 5 of 7
zhaodad
in reply to: ekinsb

I thought geometry elements are persistent instead of transient. If that's the case, id is definitely not needed anymore. Thanks for the explanation! 

Message 6 of 7
ekinsb
in reply to: zhaodad

Let me clarify to make sure we're talking about the same thing.  There are persistent entities, which are any of the B-Rep entities and things like sketch lines, arcs, construction planes, joints, etc.  Typically these are things that can be user selected and often are shown in the browser or timeline.  Geometry objects are things like Line3D, Cylinder, Plane, Point3D, etc.  You typically get geometry objects from entity objects and the geometry provides a description the shape of the entity.  When you get the geometry object it represents a snapshot of the current shape of the entity.  You can also explicitly create geometry objects using the static create methods.  


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 7 of 7
zhaodad
in reply to: ekinsb

Yes, I think we are on the same page. Geometry (or shape) is a descriptor according to your description and it's only needed whenever you want that information.

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report