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

Rs.AddPolyline not culling duplicate points as per Help

$
0
0

Mitch Heynick wrote:

I notice that the Python rhinoscriptsyntax does not automatically cull duplicate points as the Help indicates (Help which was copied over from VB essentially), if consecutive duplicates are found, the method fails. If non-consecutive duplicates are found, it does create a polyline (albeit potentially a problematic one)...

I notice that the syntax for rs.AddPolyline does not call cull duplicates (Point3d.CullDuplicates Method) nor does rs.coerce3dpointlist; so as it is coded now, I guess behavior is expected... However, CullDuplicates cullls ALL duplicates, not just consecutive ones, so this is a bit different than culling consecutive ones...

Just a heads-up... Either the Help needs to be changed or the method needs a bit of tweaking...

--Mitch

Fails:

import rhinoscriptsyntax as rs
pointlist=[]
for i in range(10):
    pointlist.append(rs.coerce3dpoint((i,i,i)))
    pointlist.append(rs.coerce3dpoint((i,i,i)))
rs.AddPolyline(pointlist)

Succeeds, but not a good idea...

import rhinoscriptsyntax as rs
pointlist=[]
for i in range(10):
    pointlist.append(rs.coerce3dpoint((i,i,i)))
    pointlist.append(rs.coerce3dpoint((i+1,i+1,i+1)))
    pointlist.append(rs.coerce3dpoint((i,i,i)))
rs.AddPolyline(pointlist)

Better...

import rhinoscriptsyntax as rs
pointlist=[]
for i in range(10):
    pointlist.append(rs.coerce3dpoint((i,i,i)))
    pointlist.append(rs.coerce3dpoint((i+1,i+1,i+1)))
    pointlist.append(rs.coerce3dpoint((i,i,i)))
    pointlist=rs.CullDuplicatePoints(pointlist)
rs.AddPolyline(pointlist)

Posts: 1

Participants: 1

Read full topic


Is it possible to open a file in a zip file?

$
0
0

Jørgen Holo wrote:

I made a zip file called "test.zip" of a small rhino file called "test.3dm" and put it on C:
Then I tried this to see if it would open it:

import rhinoscriptsyntax as rs
rs.Command ("-Open c:\test.zip\test.3dm Enter" )

But I get an "Rhino Plug-in Error: Unable to load plug-in..."

Posts: 3

Participants: 2

Read full topic

Scripting Brazil Materials

$
0
0

James Carruthers wrote:

Does Brazil(or I guess it's the RDK?) have enough RhinoScript support that I could script the following: I have a bunch of 3D clipart in OBJ or other format, which imports with the textures mapped to a basic Rhino material, and I want to "convert" those Basic materials into BAMs.

Posts: 4

Participants: 3

Read full topic

How to get/set named construction planes origin/angles numerically

$
0
0

Katsu wrote:

Hello,

I'd like to know about
how to get/set "named" construction planes origin/angles numerically.

I use python.

Posts: 2

Participants: 2

Read full topic

Bug rs.ZoomExtents() with rs.EnableRedraw(False)

$
0
0

Peter Schmidt wrote:

Greetings,
I have written the following script to set a specific layout as the current view then for each detail in the layout I call the rs.ZoomExtents() command. However I get different results depending on weather or not rs.EnableRedraw() is True or False.
When I set EnableRedraw(False) all of the views on the layout are not set to the proper zoom, but when I commented out rs.EnableRedraw() the script functioned as anticipated. However I am not sure why turning off redraw would have an effect on this.
Any assistance would be greatly appreciated.
Below is my short bit of code as well as images of before and after images with and without redraw enabled.

import scriptcontext
import rhinoscriptsyntax as rs

LayoutNames=scriptcontext.doc.Views.GetPageViews()
Layouts=[]
for name in LayoutNames:
    Layouts.append(name.PageName)
Layout=rs.GetString("Select which layout the block should be place into.",Layouts[0],Layouts)
if rs.IsLayout(Layout)==True:
    rs.CurrentView(Layout)
    Details = scriptcontext.doc.Views.ActiveView.GetDetailViews()
    #rs.EnableRedraw(False)
    for detail in Details:
        guid = rs.coerceguid(detail)
        rs.CurrentDetail("DRAWING",guid)
        rs.DetailLock(guid,False)
        rs.ZoomExtents()

Before the script was run:

Script with rs.EnableRedraw(False)

Script with #rs.EnableRedraw(False) commented out

Also, I should note that this is happening with SR 5.8.40128

Posts: 3

Participants: 2

Read full topic

Small Scripting Jobs (will pay, of course)

$
0
0

Kristen wrote:

Hi All.

Not sure if this is the done thing, but I'd love a couple of scripts written for me.
I guess if you could provide a quote that'd be the starting point.

I want a leader that points to a linked block and displays it's name using the current dimension properties for formatting.
The catches:

Posts: 2

Participants: 2

Read full topic

Python – move a mesh using pythonscript

$
0
0

Hossam Magdy wrote:

I want to move a mesh cube from its volume-centroid to the point 0,0,0 .. but I need it to be done fully automated by the python script without picking any thing from the screen ..

I am a beginner .. so please help me as long as you can .

  • I tried this code , but turns Error :-

import rhinoscriptsyntax as rs

def myFunction(object,translation):
rs.MoveObject(object,translation)

hossam = rs.ObjectName(32)

start = rs.MeshVolumeCentroid(hossam)
End = rs.GetObject(0,0,0)
translation = end-start

myFunction(hossam,translation)

thanks in advance !

Posts: 12

Participants: 3

Read full topic

Python - creating a curve through points

$
0
0

Keith wrote:

I am trying to create a curve which passes through specific point locations.
In Rhino, the command is _curvethroughPt.

What is the Python equivalent? I started out thinking it was rs.AddInterpCurve(points), but this treats the points as control points instead of actually passing through the points.


Posts: 7

Participants: 3

Read full topic


Script is 4X! bigger than it needs to be, please help

$
0
0

Justin Jahn wrote:

Hi, studying RhinoScript 101 by David Rutten, pg 58 'Geodesic Curve'

After studying the script closely I deleted about 100 lines of apparently unnecessary codes, and discovered the script still works perfectly!. How? Why would David write almost 100 lines more code than necessary? I deleted everything after line 32 and the script works just fine.

L31 Call Rhino.Print("Geodesic curve added with length: " & dblNewLength)
L32 End Sub

Could someone please explain. Copy of script below.

Justin

Option Explicit

Call GeodesicCurve()
Sub GeodesicCurve()
Dim strSurfaceID
strSurfaceID = Rhino.GetObject("Select surface for geodesic curve solution", 8, True, True)
If IsNull(strSurfaceID) Then Exit Sub

Dim arrV
arrV = GetR2PathOnSurface(strSurfaceID, 10, "Start of geodesic curve", "End of geodesic curve")
If IsNull(arrV) Then Exit Sub

Dim dblTolerance : dblTolerance = Rhino.UnitAbsoluteTolerance() / 10
Dim dblLength : dblLength = 1e300
Dim dblNewLength : dblNewLength = 0.0

Do
	Call Rhino.Prompt("Solving geodesic fit for " & UBound(arrV) & " samples")
	Call GeodesicFit(arrV, strSurfaceID, dblTolerance)

	dblNewLength = PolylineLength(arrV)
	If (Abs(dblNewLength - dblLength) < dblTolerance) Then Exit Do

	If (UBound(arrV) > 1000) Then Exit Do

	arrV = SubDividePolyline(arrV)
	dblLength = dblNewLength
Loop

Call Rhino.AddPolyline(arrV)
Call Rhino.Print("Geodesic curve added with length: " & dblNewLength)

End Sub

Sub GeodesicFit(ByRef arrVertices, strSurfaceID, dblTolerance)
Dim dblLength
dblLength = PolylineLength(arrVertices)

Dim dblNewLength

Do
	Call SmoothPolyline(arrVertices)
	Call ProjectPolyline(arrVertices, strSurfaceID)

	dblNewLength = PolylineLength(arrVertices)
	If (Abs(dblNewLength - dblLength) < dblTolerance) Then Exit Do

	dblLength = dblNewLength
Loop

End Sub

Sub SmoothPolyline(ByRef arrVertices)
Dim arrCopy : arrCopy = arrVertices
Dim i, j

For i = 1 To UBound(arrVertices) - 1
	For j = 0 To 2
		arrVertices(i)(j) = (arrCopy(i - 1)(j) + _
			arrCopy(i)(j) + _
			arrCopy(i + 1)(j)) / 3.0
	Next
Next

End Sub

Sub ProjectPolyline(ByRef arrVertices, strSurfaceID)
Dim arrProjPt, i

For i = 1 To UBound(arrVertices) - 1
	arrProjPt = Rhino.BRepClosestPoint(strSurfaceID, arrVertices(i))
	If Not IsNull(arrProjPt) Then
		arrVertices(i) = arrProjPt(0)
	End If
Next

End Sub

Function GetR2PathOnSurface(strSurfaceID, intSegments, strPrompt1, strPrompt2)
GetR2PathOnSurface = Null

Dim ptStart, ptEnd
ptStart = Rhino.GetPointOnSurface(strSurfaceID, strPrompt1)
If IsNull(ptStart) Then Exit Function

ptEnd = Rhino.GetPointOnSurface(strSurfaceID, strPrompt2)
If IsNull(ptEnd) Then Exit Function

If (Rhino.Distance(ptStart, ptEnd) = 0.0) Then Exit Function

Dim uvA : uvA = Rhino.SurfaceClosestPoint(strSurfaceID, ptStart)
Dim uvB : uvB = Rhino.SurfaceClosestPoint(strSurfaceID, ptEnd)
Dim arrV() : ReDim arrV(intSegments)

Dim i, t, u, v
For i = 0 To intSegments
	t = i / intSegments
	u = uvA(0) + t * (uvB(0) - uvA(0))
	v = uvA(1) + t * (uvB(1) - uvA(1))
	arrV(i) = Rhino.EvaluateSurface(strSurfaceID, Array(u, v))
Next

GetR2PathOnSurface = arrV

End Function

Function SubDividePolyline(ByRef arrV)
Dim arrSubD()
ReDim arrSubD(2 * UBound(arrV))

Dim i
For i = 0 To UBound(arrV) - 1
	'copy the original vertex location
	arrSubD(i * 2) = arrV(i)
	'compute the average of the current vertex and the next one
	arrSubD(i * 2 + 1) = Array((arrV(i)(0) + arrV(i + 1)(0)) / 2.0, _
		(arrV(i)(1) + arrV(i + 1)(1)) / 2.0, _
		(arrV(i)(2) + arrV(i + 1)(2)) / 2.0)
Next

'copy the last vertex (this is skipped by the loop)
arrSubD(UBound(arrSubD)) = arrV(UBound(arrV))

SubDividePolyline = arrSubD

End Function

Function PolylineLength(ByRef arrVertices)
PolylineLength = 0.0

Dim i
For i = 0 To UBound(arrVertices) - 1
	PolylineLength = PolylineLength + Rhino.Distance(arrVertices(i), arrVertices(i + 1))
Next

End Function

Posts: 5

Participants: 2

Read full topic

Small probelm with Python/Rhinocommon Vector3f

$
0
0

Mitch Heynick wrote:

One is supposed to be able to scale vector objects by simply multiplying them by a number:

import Rhino
vec=Rhino.Geometry.Vector3d(0,0,1)
mult=2.0
scaled=vec*mult
print scaled
>>> (0,0,2)

However, with a vector3f object instead of vector3d object, there is a problem:

import Rhino
vec=Rhino.Geometry.Vector3f(0,0,1)
mult=2
scaled=vec*mult
print scaled
#this works
>>> (0,0,2)

mult=2.0
scaled=vec*mult
#this errors out
>>> Message: unsupported operand type(s) for *: 'Vector3f' and 'float'

So it is accepting integer values, but not floats...

Only reason I found this is that mesh vertex normals seem to be output as vector3f and not vector3d.

--Mitch

Posts: 2

Participants: 2

Read full topic

Rs.XFormRotation2 bug?

$
0
0

vittorio wrote:

Hi Steve
I have the below Python file saved with name matr.py

import rhinoscriptsyntax as rs
def a():
    matrot=rs.XformRotation2(360/(10*40),(0,0,1),(0,0,0))
    print matrot
if __name__=="__main__":
    a()

if i run the script the the matrot is:
R0=(0.999876632481661,-0.0157073173118207,0,0), R1=(0.0157073173118207,0.999876632481661,0,0), R2=(0,0,1,0), R3=(0,0,0,1)

If i create a new below script that called matr , matrot is:
R0=(1,0,0,0), R1=(0,1,0,0), R2=(0,0,1,0), R3=(0,0,0,1)

import rhinoscriptsyntax as rs
import matr
matr.a()

this is a bug or my mistake?

Ciao Vittorio

Posts: 1

Participants: 1

Read full topic

Find discontinuities

$
0
0

Mitch Heynick wrote:

Hi,

I am trying to duplicate the function of Grasshopper's Discontinuity component with Python/Rhinocommon. (I know I can just script the component, but I'm looking at applications where GH might not be present, like Mac)

I've hacked something together that works, but I'm not really sure it's correct. In particular, how to determine when I've found all the discontinuities and break out of the loop. The GetNextDiscontinuity method returns a tuple with a boolean value and a number (parameter), it's not explained exactly what the boolean represents, but I noticed it appeared to switch to False when I had found all discontinuities... At this point, it also returns a parameter of "virtual negative infinity" (-1.23432101234321E+308) - funny number! But I don't know if I'm using the method correctly.

Anyway, code I hacked below... It just creates a square polycurve with two extraneous points which should not be found as discontinuous.

Thx, --Mitch

import Rhino
import scriptcontext as sc

pts=[]
pts.append(Rhino.Geometry.Point3d(0,0,0))
pts.append(Rhino.Geometry.Point3d(5,0,0))
pts.append(Rhino.Geometry.Point3d(10,0,0))
pts.append(Rhino.Geometry.Point3d(10,10,0))
pts.append(Rhino.Geometry.Point3d(5,10,0))
pts.append(Rhino.Geometry.Point3d(0,10,0))
pts.append(Rhino.Geometry.Point3d(0,0,0))

#create a polycurve
pc=Rhino.Geometry.PolyCurve()
for i in range(len(pts)-1):
    pc.Append(Rhino.Geometry.Line(pts[i],pts[i+1]))
sc.doc.Objects.AddCurve(pc)

#find discontinuities
dom=pc.Domain
cornerParams=[]
t=dom[0]
cont=Rhino.Geometry.Continuity.C1_locus_continuous
while True:
    result=pc.GetNextDiscontinuity(cont,t,dom[1])
    if not result[0]: break
    t=result[1]
    cornerParams.append(t)

#add points to see
for t in cornerParams:
    sc.doc.Objects.AddPoint(pc.PointAt(t))
sc.doc.Views.Redraw()

Posts: 4

Participants: 2

Read full topic

Plugin install folder?

$
0
0

Jørgen Holo wrote:

Hi guys,
I am trying to find a plugins install folder.
But all I could find was this:

import Rhino

InstalledPlugInFolders=Rhino.PlugIns.PlugIn.GetInstalledPlugInFolders()
print str(InstalledPlugInFolders)

And that does not tie any of those folders to any plugin. So I must be doing something wrong.
Please help.

Posts: 1

Participants: 1

Read full topic

Join curves in similar direction

$
0
0

Damon wrote:

I've written a Rhinoscript that given two lines, the script will join them if (a) they share an end point and (b) the angle of the tangent vector at their shared end point is less than a specified value. I'd like to expand this to do it for a large number of curves all at once.

Back up a bit to the bigger picture: people in my (architecture) firm like to design a surface/mass then subdivide the surface into a triangulated grid. Everybody uses different ways of doing this. I've been asked a number of times to take the resulting grid of lines and turn it into a set of triangular surfaces and sweep a profile along each curve/line to build an architectural model of glass/metal panels and steel structure. Sometimes the grid is a bunch of continuous degree-3 curves, sometimes degree-3 curves that go from node-to-node, sometimes straight lines that go from node-to-node, and sometimes polylines in each direction.

In any of these cases, I can create the sweeps for the structural members, that's not a problem.

For panels, it's another story. Given polylines or continous degree-3 curves along each diagonal (or in U and V directions), I have no problem creating panels. However, if they've split the curves/lines at the node intersections and haven't sorted them previously by layer for each direction, it's a tedious process. (I'd like to encourage/teach people how to create grids in a more useful way, but partly for practical reasons and partly for self-educational reasons, I think this is an interesting problem to solve.)

Any suggestions for working through a whole list of curves would be greatly appreciated. Here's what I have so far that works for the simple case of testing two curves:

Call JoinCurvesToPath()
Sub JoinCurvesToPath()

	Dim arrCrv : arrCrv = Rhino.GetObjects("Select two curves to join into a polycurve", 4)
	If IsNull(arrCrv) Then Exit Sub

	Call Rhino.EnableRedraw(False)

	Dim crvA : crvA = arrCrv(0)
	Dim crvB : crvB = arrCrv(1)
	Dim arrParams
	Dim angle : angle = 15

	'Find joining ends
	arrParams = JoiningEnds(crvA, crvB)

	If Not (IsNull(arrParams)) Then 

		'Join curves if angle is met.
		Call JoinCurves(crvA, crvB, arrParams, angle)

	End If

	Call Rhino.EnableRedraw(True)

End Sub

Function JoiningEnds(crvA, crvB)

	Call Rhino.Print("In JoiningEnds function")

	Dim startA, endA, startB, endB
	Dim paramA, paramB

	startA = Rhino.CurveStartPoint(crvA)
	endA = Rhino.CurveEndPoint(crvA)
	startB = Rhino.CurveStartPoint(crvB)
	endB = Rhino.CurveEndPoint(crvB)

	If (Rhino.Distance(startA, startB) < 0.1) Then
		Call Rhino.Print("starts")
		paramA = Rhino.CurveClosestPoint(crvA, startA)
		paramB = Rhino.CurveClosestPoint(crvB, startB)
		JoiningEnds = Array(paramA, paramB)

	ElseIf (Rhino.Distance(startA, endB) < 0.1) Then
		Call Rhino.Print("start, end")
		paramA = Rhino.CurveClosestPoint(crvA, startA)
		paramB = Rhino.CurveClosestPoint(crvB, endB)
		JoiningEnds = Array(paramA, paramB)

	ElseIf (Rhino.Distance(endA, endB) < 0.1) Then
		Call Rhino.Print("ends")
		paramA = Rhino.CurveClosestPoint(crvA, endA)
		paramB = Rhino.CurveClosestPoint(crvB, endB)
		JoiningEnds = Array(paramA, paramB)

	ElseIf (Rhino.Distance(endA, startB) < 0.1) Then
		Call Rhino.Print("end, start")
		paramA = Rhino.CurveClosestPoint(crvA, endA)
		paramB = Rhino.CurveClosestPoint(crvB, startB)
		JoiningEnds = Array(paramA, paramB)

	End If

End Function

Function JoinCurves(crvA, crvB, arrParams, minAngle)

	Dim vecA, vecB
	Dim joined

	vecA = Rhino.CurveTangent(crvA, arrParams(0))
	If IsNull(vecA) Then
		Call Rhino.Print("CurveTangent at A failed")
		Exit Function
	End If
	vecB = Rhino.CurveTangent(crvB, arrParams(1))
	If IsNull(vecB) Then
		Call Rhino.Print("CurveTangent at B failed")
		Exit Function
	End If

	Dim angle: angle = Rhino.VectorAngle(vecA, vecB)

	Call Rhino.Print("Angle = " & angle)

	If (angle < minAngle) Then

		joined = Rhino.JoinCurves(Array(crvA, crvB))
		Call Rhino.Print("joined has " & Ubound(joined) + 1 & "objects")

		JoinCurves = joined(0)
		Call Rhino.DeleteObjects(Array(crvA, crvB))

	Else

		JoinCurves = Null

	End If


End Function

Posts: 1

Participants: 1

Read full topic

Why is Python showing the first calculations but not the last?

$
0
0

Jørgen Holo wrote:

Rhino shows the 20 or so first calculations, then suddenly jumps to the end of the calculations.
Why is that?

import rhinoscriptsyntax as rs
import random
import time

time1=time.time()

rs.EnableRedraw (False)
arrPT=[]
for i in range(40):
    for ii in range(40):
        arrPT.append(rs.AddPoint((i,ii,0)))

rs.SelectObjects(arrPT)

rs.EnableRedraw (True)
rs.EnableRedraw (False)

for n in range(40):
    print n
    rs.EnableRedraw (True)
    rs.EnableRedraw (False)
    for ii in range(len(arrPT)):
        rs.MoveObject(arrPT[ii],(0,0,0.5-random.random()))

rs.EnableRedraw (True)

time2=time.time()
print round(time2-time1,2)

Posts: 4

Participants: 2

Read full topic


Small problem with Python/Rhinocommon Vector3f

$
0
0

Mitch Heynick wrote:

One is supposed to be able to scale vector objects by simply multiplying them by a number:

import Rhino
vec=Rhino.Geometry.Vector3d(0,0,1)
mult=2.0
scaled=vec*mult
print scaled
>>> (0,0,2)

However, with a vector3f object instead of vector3d object, there is a problem:

import Rhino
vec=Rhino.Geometry.Vector3f(0,0,1)
mult=2
scaled=vec*mult
print scaled
#this works
>>> (0,0,2)

mult=2.0
scaled=vec*mult
#this errors out
>>> Message: unsupported operand type(s) for *: 'Vector3f' and 'float'

So it is accepting integer values, but not floats...

Only reason I found this is that mesh vertex normals seem to be output as vector3f and not vector3d.

--Mitch

Posts: 7

Participants: 4

Read full topic

Lintypes - where is the pattern information stored?

Avoid multiselection dialog when picking closed seam edge?

$
0
0

Mitch Heynick wrote:

Hi all,

When using RhinoGet to pick a surface edge:

msg="Pick a surface seam edge"
filter=Rhino.DocObjects.ObjectType.EdgeFilter
rc, objref = Rhino.Input.RhinoGet.GetOneObject(msg,False,filter)

Is there any way to further limit the choice to closed seam edges (like the seam on a cylinder or pipe) and avoid the multi-selection box that comes up?

I see ChooseOneQuestion (=False?) but I don't know if that's the way and how I should use it...

Thx,
--Mitch

Posts: 2

Participants: 1

Read full topic

Looking for trim by rectangle tool

$
0
0

Przemek wrote:

I'm wondering whether you've seen script that would trim curves by rectangle/window select. I'm pretty surre I've seen it somewhere , but now I can't find it.

Posts: 2

Participants: 2

Read full topic

Rs.CurveLength Help needs update

$
0
0

Mitch Heynick wrote:

@dale The Python rhinoscriptsyntax help page for rs.CurveLength is missing the descriptions for the two additional arguments segment_index and subdomain... (they can be stolen from the vb Rhinoscript help)

Thanks, --Mitch

Posts: 3

Participants: 2

Read full topic

Viewing all 4202 articles
Browse latest View live