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.
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to Rotate a body From Python Script

Message 1 of 4
3280 Views, 3 Replies

How to Rotate a body From Python Script

I'm trying to rotate a body, (ideally to align to vector). I have some test code that just rotates by 78 degrees along the yAxis at the origin. However it does nothing. Its based on teh Move example code. How do I rotate a body?

import adsk.core, adsk.fusion, traceback
def createNewComponent(rootComp):
    allOccs = rootComp.occurrences
    newOcc = allOccs.addNewComponent(adsk.core.Matrix3D.create())
    return newOcc.component
def run(context):
    ui = None
        app = adsk.core.Application.get()
        ui  = app.userInterface

        # Create a document.
        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)

        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)

        # Get the root component of the active design.
        rootComp = createNewComponent(design.rootComponent)
        ui.messageBox("FirstOrigin: "+ str(rootComp.originConstructionPoint.geometry.asArray()))
        features = rootComp.features

        # Create sketch circle on the xz plane.
        sketches = rootComp.sketches
        sketch = sketches.add(rootComp.xZConstructionPlane)
        sketchCircles = sketch.sketchCurves.sketchCircles
        centerPoint = adsk.core.Point3D.create(0, 0, 0)
        sketchCircles.addByCenterRadius(centerPoint, 10)

        # Create a collection of entities for extrude
        entities0 = adsk.core.ObjectCollection.create()

        # Create a cylinder with ExtrudeFeature using the profile above.
        extrudeFeats = features.extrudeFeatures
        extrudeFeatureInput = extrudeFeats.createInput(entities0, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        extrudeFeatureInput.isSolid = True
        extrudeFeatureInput.setDistanceExtent(False, adsk.core.ValueInput.createByReal(2.0))
        extrudeFeature = extrudeFeats.add(extrudeFeatureInput)

        # Create a collection of entities for move
        entities1 = adsk.core.ObjectCollection.create()

        # Create a transform to do move
        transform = adsk.core.Matrix3D.create()

        # Create a move feature
        moveFeats = features.moveFeatures
        moveFeatureInput = moveFeats.createInput(entities1, transform)
        ui.messageBox("FirstOrigin: "+ str(rootComp.originConstructionPoint.geometry.asArray()))
        if ui:
Message 2 of 4
in reply to: nnamfoh

I don't see any big problems with your code except that anytime angles are specified or returned by the API that are in radians rather than degrees.  The other thing is that the position that you're displaying at the end won't change even if the body is moved.  That's because you're getting the position of the occurrence and it hasn't changed.  Instead you've moved the body in the component that the occurrence is referencing.  I don't know what you're trying to accomplish but I'm guessing that it would be better to move the occurrence instead of the body.  The Occurrence object has a transform property that you can set with a Matrix3D to change the occurrence position and orientation.



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

Ignore the last statment. Its a debug statement left over froim an old test. I'm not trying to do anything to the occurance. I'm trying to rotate that specific body by any angle (so the radians, degrees problem doesn't matter). If you run this script the cylinder generated does not move at all. It stays still.


Is there a sample script that shows moving a body by any angle. I know this is possible in the system because I rotate objects all the time using the interface displayed below, how do you recreate this?Capture.PNG

Message 4 of 4
in reply to: nnamfoh

I think your program probably was working it's just that you were rotating the cylinder around it's axis, so you didn't see any change.  In your setToRotation call, change "newComp.yConstructionAxis.geometry.getData()[2]" to "newComp.zConstructionAxis.geometry.getData()[2]" and you should see a difference.



Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog

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