Help to have spheres that will be created and be moved to match points that are created in a voronoi pattern/randomised .

itrebilcoX5LR6
Explorer

Help to have spheres that will be created and be moved to match points that are created in a voronoi pattern/randomised .

itrebilcoX5LR6
Explorer
Explorer

Hello everyone, I have two bits of code here that work by themselves but i cant seem to make them work together.

What I am hoping to create is to have the Voronoi pattern/randomised points created and then have spheres be created and moved to those points. Attached is a screen shot of the effect im looking to create. 

Here are the two bits of code - 

This one creates the randomised points -

 

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

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        design = app.activeProduct

        # Create a new sketch on the XY plane.
        rootComp = design.rootComponent
        sketches = rootComp.sketches
        xyPlane = rootComp.xYConstructionPlane
        sketch = sketches.add(xyPlane)

        # Parameters for the pattern
        width = 100.0  # Width of the pattern area
        height = 100.0  # Height of the pattern area
        grid_spacing = 10.0  # Spacing between grid points
        perturbation = 3.0  # Maximum random perturbation of points

        # Generate grid points with random perturbations
        points = []
        num_x_points = int(width / grid_spacing) + 1
        num_y_points = int(height / grid_spacing) + 1

        for i in range(num_x_points):
            for j in range(num_y_points):
                x = i * grid_spacing + random.uniform(-perturbation, perturbation)
                y = j * grid_spacing + random.uniform(-perturbation, perturbation)
                points.append(adsk.core.Point3D.create(x, y, 0))

        # Add points to the sketch
        for point in points:
            sketch.sketchPoints.add(point)

        ui.messageBox('Pattern of points created successfully.')

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

 


And this is the code that creates a sphere 

 

import adsk.core, adsk.fusion, math, traceback

def run(context):
    ui = None
    try: 
        app = adsk.core.Application.get()
        ui = app.userInterface

        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        design = app.activeProduct

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

        # Create a new sketch on the xy plane.
        sketches = rootComp.sketches;
        xyPlane = rootComp.xYConstructionPlane;
        sketch = sketches.add(xyPlane)

        # Draw a circle.
        circles = sketch.sketchCurves.sketchCircles
        circle = circles.addByCenterRadius(adsk.core.Point3D.create(0, 0, 0), 2)
        
        # Draw a line to use as the axis of revolution.
        lines = sketch.sketchCurves.sketchLines
        axisLine = lines.addByTwoPoints(adsk.core.Point3D.create(-3, 0, 0), adsk.core.Point3D.create(3, 0, 0))

        # Get the profile defined by half of the circle.
        prof = sketch.profiles.item(0)

        # Create an revolution input to be able to define the input needed for a revolution
        # while specifying the profile and that a new component is to be created
        revolves = rootComp.features.revolveFeatures
        revInput = revolves.createInput(prof, axisLine, adsk.fusion.FeatureOperations.NewComponentFeatureOperation)

        # Define that the extent is an angle of 2*pi to get a sphere
        angle = adsk.core.ValueInput.createByReal(2*math.pi)
        revInput.setAngleExtent(False, angle)

        # Create the extrusion.
        ext = revolves.add(revInput)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 


I cant figure this out so any help would be so amazing.

Thanks so much 🙂 

0 Likes
Reply
Accepted solutions (1)
402 Views
2 Replies
Replies (2)

kandennti
Mentor
Mentor
Accepted solution

Hi @itrebilcoX5LR6 -San.

 

Did you make it with chatGPT?

I know the way you created the sphere is different, but I hope it is what you want.

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion
import random

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

        # Parameters for the pattern
        width = 100.0  # Width of the pattern area
        height = 100.0  # Height of the pattern area
        grid_spacing = 10.0  # Spacing between grid points
        perturbation = 3.0  # Maximum random perturbation of points

        # Generate grid points with random perturbations
        points = []
        num_x_points = int(width / grid_spacing) + 1
        num_y_points = int(height / grid_spacing) + 1

        for i in range(num_x_points):
            for j in range(num_y_points):
                x = i * grid_spacing + random.uniform(-perturbation, perturbation)
                y = j * grid_spacing + random.uniform(-perturbation, perturbation)
                points.append(core.Point3D.create(x, y, 0))

        # Create a temporary sphere
        radius = 2
        tmpMgr: fusion.TemporaryBRepManager = fusion.TemporaryBRepManager.get()
        spheres = [tmpMgr.createSphere(p, radius) for p in points]

        # Create base features for parametric design
        baseFeat: fusion.BaseFeature = None
        if des.designType == fusion.DesignTypes.ParametricDesignType:
            baseFeat = root.features.baseFeatures.add()

        # Create a sphere in the root component
        if baseFeat:
            try:
                baseFeat.startEdit()
                [root.bRepBodies.add(sp, baseFeat) for sp in spheres]
            except:
                pass
            finally:
                baseFeat.finishEdit()
        else:
            [root.bRepBodies.add(sp) for sp in spheres]

        # view fit
        app.activeViewport.fit()

        ui.messageBox("Done", "Voronoi pattern sphere")
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

1.png

0 Likes

itrebilcoX5LR6
Explorer
Explorer

Yes I was using chatGPT, That works perfectly thank you so much for you help!

1 Like