Autocomplete while editing python scripts outside of Rhino

I have after some work gotten gtalarico/ironpython-stubs to work in Vim. I have tried but not succeeded in getting the Rhino-Packages to work however.

Here are the steps in case someone else prefers working in Vim. These instructions assumes that you have a Linux environment with an installation of Vim that has been compiled with python support, and that you have python2 installed. Other completion plugins might interefere with jedi-vim.

  1. Install Vim plugin davidhalter/jedi-vim using your prefered method.
  2. Clone or download gtalarico/ironpython-stubs. The repo will be referenced in the next step.
  3. Append the path to the stubs to the PYTHONPATH variable in your shell profile (.bash_profile/.bashrc/.zshrc) using the command export PYTHONPATH=${PYTHONPATH}:/path/to/iron-python-stubs/release/stubs.min
  4. Put let g:jedi-vim#force_py_version = '2' in your .vimrc if you have python3 in your environment.
  5. You are now ready to complete using jedi-vim, the standard keymap is <C>-<Space>. I use ervandew/supertab to map completions to <Tab>.

As I said, I’ve tried to use this method to complete with Rhino-Stubs. I tried installing the package using pip2 and adding the path to the relevant site-packages directory but for some reason jedi-vim doesn’t complete Rhino methods.

Edit: I wrote this up with some more context on my blog and I have opened an issue to have it added to the ironpython-stubs wiki.

1 Like

Well done! Hmm maybe I should do the same for Pycharm :thinking:

Why not use @stevebaer’s python package if you are using PyCharm?

Yes good point. I have recently changed computer and I haven’t yet set up Pycharm so I’ll now give that a go. I might write up the stubs process for others as well … after my holidays :sunglasses:

1 Like

FYI; I just updated all of the stubs packages on pypi.org to be based off of this week’s V7 WIP. A lot of functionality has added to our SDKs since the last time I did this. Please let me know if you experience any issues.

3 Likes

New stubs are available based on help from @eirannejad. The stubs now work cleaner in VSCode with all overloads for methods and constructors getting properly displayed. The experience in PyCharm is pretty much the same as with the previous stubs libraries.

pip install --user --upgrade Rhino-stubs

2 Likes

Hi Steve,

I succesfully installed the Rhino-stubs in PyCharm CE.

However it’s not working as I expected for autocompleting.
If I import Rhino like I’m used to in the rhino-pythoneditor I do not get an autocomplete for, for example Rhino.Geometry:

image
image

Might there also be an easy solution to this that I’m missing?

In addition:
These Rhino-stubs are from the WIP right? And there ar no available for V6

Thanks!
-Willem

It doesn’t look like PyCharm is recognizing your installation of Rhino stubs. You might need to look at you PyCharm project settings to see if the package is installed.

The stubs are just there to assist with autocomplete so it doesn’t matter much if you are focused on V6 or V7 work.

@Willem can you try in Pycharm:

import Rhino.Geometry as rg

image

does that work?

Hi David,

That is indeed working and was my point, but I see that was not clear:

image

image

I’d rather not have to import all Rhino namespaces individually

EDIT:
I though this was related to the Rhino Namespace but any ‘sub’ will not autocomplete:
image

-Willem

Hi @stevebaer and @DavidLeon

After some digging and testing I found that this is expected behavior and the auto-completion like in the Rhino Python Editor is not.

However I did find a solution in defining an __all__ in the __init__.pyi files.
For that I wrote the script below to be run in the Rhino-stubs root.

import os

start_dir = os.path.dirname(__file__)

for root, dirs, files in os.walk(start_dir ):
    
    init_files = [f for f in files if os.path.basename(f) == '__init__.pyi']
    if init_files and dirs:
        
        str_dirs = ["'{}'".format(dir) for dir in dirs]
        
        all_str = '__all__ =['
        all_str += ','.join(str_dirs)
        all_str += ']\n'
        
        #assume single __init__.pyi per directory
        init_file = os.path.join(root,init_files[0])
        with open(init_file, 'r') as file:
            init_data = file.read()
        
        #ignore existing '_all__'
        if init_data.startswith('__all__ =['):
            lines = init_data.split('\n')
            init_data = '\n'.join(lines[1:])
            
        new_init_data = all_str + init_data
        
        with open(init_file, 'w') as file:
            file.write(new_init_data)
        

__init__.pyi files now look like this:

__all__ =['ApplicationSettings','Collections','Commands','Display','DocObjects','FileIO','Geometry','Input','NodeInCode','PlugIns','Render','Runtime','UI']
from typing import Tuple, Set, Iterable, List


class RuntimeEnvironment:
    Unset = 0
    #None = 1

And autocompletion in pycharm is working like so:
image
image

I’m not sure how harmful it is to add the __all__ declaration to the stubs but I suppose it’s not.
I’m going to run the script above over my own module structure to have this type of autocompletion for those as well.
-Willem

2 Likes

Interesting; I could definitely add this to the pyi files if it helps. Play with it for a bit and let me know if it continues to work as expected. We’ll also need to test this in VS Code to make sure it doesn’t break anything there.
Thanks Willem

1 Like

@eirannejad do you have any experience with using the __all__ syntax in the pyi files? I’m looking to update the stubs with the latest set of RhinoCommon changes and was wondering if I should tackle this at the same time.

I have used __all__ in python modules but not in .pyi files. It should be fine tho. It basically controls what is exported from a module. Let’s add and test in VSCode.

@Willem Out of curiosity, does the auto-complete feel faster after the addition of __all__?

Sounds good, I’m tweaking the stubbler application to include __all__ when generating the stubs. I’m also adding some functionality to skip methods marked as Obsolete in .NET so you get a better autocomplete list.

Ok; I updated the packages on PyPi.org. See if these new ones work for you. If they don’t I can adjust and upload a different version

@stevebaer: I’m trying out visual studio code with this. I am trying to mimic importing scriptcontext as I haven’t found a way to do this with the stubb module alone, so I am making a scriptcontext.py living in the project folder. I’m mostly just after the doc object and it’s tables, so the file content is simple:

import Rhino

doc = Rhino.RhinoDoc()

Then in the script I can do the regular import:

import scriptcontext as sc 

Which will also work when hosted in Rhino.

However, VSCode doesn’t seem to pick up the tables of the doc. In the __init__.py file, the Rhinodoc class has these as properties (the object table for example):

@property
def Objects(self) -> ObjectTable: ...

But (at least my) VSCode doesn’t know what an ObjectTable is so the return type is lost. I found that if I import Rhino at the top of __init__.py, and then put the return type as:

@property
def Objects(self) -> Rhino.DocObjects.Tables.ObjectTable: ...

Then VSCode is auto-completing the object table in my script.

Do you think this could be included in the stubb generator for all Rhinodoc tables (or maybe all Rhino return types, I don’t think VSCode knows what any of them are)?

Thanks.

Nathan

Hi, there is a question that are there any ways to use pycharm for editing RhinoPython and keep pycharm and rhino interlocked?

hi @stevebaer @eirannejad, It´s been a while since I use the stubs… are they supposed to work with the latest versions of python? I can´t manage to get any autocomplete in VS Code with ver 3.9. If not, which version do you recommend I use? Thanks!

1 Like

Hey David; I also haven’t touched this library for a long time. They should work with 3.9