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: 

Trouble Scaling after copying and moving component.

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
alfredordz86
222 Views, 2 Replies

Trouble Scaling after copying and moving component.

Hi guys, total beginner writing scripts here.

 

I'm trying to copy, move and rotate a body and then scale the new body in its new position. I didn't have much trobule moving it a few inches in the x axis and rotating it 90 degrees (see screenshot) but I've been stuck for hours trying to scale it. I get RuntimeError: 2 : InternalValidationError : Utils::getObjectPath(obj.get(), objPath, nullptr, path)  (line 80). I'm totally lost here, I'd really appreciate some input.

 

Thanks!

 

 

 

import adsk.core, adsk.fusion, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        if not design:
            ui.messageBox('No active Fusion design', 'No Design')
            return

        # Get the root component of the active design.
        rootComp = design.rootComponent
        features = rootComp.features

        # Get the first sub component
        occs = rootComp.occurrences
        subComp1 = occs.item(0).component

         # Create sketch
        sketches = rootComp.sketches
        sketch = sketches.add(rootComp.xYConstructionPlane)
        sketchCircles = sketch.sketchCurves.sketchCircles
        centerPoint = adsk.core.Point3D.create(0, 0, 0)
        circle = sketchCircles.addByCenterRadius(centerPoint, 1.5)

        # Get the first body in sub component 1  
        baseBody = subComp1.bRepBodies.item(0)
        
        # Copy/paste bodies
        subComp1.features.copyPasteBodies.add(baseBody)
        
        # Rename Body
        baseBodyCopy = subComp1.bRepBodies.item(1)

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

        # Create a transform to do move
        vector = adsk.core.Vector3D.create(5, 0, 0)
        transform = adsk.core.Matrix3D.create()
        transform.translation = vector
        
        # Create a move feature
        # moveFeats = features.moveFeatures
        moveFeats = subComp1.features.moveFeatures
        moveFeatureInput = moveFeats.createInput(bodies, transform)
        moveFeats.add(moveFeatureInput)

        # Create a transform to do rotation
        transform = adsk.core.Matrix3D.create()
        rotPoint= adsk.core.Point3D.create()
        rotPoint = adsk.core.Point3D.create(5, 0, 0)
        transform.setToRotation(3.1416,(rootComp.zConstructionAxis.geometry.getData()[2]),rotPoint)

        # Create a move feature
        moveFeats = subComp1.features.moveFeatures
        moveFeatureInput = moveFeats.createInput(bodies, transform)
        moveFeats.add(moveFeatureInput)

        inputColl = adsk.core.ObjectCollection.create()
        inputColl.add(baseBody)
        
        basePt = sketch.sketchPoints.item(0)
        scaleFactor = adsk.core.ValueInput.createByReal(1)
        
        scales = rootComp.features.scaleFeatures
        scaleInput = scales.createInput(bodies, basePt, scaleFactor)
        
        #Set the scale to be non-uniform
        xScale = adsk.core.ValueInput.createByReal(1.5)
        yScale = adsk.core.ValueInput.createByReal(3)
        zScale = adsk.core.ValueInput.createByReal(2)
        scaleInput.setToNonUniform(xScale, yScale, zScale)
        
        scale = scales.add(scaleInput)
    

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

 

Labels (4)
2 REPLIES 2
Message 2 of 3

Hi @alfredordz86 ,

 

The ScaleFeatureInput need to be created with the parent component of the body.

So, in you code replace line 71 with this:

scales = baseBodyCopy.parentComponent.features.scaleFeatures

 

This solve your problem.

Hope this can help.

 

Regards,

Jorge

Message 3 of 3
alfredordz86
in reply to: alfredordz86

Yes!!! I had been stuck for hours with this. I'm still starting to understand how everything works in Python.

 

Muchas gracias mi estimado Jorge.

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