用pycharm写Rhino的python填坑记录2

Q·1

No module named 'Rhino' 成功安装但是还有红线说没有包的错误信息

Solution:

只要不影响使用都没关系!!

如果跑不起来可以看看路径,设置 PYTHONPATH

将 Rhino 的 System 文件夹路径添加到 PYTHONPATH 环境变量中:

在 Windows 上:

右键点击 “此电脑” -> “属性” -> “高级系统设置” -> “环境变量”

在 “系统变量” 中找到 PYTHONPATH,如果没有则新建一个

添加 Rhino 的 System 文件夹路径,例如 C:\Program Files\Rhino 7\System

同时检查下环境变量,加上好用。

或者 动态添加 RhinoCommon.dll 路径

在脚本中明确添加路径,并加载 RhinoCommon.dll

确保这个pip install pythonnet是安装成功的

Q2,如何检查python的路径,确保脚本运行目录与python解释器环境一致

which python  # 在Unix系统上

where python  # 在Windows系统上

import os

print("Current working directory:", os.getcwd())

import sys # 打印Python解释器路径

print("Python interpreter:", sys.executable)

import os # 打印脚本路径

print("Script path:", os.path.abspath(__file__))

# 打印sys.path

print("sys.path:", sys.path)

Q3:Rhino换成小写就不会报错,但是小写无法执行。System, grasshopper.Kernel都无法引用的问题。

不能用小写,在 PyCharm 中不能直接安装 grasshopper.Kernel 和rhino包,因为不是一个标准的 Python 包,而是属于 Rhino 和 Grasshopper 的 API。为了在 Python 中使用这些 API,需要使用 Rhino 和 Grasshopper 提供的环境,如 RhinoScript 和 Rhino.Inside.如果安装了,卸载掉,安装好rhinoinside再引用加载它。System它是 .NET 框架的一部分

pip install rhinoinside

import rhinoinside

rhinoinside.load()

Q4:pycharm中如何查看当前python 是IronPython 还是CPython

import platform

import sys

def check_python_implementation():

    implementation = platform.python_implementation()

    if implementation == 'IronPython':

        print("Current Python implementation: IronPython")

    elif implementation == 'CPython':

        print("Current Python implementation: CPython")

    else:

        print(f"Current Python implementation: {implementation}")

    print("Python version:", sys.version)

    print("Python executable:", sys.executable)

if __name__ == "__main__":

check_python_implementation()

或者

通过 PyCharm 的 Terminal 检查打开 PyCharm 的 Terminal:

在 PyCharm 界面下方,选择 Terminal 选项卡。

运行 Python 解释器:

在终端中输入 python 或 ipy,并按 Enter 键。

检查版本和实现:

在 Python 解释器的交互式命令行中运行以下命令:

import platform

platform.python_implementation()

Q5:AttributeError: module 'grasshopper' has no attribute 'Instances'

A5:在 Python 中使用 Rhino 和 Grasshopper 的 API,推荐使用 Rhino.Inside 框架,pip安装的包卸载掉,在A4的基础上找到电脑中安装的Grasshopper.dll,路径处理添加

grasshopper_dll_path = r"D:\software\Plug-ins\Grasshopper\Grasshopper.dll"  # 确保路径正确

clr.AddReference(grasshopper_dll_path)

Q6: System.InvalidOperationException: DragDrop 注册失败。 ---> System.Threading.ThreadStateException: 在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式。

A6

thread = Thread(ThreadStart(runner))

thread.SetApartmentState(ApartmentState.STA)  #设置线程为 STA 模式

thread.Start()

thread.Join()

Q7:Grasshopper.dll文件源码怎么看?

A7:Rhino 安装路径和 Grasshopper.dll 文件的位置一般在Rhino 安装目录的 System 文件夹中,没有的话需要去plugin里找。

使用ILSpy查看DLL源码

下载并安装ILSpy或dotPeek。

打开ILSpy或dotPeek。

加载Grasshopper.dll文件(路径通常为C:\Program Files\Rhino 7\Plug-ins\Grasshopper\Grasshopper.dll)

.gha文件也一样,也是.net的包,component源码包,用上面的方式打开。找需。。。

Q8,官方文档地址:调用Grasshopper组件的python开发文档

调用Grasshopper组件的Python开发文档通常涉及使用RhinoCommon和Grasshopper SDK。虽然没有专门针对Python的全面官方文档,但你可以参考以下资源和方法来开发和调用Grasshopper组件:Grasshopper Addons Index | Grasshopper Docs

  1. RhinoCommon API Documentation:
    • RhinoCommon是Rhino的.NET SDK,Grasshopper也基于此。你可以使用Python for .NET(pythonnet)来调用这些API。
    • RhinoCommon API Documentation
  2. Grasshopper SDK Documentation:
  3. Rhino Developer Website:
  4. GhPython:
    • GhPython是用于在Grasshopper中编写Python脚本的组件。它使用了RhinoCommon和Grasshopper SDK。
    • GhPython Documentation

Q9:搞个跑通的成功案例?

import sys
import clr
import System.Reflection
import rhinoinside
rhinoinside.load()
import System
import os
import Rhino
clr.AddReference("RhinoInside")
clr.AddReference("RhinoCommon")
# 指定 Grasshopper 程序集路径 # 加载 gha 文件与dll 同操作
grasshopper_dll_path = r"D:\software\Plug-ins\Grasshopper\Grasshopper.dll"  # 确保路径正确

if not os.path.exists(grasshopper_dll_path):
    raise FileNotFoundError(f"无法找到 Grasshopper 程序集: {grasshopper_dll_path}")

try:
    # 添加 Grasshopper 程序集引用
    clr.AddReference(grasshopper_dll_path)
    import Grasshopper
    import Grasshopper.Kernel
    import Grasshopper.Kernel.Types
    import Grasshopper.Kernel.Special as ghs
    from Grasshopper.Kernel.Parameters import Param_Number
    print("Grasshopper 程序集已成功加载")
except Exception as e:
    print(f"加载 Grasshopper 程序集时出错: {e}")

# 测试 Grasshopper.Instances 是否可用
try:
    gh_instances = Grasshopper.Instances
    print("Grasshopper.Instances 已成功访问")
except Exception as e:
    print(f"访问 Grasshopper.Instances 时出错: {e}")

# 加载 Grasshopper 文档并运行
def run_grasshopper_file(gh_file_path):
    def runner():
        # 初始化 Rhino
        Rhino.RhinoApp.Initialized += lambda sender, e: print("Rhino initialized")

        # 检查并启动 Grasshopper 插件
        gh = Rhino.RhinoApp.GetPlugInObject("Grasshopper")
        if gh is None:
            print("Grasshopper is not available")
            return

        # 打开 Grasshopper 文件
        if not gh.OpenDocument(gh_file_path):
            print("Failed to open Grasshopper document")
            return

        # 获取当前活跃的 Grasshopper 文档
        gh_doc = Grasshopper.Instances.ActiveCanvas.Document

        if gh_doc is None:
            print("Failed to load Grasshopper document")
            return

        # 运行 Grasshopper 文档中的所有组件
        gh_doc.NewSolution(True)
        #print(gh_doc.Objects) #System.Collections.Generic.List`1[Grasshopper.Kernel.IGH_DocumentObject]
        print(f"You are opening the file: {gh_doc}") ##test 文件名字
        # 获取结果
        pass

    from System.Threading import Thread, ThreadStart, ApartmentState
    thread = Thread(ThreadStart(runner))
    thread.SetApartmentState(ApartmentState.STA)
    thread.Start()
    thread.Join()

if __name__ == "__main__":
    gh_file_path = r"D:\urFileFolder\test.gh"  # 替换为你的 gh 文件路径
    results = run_grasshopper_file(gh_file_path)
    print("End of Grasshopper importing test")

  • 17
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值