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: 

Manipulating a component's origin

2 REPLIES 2
Reply
Message 1 of 3
nnamfoh
616 Views, 2 Replies

Manipulating a component's origin

is there a way to translate and rotate an orgin of a component using the python API.

I have code to reorient the coordinate system of an occurance but it doesn't seem to do anything to the actual component:

 

def translateToNewOrigin(root, newOrigin, newZaxis):
        zAxis = newZaxis
        (x,y,z)= axes(zAxis)
        xAxis = adsk.core.Vector3D.create(x[0],x[1],x[2])
        yAxis = adsk.core.Vector3D.create(y[0],y[1],y[2])
        zAxis = adsk.core.Vector3D.create(z[0],z[1],z[2])
#        reportMessageBox("Axes: \nx:{}\ny:{}\nz:{}",(x,y,z))
        for occurrence in design.rootComponent.allOccurrencesByComponent(root):
    #        reportMessageBox("occurance: {}", (occurrence.component.name))
            # Get the current transform of the occurrence
            transform = occurrence.transform
    #        reportMessageBox("transform: {}", (transform.asArray()))
            coordinateSystem = transform.getAsCoordinateSystem()
    #        reportMessageBox("system: {}, {}, {},{};", coordinateSystem)
            transform.setToAlignCoordinateSystems(coordinateSystem[0],coordinateSystem[1],coordinateSystem[2],coordinateSystem[3],newOrigin,xAxis,yAxis,zAxis)
    #        reportMessageBox("new Transform: {}", (transform.asArray()))
            # Set the tranform data back to the occurrence 
            occurrence.transform = transform
    #        reportMessageBox("result: {}", (self.design.rootComponent.allOccurrencesByComponent(self.RWOH).item(0).transform.asArray()))
2 REPLIES 2
Message 2 of 3
ekinsb
in reply to: nnamfoh

I didn't follow how you intend to define your new coordinate system.  Just the origin and Z axis isn't enough because the part can still spin around the Z axis.  Moving an occurrence is certainly possible.  Below is a variation of your program that just uses a point to define the new origin.

 

def testTranslate():
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        design = app.activeProduct
        rootComp = adsk.fusion.Component.cast(design.rootComponent)
        
        newOrigin = adsk.core.Point3D.create(1,1,1)
        translateToNewOrigin(design, newOrigin)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def translateToNewOrigin(design, newOrigin):
    root = adsk.fusion.Component.cast(design.rootComponent)
    
    # Iterate through the occurrences in the root component.
    for occurrence in root.occurrences:
        # Get the current transform of the occurrence.
        trans = adsk.core.Matrix3D.cast(occurrence.transform)
        
        # Change the translation portion of the matrix to account for the new origin.
        trans.translation = newOrigin.asVector()

        # Set the transform of the occurrence.
        occurrence.transform = trans

    # Snapshot the changes.
    root.parentDesign.snapshots.add()

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

I have read many times that the Fusion GUI does not allow any manipulation of a component's origin. Does your post mean that you can override this limitation by directly manipulating a component's origin programmatically through the Python interface? Does the Python interface, therefore, expose functionalities that are hidden from the GUI?

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