Quantcast
Channel: Scripting - McNeel Forum
Viewing all 5809 articles
Browse latest View live

Call a recursive function in python as long as a condition is met?

$
0
0

@flokart wrote:

Hi all,

i tried to create a recursive function with python that choose a random point from a nearest points group.
It works but now i want analyze the result and depending on some conditions i want to recall the function.

How can a function can be called again till the conditions is met?

import scriptcontext
import rhinoscriptsyntax as rs
import ghpythonlib.components as gc
import random
import Rhino

visiblept = [startpt]
cullindexin.remove(startpt)



def path(closestpt,cullist):

    # 1
    # choose a random point from nearest points and do this process recursiv
    # store these random points in a list called visiblept
    
    i = gc.ListItem(closestpt,random.choice(closestpt),True)
    visiblept.append(i)
    L = cullist
    L.remove(i)
    P = gc.ClosestPoints(i,L,5)[0]
    
    
    if len(visiblept) <= iterator:
        return path(P,L)

path(closestptin,cullindexin)

# 2
# create a polyline from visiblept and check for self intersection

Curveguid = rs.AddPolyline(visiblept)
Curve = rs.coercecurve(Curveguid)
events = Rhino.Geometry.Intersect.Intersection.CurveSelf(Curve,0.001)


# 3
# if self intersection is True do 1 again till selfintersection is False
# if len(events) > 0:
    # HOW CAN I CALL THE PATH FUNCTION AGAIN

Any help is welcome.

Posts: 3

Participants: 2

Read full topic


Find center point of cylinder with RS

Minimum oriented bounding box. Implementation in Grasshopper + Python script node

$
0
0

@ilmar_ik wrote:

Hello for everyone,
I am trying to understand the logic of minimum bounding box definition so I can implement it in python script node. The reason is very simple - I am planning to test my gh definitons on Shapediver, which does support python script + Grasshopper. I am trying to develop an automated upper limb 3D scan re-alignment tool and as far as I am aware there is no possibility to (re) execute Galapagos in Shapediver.

I have a well-working definition in Grasshopper + Galapagos (attached) and I do have a sense that it is checking all possible options (optimisation algorithm) and seeks out for the best combination which generates minimum volume.

I did try to convert VBS to Python syntax of the code kindly provided by Mitch Heynick (I have a feeling that [Helvetosaur] is the same person here) but I am getting errors there as well (also attached here). I don’t have strong programming skills, all I do is I use this/grasshopper forums and try to “read” and “rewrite = understand” others scripts.

As I understand this is an iterative process (for loop) to rotate initial WorldXY plane, which defines the orientation of 8 points which defines boundaries of a object. So I have to rotate WorldXY plane around X [1,0,0] AND Y[0,1,0] AND Z[0,0,1] axes 360 times each and for every combination create generate “boundaries” for every combinations and check if its volume is still smaller than initial volume? So this is 360360360 combinations? So should I write all values (volumes) generated in for loop into a list and find a minimum value of that list? I am loosing track here at some point. Don’t really understand how to move from this point.

I don’t need a very precise and accurate method to find a minimum oriented bounding box. I would be very thankful if anyone could advise me how to implement this or give some hints about the most effective way to do it. It can be any example, even not related with this topic. Maybe there is a literature which practically explores similar optimization issues? I appreciate for your time and help QuickminimumBB VBS to Python Script by Mitch.gh (5.3 KB)
min_oriented_bb_ilja.gh (39.8 KB)

Sincerely,
Ilja

Posts: 1

Participants: 1

Read full topic

Autolabel for solid nurbs

$
0
0

@pie.ventura wrote:

hi to all boys,
I need a hand.
I’m working on this autolabel because I need to give an objname to all objects following an order like this script (by vector).
This autolabel, however, assigns a textdot that is placed in the centroid of the solid that I need but which greatly strains the computer. (a lot of time)
I do not know how to solve the problem with a quicker calculation but to do the same thing.
the same script for light obj (2d, 3d) is very fast but I need it for a piece of solid nurbs.
maybe a bounding box?
it’s enough for me to just give an objectname without placing a text dot
Tnx guysprova count.3dm (1.8 MB)


autolabeltutti.py (2.7 KB)

Blockquote

Posts: 9

Participants: 4

Read full topic

Can RhinoScriptEditor be replaced with 3rd party IDE?

RhinoCommon - plane aligned boundingbox?

$
0
0

@Helvetosaur wrote:

HI all,

Having a brain fart here - I have some RhinoCommon geometry (curves, surfaces that are not in the document), and I need to get a plane-aligned bounding box for each with a plane that is not one of the principal XYZ planes. I need to do this a lot of times.

I have tried with

xform = Rhino.Geometry.Transform.ChangeBasis(Rhino.Geometry.Plane.WorldXY, ref_plane)
#and then
bb=obj.GetBoundingBox(xform) #one single object here

then getting the corner points and transforming them in the other direction, but this still seems to get me a world-aligned bounding box…

bb=obj.GetBoundingBox(ref_plane)

Didn’t work either…

Thx, --Mitch

Posts: 2

Participants: 2

Read full topic

Python Script_Divide Curve and Add lines

$
0
0

@obhagwat29 wrote:

Hello,
I have started learning python with rhino.
Here is simple problem which i am unable to solve
##############################################

import rhinoscriptsyntax as rs

c1 = rs.AddCurve(rs.GetPoints(“create Points for curve”), 3)
c2 = rs.OffsetCurve(c1,rs.GetPoint(“Direction of offset”),rs.GetReal(“offset”))

division = rs.GetInteger(“No.of divsion”)
div1 = rs.DivideCurve(c1,division,True,True)

div2 = rs.DivideCurve(c2,division,True,True)

lines = [div1,div2]

lineID = rs.AddLine(lines[0],lines[1])

################################################

Any help is appreciated, I dont understand where I am going wrong.

Posts: 3

Participants: 2

Read full topic

Offset curves consistently

$
0
0

@Dongyeop_Lee wrote:

Hello all,

I’m trying to make a C# component that will offset planar curves consistently regardless of curve directions.
The process would be:

  1. Close all curves, if not closed.
  2. Get center points of curves.
  3. Get normal vectors of surfaces from curves
  4. Offset curves

I asked a similar question before here, but the curve offset method seems changed in Rhino 6.


I’m embarrassed that I didn’t make much progress on coding since then, but can you help me write this component in C#?

//Create surfaces to get normal vectors from surfaces using ClosestPoint method.
Brep[] crvBreps = Brep.CreatePlanarBreps(crvs, 0.1);

//Get centroid of curves 
AreaMassProperties AMP = AreaMassProperties.Compute(crvs);
Point3d cen = AMP.Centroid;

//Get Vector3d normal using Brep.ClosestPoint method 
Vector3d[] normalCrvs;
Point3d[] cPoints;
int ci;
double s;
double t;
crvBreps.ClosestPoint(cen, out cPoints, out ci, out s, out t, 100.00, out normalCrvs);

I skipped the step 1 for now and am trying to get normal vectors from surfaces using Brep.CloestPoint method. Can someone let me know I can make this work?
Once I get normal vectors, I’m thinking to use Curve.Offset Method (Point3d, Vector3d, Double, Double, CurveOffsetCornerStyle).
http://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Curve_Offset_1.htm
Thank you.

Dongyeop

Posts: 1

Participants: 1

Read full topic


Fit Polynomial through a set of 2D Points with Rhino/Python

$
0
0

@Benjamin4 wrote:

Hi everyone,
is there a possibility to calculate a Polynominal through a set of 2D Points with Python or Rhino Script?
I am used to Python programming and numpy would easily do the job unfortunately it is not available for Rhino 6 and Rhino 5 64 Bit.
Thanks a lot!
Cheers,
Benjamin

Posts: 3

Participants: 2

Read full topic

Selchain behaviour change rhino 5 to 6

$
0
0

@Alberto wrote:

Hi,
I am trying to understand how the SelChain behaviour has changed in rhino 6. The following script has two different outcome in rhino 5 and 6:

import rhinoscriptsyntax as rs

rs.Command('_SelChain _SelID e7423f0d-1196-41bf-b371-af76f1472dce  _AngleTolerance=1 _GapTolerance=0.001 _ChainContinuity=Position _Enter ', True)
print rs.SelectedObjects()

where the ID need to be changed to the one of an object in a chain in your model. The expected outcome is that all the objects that are adjacent to the one passed to the command should be selected. In V5 this is the case. In V6 the list returned by rs.SelectedObjects() is empty.

Am I missing something?

Thanks a lot
Alberto

Posts: 1

Participants: 1

Read full topic

Python script "Converts text objects into single stroke curves"

$
0
0

@onlyforpeace wrote:

hi, i ve a python script made by mitch to convert text to single stroke curve.
i add a line to keep the name of the text object to the curve, but there is now a error message:
error

what is this error?

Posts: 2

Participants: 2

Read full topic

C# - close open curves

$
0
0

@Dongyeop_Lee wrote:

Hello,

I’m trying to close open curves using C# like CloseCrv command in Rhino. To begin with, I’m not sure what the right method is in Rhino Common. It looks like Curve.MakeClosed method is the right one, but it only outputs boolean values, not closed curves. Am I looking at the wrong method? Without knowing it right, I started writing a code like this.
image
Please excuse my level of coding, if it has too many simple mistakes.
I searched other topics related with “close curves”, but I don’t understand how I actually get closed curves instead of just testing in other posts I found.

Dongyeopclose curves.gh (11.5 KB)

Posts: 3

Participants: 3

Read full topic

Minimum bounding box python script

For loop error GHPython in Rhino 5.13

$
0
0

@avishek.das wrote:

Hello all,

I am new to Python scripting in rhino and for now following a simple tutorial from ThinkParametric. I have understood the basic of it. but the problem I am facing to use a for loop withing Ghpython component. I have tried writing a simple loop in other GHpython component and it runs totally ok. but somehow here it gives an error:
Runtime error (ArgumentTypeException): list is not callable
Traceback:
_ line 33, in script_

here in line 33 the for loop starts. I am attaching the files here. if you could please have a look and suggest some solution.

Thank you
20180610_Curvature Optimization_01.gh (3.9 KB)
20180610_Curvature Optimization_01.3dm (157.4 KB)

Posts: 2

Participants: 2

Read full topic

Sorting Curves into columns

$
0
0

@dustin_headley wrote:

Hey there. Attempting to do a 2D sort of parallel curves with uneven seaming between the arrays (see below). First column is simple, having trouble once I get the next column. Each sequence will vary (will always be parallel). Needing help with the logic. As far as I can tell I need to redact the curves already counted and tagged (so take a slice of the list) and resort the remaining recursively (since there could be more columns). Always want to count upwards from the bottom of the column of curves. Thoughts? any help is appreciated.

Posts: 1

Participants: 1

Read full topic


Embedding python functions into toolbar

$
0
0

@danielogg92 wrote:

Hi, I am written a function that will take a list of points and pipe diameter and create a pipe with a certain fillet radius. This is all contained within my .py file that I run from Rhino Python Editor. I am no looking at making this function more useable and create a tile on the toolbar that runs this function. How do I put my custom functions onto the toolbar.

Posts: 2

Participants: 2

Read full topic

Save all -> reset script-engine -> run

$
0
0

@Willem wrote:

Hi,

I’m working with different modules imported in a single main python script.
In order to run the scripts after editing them, I need to save all the scripts, reset the script-engine en only after that I can run the new scripts.

I’d like to have a single click solution: ‘save all -> reset script-engine -> run’

Does anyone have an idea how to solve this. Else I’d like to opt for such a button in the script editor.

Thanks
-Willem

Posts: 1

Participants: 1

Read full topic

How to initialize an array of surface objects in RhinoPython?

$
0
0

@Rhino_Gandalf wrote:

Hi everyone,

I would like to dynamically add surfaces to an object array through Rhinopython.

I tried doing this but it did not work out:

import rhinoscriptsyntax as rs
center=(0,0,0)
radius=1
j=0
while j<4:

   surf = rs.AddSphere(center,radius+j)
   if j==0:
            objs={surf}  
   else:
            objs={objs,surf} 

I am getting an error, “set objects are unhashable”.
Does anyone have any suggestions of any other way to go about it?

Posts: 3

Participants: 2

Read full topic

Osnap numbers have changed in SR5

$
0
0

@DanBayn wrote:

It seems that with the latest SR5, the OsnapMode behaviour has changed.

Was this intentional?

I have users reporting that where they used to get Point + End, they’re now getting Knot.

I can fix all these scripts, but I want to make sure it’s staying this way (or else I will need to make copies to use if it changes again).

Thanks,

Dan

Posts: 2

Participants: 2

Read full topic

How to ensure consistency of normal direction while using Network curves in RhinoPython?Scr

$
0
0

@Rhino_Gandalf wrote:

Hi everyone,

I am trying to figure use of NetworkSurface in Rhino using RhinoPython. Before moving to non-straight edge geometries, I am working on creating just a single rectangular surface. Please give your feedback on the issue.

While creating the surface, I am facing a trouble that the normal direction of the surface created does not remain consistent. I am also implementig a flag to flip the normal using the FlipNormal function if necessary but I would really like the normal direction to be consistent for a given NetworkSurface function call for a given order of curves/lines created.

See the code below. This if you run once will have normal in one direction. But delete that surface and say run again then it might end up having a flipped normal.

import rhinoscriptsyntax as rs

def srfCreate(P1=None,P2=None,P3=None,P4=None):
Line1 = rs.AddLine(P1,P2)
Line2 = rs.AddLine(P2,P3)
Line3 = rs.AddLine(P3,P4)
Line4 = rs.AddLine(P4,P1)
curves = {Line1,Line2,Line3,Line4}
Rectangle_obj = rs.AddNetworkSrf(curves,edge_tolerance=1e-5, interior_tolerance=1e-4,angle_tolerance=0)

if name == ‘main’:
P1=(0,0,0)
P2=(0,1,0)
P3=(1,1,0)
P4=(1,0,0)
normal_flag=‘FALSE’
srf1=srfCreate(P1,P2,P3,P4)
if(normal_flag):
rs.FlipSurface(srf1, normal_flag)

Posts: 7

Participants: 2

Read full topic

Viewing all 5809 articles
Browse latest View live