Grasshopper

algorithmic modeling for Rhino

I have a list of referenced points and a list with true and false. I would like to be able to show/hide or lock/unlock the baked points in rhino according that list using python script.

I am really new to scriping and it is really difficult for me to work with custom gh component.

Does anybody care to help.

P.S. I found that piece of code that is supposed to help me but I can't get it to work and it is for curves anyway

def lockcurves_fail():
rs.LockObjects(rs.ObjectsByType(4))

def lockcurves_nofail():
curves = rs.ObjectsByType(4)
if not curves: return False
rs.LockObjects(curves)
return True

Views: 2488

Replies to This Discussion

you can cycle through two lists in the same order this way:

 


def lockCurves( curves, trueFalseValues ):
"""This function assumes that the curves and True/False values
are in the same order.
"""
# make sure we have a reasonable input
if not curves or ( len( curves ) != len( trueFalseValues ) ):
# if the input is bad, print a message and return False
print "either that list of curves was empty or"
print "the lists of curves and boolean values were"
print "different lengths."
return False
# loop through the curves
# this 'for' loop runs through a range of integers
# one integer for each curve
for i in range( len( curves ) ):
curve = curves[i] # get the curve
# get the success value for this curve
success = trueFalseValues[i]
if success: # if the value is True
# then lock the curve
rs.lockObjects( curve )
curves = rs.ObjectsByType(4) # get your curves
lockCurves( curves, myBooleans )


Here is an alternative way of looping through the curves and booleans

 


for i, curve in enumerate( curves ):
# get the success value for this curve
success = trueFalseValues[i]
if success: # if the value is True
# then lock the curve
rs.lockObjects( curve )

I hope that's helpful. I didn't look up the RhinoScript functions, I just tried to give an example that showed how you would write the logic in Python. You might need to adjust this or check it for errors.

Good luck!

P.S. Follow this link to read the code easier: https://gist.github.com/4222169

P.P.S: I edited the linked example because I realized that you want points, not curves. Look at the linked example: https://gist.github.com/4222169

I'm sorry to bother you again but i still have problem understanding how am I feeding the point list from grasshopper.

It is possible that I haven't made myself clear enough i my previous post. Basically I would like to hide/show/lock/unlock points that exist in rhino and are referenced in grasshopper. First I was thinking that I will do the operations with the rhino geometry according their grasshopper references. Now I realize that this is not really needed. I indeed can select the points from a layer.(that wasn't my initial intention)

It seems to me that this script is supposed to get all the points in the rhino document or all the points within a layer (or tell me why it does not try to).

I found out from your repository at git how to setup the script oterwise it gives out some nasty errors

import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
sc.doc = Rhino.RhinoDoc.ActiveDoc

I'm looking for some good basic tutorial because I'm missing the basics of scripting in rhino. I cannot even manipulate the number of inputs that I would like to have (removing one of the input of the component). 

any help is greatly appreciated 

Attachments:

here's some good resources for beginning scripting, by Studio Mode: http://python.rhino3d.com/showthread.php?t=1226

This issue of understand how the different documents work is one of the most confusing things for beginners trying to use python in the grasshopper python component. Your life will be easier for now (as a beginner) if you restrict yourself to either working on things that exist only in Grasshopper or only in the Rhino Document, rather than both at once.

If you right click on the python component you can designate among three options. I don't have rhino nearby or I would tell you which option to pick, but here's what you need to know to figure it out for yourself:

right click and experiment with the options.

to change variables on the component you should be able to just zoom in and add or delete inputs/outputs

the different doc options affect whether scriptcontext.doc refers to the Rhino Document or the Grasshopper document. Furthermore, they affect what kind of data your inputs give to your script.

to understand what kind of data you are working with, you can always get the data type of a variable, using

"print type( myvariable )".

If you have a list of things you can say

"print type( myvariable[0] )"

try using this on anything you get mildly curious about.

based on the options you select by right clicking on the component, you will get either:

1. object IDs for grasshopper objects

2. object IDs for items in the Rhino Document

3. geometry objects that you will find inside of Rhino.Geometry (look here)

Everything in python is some data type. you can find out information on the capabilities of that type by looking at the RhinoCommon docs (that I just linked to above) or the Grasshopper SDK documents. Mostly you'll just need RhinoCommon. Every function for each type will list the kinds of data it needs, and the kinds of data it gives you. If you encounter a type you don't know, just look it up, and you can see what abilities and properties it has. Read the RhinoCommon documentation and play around a lot, and everything will begin to come clear.

be patient, you'll run into errors constantly, but keep exploring the documentation.

Thank you for your great posts. I finally figured most of it out.

The final code looks like this.

import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
sc.doc = Rhino.RhinoDoc.ActiveDoc

# make sure to read the available functions here: http://4.rhino3d.com/5/ironpython/index.html

def lockStuff( things, trueFalseValues ):
"""This function assumes that the things and True/False values
are in the same order.
"""
# make sure we have a reasonable input
if not things or ( len( things ) != len( trueFalseValues ) ):
# if the input is bad, print a message and return False
print "either that list of things was empty or"
print "the lists of things and boolean values were"
print "different lengths."
return False
# loop through the things
# this 'for' loop runs through a range of integers
# one integer for each item
for i in range( len( things ) ):
item = things[i] # get the curve
# get the success value for this curve
old = rs.IsObjectHidden(item)
success = trueFalseValues[i]
if old != success:
if success:
rs.HideObjects(item)
else:
rs.ShowObjects(item)

x = rs.ObjectsByLayer(Layer)
lockStuff( x, Booleans )

I added a small detail because operating with more than 800 points it actually takes rhino a little bit to hide/unhide. I added a check whether there is a difference between the new boolean list and the current state of the points so the rest of the operations can work only on the changed points.

Thank you very much for enduring me.

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service