Get world position of MeshBody's MeshNode (Vertex)

Get world position of MeshBody's MeshNode (Vertex)

Anonymous
Not applicable
1,275 Views
4 Replies
Message 1 of 5

Get world position of MeshBody's MeshNode (Vertex)

Anonymous
Not applicable

Hello

 

I have a MeshBody and would like to find the World coordinate of its vertexes (nodes). Right now I am using Meshbody>mesh>nodeCoordinates but they seem to be relative to mesh origin.

 

How can I get the world-space coordinates? 

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

kandennti
Mentor
Mentor

Hi n_autodesk.

 

I have written how to get Matrix3D for conversion here.

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/incorrect-transformation-from-occurrence-t... 

 

If you get Matrix3D from the component (occurrence) to which MeshBody belongs and

use transformBy method of nodeCoordinates (Point3D), you can get world-space coordinates.

 

I want to make it if I have time.

Message 3 of 5

Anonymous
Not applicable

Awesome!

This almost works (or I have misunderstood how to implement it).

It works fine when I move the component, but when I move the body itself it no longer works since the function looks at the component and not at the mesh body. I hope this will be as simple as finding the occurrence for the mesh body and making it the first step. 

0 Likes
Message 4 of 5

kandennti
Mentor
Mentor
Accepted solution

I tested it.

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        des = app.activeProduct
        root :adsk.fusion.Component = des.rootComponent

        # select
        msg = 'select MeshBody'
        mb :adsk.fusion.MeshBody = selectMeshBodies(ui, msg)
        if not mb: return

        mat3d :adsk.core.Matrix3D =getRootMatrix(mb.parentComponent)

        # This is a clone
        # nodes = mb.mesh.nodeCoordinates #NG!
        nodes = mb.displayMesh.nodeCoordinates

        # transform
        [node.transformBy(mat3d) for node in nodes]

        # Create a sketch at the root and create a point
        skt = root.sketches.add(root.xYConstructionPlane)
        skt.isComputeDeferred = True
        [skt.sketchPoints.add(node) for node in nodes]
        skt.isComputeDeferred = False

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

def getRootMatrix(
    comp :adsk.fusion.Component) -> adsk.core.Matrix3D:

    comp = adsk.fusion.Component.cast(comp)
    des = adsk.fusion.Design.cast(comp.parentDesign)
    root = des.rootComponent

    mat = adsk.core.Matrix3D.create()
  
    if comp == root:
        return mat

    occs = root.allOccurrencesByComponent(comp)
    if len(occs) < 1:
        return mat

    occ = occs[0]
    occ_names = occ.fullPathName.split('+')
    occs = [root.allOccurrences.itemByName(name) 
                for name in occ_names]
    mat3ds = [occ.transform for occ in occs]
    mat3ds.reverse() #important!!
    for mat3d in mat3ds:
        mat.transformBy(mat3d)

    return mat

def selectMeshBodies(
    ui :adsk.core.UserInterface,
    msg :str) -> adsk.fusion.MeshBody:

    filtterStr = 'MeshBodies'
    while True:
        try:
            sel :adsk.core.Selection = ui.selectEntity(msg, filtterStr)
            return sel.entity
        except:
            return None

 

This did not get the correct position.

Meshbody.mesh.nodeCoordinates

Movements that are not visible on the timeline appear to be ignored.


If it is here, I think that it can be processed correctly.

Meshbody.displayMesh.nodeCoordinates

please make sure.

Message 5 of 5

Anonymous
Not applicable

Thank you, this works perfectly. I just hope the displayMesh will always be the same as the mesh, but I see no reason why that wouldnt be the case.

0 Likes