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: 

Issue with transform(body)

2 REPLIES 2
Reply
Message 1 of 3
elise_joulain
120 Views, 2 Replies

Issue with transform(body)

I have this script :

import adsk.core, adsk.fusion, adsk.cam, traceback,threading

def run(context😞
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        ui.messageBox('Hello script')

        #Create of the transformation matrix
        transformation_matrix = adsk.core.Matrix3D.create()
        transformation_matrix.setCell(0, 0, 1.0)  # First row, first column
        transformation_matrix.setCell(1, 3, 10.0)  # Second row, second column
        transformation_matrix.setCell(2, 2, 1.0)  # Third row, third column
        transformation_matrix.setCell(3, 5, 20.0)  # Fourth row, fourth column

        matrix_str = ""
        for i in range(4😞
            row_values = [transformation_matrix.getCell(i, j) for j in range(4)]
            matrix_str += " ".join(map(str, row_values)) + "\n"

        ui.messageBox(f'Transformation Matrix:\n{matrix_str}')

        #Get the body to transform
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        if not design:
            ui.messageBox('No active Fusion design', 'No Design')
            return

        root_comp = design.rootComponent
               
        for body in root_comp.bRepBodies:
            ui.messageBox(f'name : {body.name}')
            if hasattr(body, 'isValid') and not body.isValid:
                ui.messageBox('Invalid BRepBody object', 'Error')
               
            if body.name == "embedding":
                ui.messageBox('Entered transformation loop')
                # Get TemporaryBRepManager
                tempBrepMgr = adsk.fusion.TemporaryBRepManager.get()
               
                # Transform the body using the specified transformation matrix
                isSuccess = tempBrepMgr.transform(body, transformation_matrix)
                if not isSuccess:
                    ui.messageBox('Failed to transform the body', 'Error')
                    return

                ui.messageBox('Body transformed successfully')
                break  # Exit the loop once the "embedding" body is found



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

Failed:

Traceback (most recent call last):

File "C:/Users/Elise/AppData/Roaming/Autodesk/Autodesk Fusion 360/API/Scripts/Transform_matrix/Transform_matrix.py", line 51, in run

isSuccess = tempBrepMgr.transform(body, transformation_matrix)

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "C:\Users\Elise\AppData\Local\Autodesk\webdeploy\production\9852aa66f5af6660db76d8087c75a7da16ce322c\Python\lib\adsk\fusion.py", line 48265, in transform

return _fusion.TemporaryBRepManager_transform(self, body, transform)

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

RuntimeError: 3 : invalid argument body

 

but I have a valid body, with the right name... I don't know how to fix this issue

2 REPLIES 2
Message 2 of 3

try:

....

tempBody = tempBrepMgr.copy(body)

# Transform the body using the specified transformation matrix
isSuccess = tempBrepMgr.transform(tempBody, transformation_matrix)

....

 

MichaelT
Message 3 of 3

Thank you for your reply. It didn't work but I found another way, it works when accessing the body from an occurence. Now I have another issue with the transform function. I have a transform matrix from 3D-slicer: 

[0.51, -0.85, 0.13, -126.35], [-0.85, -0.52, -0.05, -172.36], 0.11, -0.09, -0.99, 313.17],  [0.0, 0.0, 0.0, 1.0]

I am using: occurrence_to_transform.transform = transformation_matrix

But the transform function doesn't accept this type of matrix it only complies with a diagonal identity matrix and only the 4th column can be none 0. 

How can I apply this transformation to my body?

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