Grasshopper

algorithmic modeling for Rhino

Hello,

i want to learn python so i thought begin with remaoing numbers cause when iam using it in gh i need mostly the remap,bounds and create domain so i tought it would be possible doing it in one python component. i found some code and it gets me no error message but neither an solution.

Maybe someone is willed to help me?

the code i put is:

def remap( x, oMin, oMax, nMin, nMax ):

    #range check
    if oMin == oMax:
        print "Warning: Zero input range"
        return None

    if nMin == nMax:
        print "Warning: Zero output range"
        return None

    #check reversed input range
    reverseInput = False
    oldMin = min( oMin, oMax )
    oldMax = max( oMin, oMax )
    if not oldMin == oMin:
        reverseInput = True

    #check reversed output range
    reverseOutput = False   
    newMin = min( nMin, nMax )
    newMax = max( nMin, nMax )
    if not newMin == nMin :
        reverseOutput = True

    portion = (x-oldMin)*(newMax-newMin)/(oldMax-oldMin)
    if reverseInput:
        portion = (oldMax-x)*(newMax-newMin)/(oldMax-oldMin)

    result = portion + newMin
    if reverseOutput:
        result = newMax - portion

    
    
    return result
    print result
    
    a = result

thanks in advance!

Views: 2609

Attachments:

Replies to This Discussion

I think if you return then all statements after that will not be called. Hence, I'm sceptical about this:

  return result
  print result
    
  a = result

I'd say a = result should be sometime before return result.

I do not know Python but it seems you're not calling the function, only you have defined it.

BTW, the formula can be done in one line:

NewValue = (OldValue-SourceFrom)/(SourceTo-SourceFrom)*(TargetTo-TargetFrom)+TargetFrom

oh thanks daniel, it works now!

and thanks david

i have another question,

if i want to do it like the gh component, to access the min and max values, i input the domain, how can i acces these values? is it like a list?would it be the first and second item? how would be the procedure?

thanks in advance!

T0 and T1 properties of Interval object.

http://4.rhino3d.com/5/rhinocommon/?topic=html/AllMembers_T_Rhino_G...

If "domain" is the input nickname then domain.T0 or domain.T1.

thanks Daniel,

when i put the the domain in it comes in as a data tree, to what have i to change the type hint?

ah have to change to item acces right?

Yes. Note that the types of access are not chosen by the data you will use (except to optimize or other cases), but usually to relate data from different parameters. In this case it should let all access as item type, so that each value is associated with the corresponding source and target domain.

However, this component usually uses a list as input values, what it might be good to put the first input as list access. The difference is that in this way, the component is only executed once (but there will be a loop internally), otherwise the component will be executed once for each item in the list (and internally no loop). I do not know if there is much difference in performance from each other, but I do not think it much.

You have to set the type of object that collects all parameter.

Set Type Hint to Interval. 

thanks, Daniel!

Here's the function I usually use to generate color gradients, it demonstrates a similar method for remapping values. Might help as well :)

thanks for sharing Anders!

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service