algorithmic modeling for Rhino
Hi I have written some extension methods for the Rhino.Geometry.Transform struct but am not sure how to access it from the Python component within Grasshopper ( I remember getting it to work once but not sure why it broke down again).
I have a the following code written in C#:
namespace blahblah.Extensions{
public static class TransformMethods{
public static AxisAngle ToAxisAngleRepresentation (this Transform m){
//Implemented code here
}
}
}
I have several methods to extend Transform struct, in order to convert 4x4 matrices into different formats e.g. axis-angle representation. In this case the return AxisAngle is one of my custom structs. My desire is to script completely in Python from Grasshopper. However I cannot seem to access these extension methods from there.
I have done the following:
1) I compile "blahblah.dll" and place it in my Grasshopper components directory.
2) To test it I use a C# component and reference the dll from "manage assemblies". The C# component automatically adds "using blahblah.Extensions". I feed in a Transform struct called m and am able to call m.ToAxisAngleRepresentation() without errors. Everything is good. (except maybe intellisense doesn't pick it up the method)
3) Next I wish to do the same thing in a Python component. (Here I am still confused about referencing external libraries). I first add my bin/release folder (where blahblah.dll is located) to my sys.path.
4) Next I add clr.AddReference("blahblah.dll")
5) I add the following line:
from blahblah.Extensions import *
which I would assume is equivalent to "using blahblah.Extensions" in the C# component
6) I then test with the following code:
m = Transform()
m.ToAxisAngleRepresentation()
7) I get the following error: Runtime error (MissingMemberException): 'Transform' object has no attribute 'ToAxisAngleRepresentation'
A further question I have with Python components. If I copy my dlls into the Grasshopper component folders. Do I still need to perform steps 3 and 4?
Thanks!
Tags:
It looks like you set up your paths correctly for importing your dll to IronPython, so the problem is either:
1. a difference in the actual object you are using in C# vs. Python - perhaps you aren't calling .ToAxisAngleRepresentation() on the same type of object.
2. Classes are read and compiled differently between C# and IronPython.
With regard to copying your dlls into the component folders: Last I checked, the component folders are not in sys.path by default (though I think they should be), so you would still have to do steps 3 and 4.
You may want to post your code in C# and IronPython separately, as a brief test calling .ToAxisAngleRepresentation() in both languages somewhere where others can see it. Paste it into a gist or post it on StackOverflow. Show the same code calling the same method in both.
Hi Benjamin,
Thank you for taking the time to reply. I have found the solution, which I did before but somehow forgot. The key is to use clr.ImportExtensions which is provided by IronPython 2.7
So the above code should be:
import clr
clr.AddReference("blahblah.dll")
import blahblah.Extensions
clr.ImportExtensions(blahblah.Extensions) #Import my namespace
import Rhino.Geometry as rg
Transform t = rg.Transform()
t.ToAxisAngleRepresentation() #Yay it works
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by