Reparametrize geometry python

Hi, is there a reparametrize method in python, like in grasshopper?
to make the domain of a curve from 0 to 1?

import Rhino.Geometry as rg
x.Domain = rg.Interval(0,1)
a = x

CurveReparam.gh (3.4 KB)

1 Like

Hi @onlyforpeace

Every curve has a domain property where you can set the new parametrization as an interval:
https://developer.rhino3d.com/api/RhinoCommon/html/P_Rhino_Geometry_Curve_Domain.htm

In the case you’d like to reparametrize like in GH, then you’d set the domain to this new interval:

myCurve.Domain = Rhino.Geometry.Interval(0,1)

Here’s an example for a surface as well:

1 Like

ok,

but in python my curve is create by mycurve=rs.AddPolyline(pts) so how can i transform my python curve to a rhinocomon curve?

it’s really difficult for me to mix python and rhinocomon…

i have to use rs.SplitBrep() but it ask me that:
unable to convert [<System.Guid object at 0x0000000000000069 [b7246cbf-09a5-4cf0-bcfc-93d5e4eeac2d]>] into Brep geometry for some extrusions…

Short answer:

mycurve= rs.AddPolyline(pts) #GUID
mycurve= rs.coercecurve(mycurve) #Rhino.Geometry.PolylineCurve

Long answer:

  • RhinoScriptSyntax is a wrapper for RhinoCommon (checkout this nifty tool)
  • rs.AddPolyline() adds an object to the document and returns a GUID
  • In order to identify that GUID as a geometry object you need to convert it to a Rhino.Geometry.PolylineCurve with rs.coercecurve
  • rs.SplitBrep() asks for two GUIDS. You must be passing something else than a Brep hence the error.

Hope this helps. If you have more doubts, please post your code, it´s always easier to help then.

2 Likes

It might also be worth mentioning that the rhinoscriptsyntax Python package is located here:

C:\Users\USERNAME\AppData\Roaming\McNeel\Rhinoceros\6.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript

Where you can browse through the .py files and learn how RhinoCommon has been implemented:

I like to use Sublime Text for this. Where I add the rhinoscript folder (under Project > Add Folder to Project), which allows you to search across all the .py files at once for a function:

Then double click the search results in the editor, and hey presto:

As you can see here, most of the SplitBrep() code is related to guid/Rhino document stuff, while it’s only line 2383 that is actually doing any RhinoCommon geometry business. If you’re in GHPython, I find it simpler/better/faster to go directly with RhinoCommon when computing geometry stuff.

1 Like

Whoua…
Excuse me @AndersDeleuran, but my English is really limited…
I understood that you use sublime text, but how you can use it in rhino? I thunk that rhino use only rhino python editor, in rhino…

I’m not using it in Rhino (though one could). Just as an external code/text editor/viewer. In this case, to search/view the Python code that is in the rhinoscript folder :male_detective:

So when you work on rhino python script, you script it on sublime text, copy past on python editor of rhino, for debugging?

No, I just use the EditPythonScript and GHPython editors directly.

And then Sublime Text when I want to inspect the source code of e.g. rhinoscriptsyntax.

I’ve personally not found much need/had luck with using external editors to write/run code (specifically in the case of Rhino Python).

Edit: When I write/run CPython outside of Rhino, I do actually use Sublime Text as my IDE.

You can also use the handy utility made by @Dancergraham posted above to search for and find the Rhinocommon code for rhinoscriptsyntax functions (I’ll post it again)

Then copy/paste the relevant parts you need into your script… but, you will probably need to change some variable names, and you likely will only need some part of the whole rs function code…

You can also simply call inspect.getsource() while you’re developing:

Edit: A downside to this (and the utility posted above) is that you don’t get the import statements included in the .py module file, which you would by directly inspecting the .py file. Thereby breaking the breadcrumb trail of where functions come from.

2 Likes

Yes I must get around to fixing that…

1 Like

I feel like there’s probably some built-in method for this, but I suppose one could get the source file and scrape that for import statements, and then check these against available modules (though that wouldn’t catch referenced/imported .NET libraries). A clever regular expression would probably do it, but in principle, something like this maybe:


200417_GHPython_GetImportStatements_00.gh (2.8 KB)

Though this wouldn’t necessarily account for errors (e.g. rhinocsriptsyntax as rs).

2 Likes

Looks good! I guess a further improvement could be to filter the last word in l, only retaining import statements where the final word appears in the function source somewhere …?

Insert a new line 10:

if l.split[-1] not in my_function_source_code:
    continue
1 Like

How about this…

from inspect import getsource, getmembers, isfunction, getsourcefile

import rhinoscriptsyntax as rs

""" Script to view the source code for rhinoscript modules in Rhino 5 + 6
By Graham Knapp for personal use and for the McNeel Discourse forums
https://discourse.mcneel.com/t/python-may-the-source-be-with-you/84655
13/6/2019
"""


def get_source():
    search_term = rs.StringBox('Function name to search for',
                               title='rhinoscriptsyntax'
                               ).lower()
    if not search_term:
        return
    functions = {name: obj for name, obj in getmembers(rs) if
                 isfunction(obj) and search_term in name.lower()}  # (tuples of name, fuction)
    if not functions:
        return
    selected = rs.ListBox(functions.keys(), title='rhinoscriptsyntax')
    if not selected:
        return

    the_source = getsource(functions[selected])
    filename = getsourcefile(functions[selected])

    # Get all used import statements
    import_statements = []
    with open(filename) as f:
        for line in f.readlines():
            if 'import' in line:
                imported_name = line.strip().split()[-1]
                if imported_name in the_source:
                    import_statements.append(line.strip())
                    
    # Prepend import statements to the source code
    the_source = '\n'.join(import_statements)+'\n\n'+the_source

    box_result = rs.EditBox(the_source,
                            message='Press OK to copy to clipboard',
                            title='Use the source')
    if box_result:
        rs.ClipboardText(box_result)

    return box_result


if __name__ == '__main__':
    get_source()
2 Likes

This is just what I needed, thank you. Michael

1 Like