Karamba in Python3: "ModelCreation" Example

I’m trying to use Karamba from Rhino’s Python3 editor (so not through GH). I was trying to use the ModelCreation example from the Karamba Scripting Guide, https://scripting-1-3.karamba3d.com/v/english_1_3_3/2.-scripting/2.3-how-to-create-structural-models, and convert it to Python3. Should I still use the KarambaCommon.Toolkit? The below does not work. Thank you for your help!
ModelCreation_Python3.py (1.3 KB)

#! python3

import Karamba
import KarambaCommon
from System.Collections.Generic import List, Dictionary, IReadOnlyList

def RunScript():
logger = Karamba.Utilities.MessageLogger()
k3d = KarambaCommon.Toolkit()

p0 = Karamba.Geometry.Point3(0, 0, 0)
p1 = Karamba.Geometry.Point3(0, 0, 5)
L0 = Karamba.Geometry.Line3(p0, p1)

nodes = List[Karamba.Geometry.Point3]()

elems = k3d.Part.LineToBeam(List[Karamba.Geometry.Line3]([L0]), List[str](["B1"]), List[Karamba.CrossSections.CroSec](), logger, nodes)

cond = List[bool]([True, True, True, True, True, True])
support = k3d.Support.Support(0, cond)
supports = List[Karamba.Supports.Support]([support])

pload = k3d.Load.PointLoad(1, Karamba.Geometry.Vector3(0, 0, -10), Karamba.Geometry.Vector3())
ploads = List[Karamba.Loads.Load]([pload])

mass = None
cog = Karamba.Geometry.Point3()
flag = None
info = ""
model = k3d.Model.AssembleModel(elems, supports, ploads, info, mass, cog, info, flag)

# calculate Th.I response
max_disp = List[float]()
out_g = List[float]()
out_comp = List[float]()
message = ""
model = k3d.Algorithms.AnalyzeThI(model, max_disp, out_g, out_comp, message)

ucf = Karamba.Utilities.UnitsConversionFactories.Conv()
cm = ucf.cm()
print("max disp: " + cm.toUnit(max_disp[0]) + cm.unitB)

RunScript()

Hello @Ano,
the KarambaCommon.Toolkit methods are recommended for setting up the model.
What error message do you get?
Did you try the divide and conquer strategy? You could try to set up and output the model, then caluclate it with GH compopnents.
– Clemens

Hello Clemens

I’m getting an error in the LineToBeam, before I can build the model:

Traceback (most recent call last):
File “file:///D:/Projects/0312-StructuralGrowth02/3d/Scripts/ModelCreation_Python3.py”, line 43, in
File “file:///D:/Projects/0312-StructuralGrowth02/3d/Scripts/ModelCreation_Python3.py”, line 17, in RunScript
TypeError: System.Collections.Generic.List1[Karamba.Geometry.Point3] value cannot be converted to System.Boolean in method System.Collections.Generic.List1[Karamba.Elements.BuilderBeam] LineToBeam(System.Collections.Generic.List1[Karamba.Geometry.Line3], System.Collections.Generic.List1[System.String], System.Collections.Generic.List1[Karamba.CrossSections.CroSec], Karamba.Utilities.MessageLogger, System.Collections.Generic.List1[Karamba.Geometry.Point3] ByRef, Boolean, Double, System.Collections.Generic.List1[Karamba.Geometry.Vector3], System.Collections.Generic.List1[System.Drawing.Color], System.Collections.Generic.List`1[Karamba.Geometry.Point3], Boolean, Boolean)

It is asking for a boolean, however the first 5 inputs of FactoryPart.LineToBeam() seem to not include a boolean value. Only the 6th input is a boolean (bending), which has a default value and I’m therefore not giving it.

Did you maybe forget to call the method via an instance of the Toolkit-class? Like
var myToolkit = new KarambaCommon.Toolkit();
var myBeams = myToolkit.Part.LineToBeam(…)
– Clemens

I believe that’s what I did in the above script:

k3d = KarambaCommon.Toolkit()
elems = k3d.Part.LineToBeam(List[Karamba.Geometry.Line3]([L0]), List[str](["B1"]), List[Karamba.CrossSections.CroSec](), logger, nodes)

It looks like python found a previous Karamba.dll file on my computer, even though this previous version was not installed. I have now deleted all Karamba dll files except for those in the Rhino8 Plugins folder. I can only run the script from the Rhino script editor after starting GH. It does not cause an error on importing Karamba and KarambaCommon, however there is nothing inside Karamba, not even Karamba.Geometry, and KarambaCommon doesn’t have a path:

#! python3
import Karamba
import KarambaCommon
print(Karamba.__path__,dir(Karamba))
print(KarambaCommon.__path__,dir(KarambaCommon))
print(hasattr(Karamba,"Geometry"))
_NamespacePath(['C:\\Program Files\\Rhino 8\\Plug-ins\\Karamba']) ['__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
[] ['Toolkit', 'Utilities', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__file__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__loader__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__package__', '__path__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__spec__', '__str__', '__subclasshook__']
False

Is it loading the correct files now?

Hi @Ano,
the path looks correct. It might be that Python has a problem with the gha-extension of “karamba.gha”. You could try to copy the file and rename it to karamba.dll.
– Clemens

It now finds Karamba.Geometry, but it still give the error on LineToBeam:
ModelCreation_Python3.py (1.6 KB)

#! python3

r “C:\Program Files\Rhino 8\Plug-ins\Karamba\karamba.dll”

r “C:\Program Files\Rhino 8\Plug-ins\Karamba\karambaCommon.dll”

import Karamba
import KarambaCommon
from System.Collections.Generic import List, Dictionary, IReadOnlyList

print(Karamba.path,dir(Karamba))
print(KarambaCommon.path,dir(KarambaCommon))
print(hasattr(Karamba,“Geometry”))

def RunScript():
logger = Karamba.Utilities.MessageLogger()
k3d = KarambaCommon.Toolkit()

p0 = Karamba.Geometry.Point3(0, 0, 0)
p1 = Karamba.Geometry.Point3(0, 0, 5)
L0 = Karamba.Geometry.Line3(p0, p1)

nodes = List[Karamba.Geometry.Point3]()

elems = k3d.Part.LineToBeam(List[Karamba.Geometry.Line3]([L0]), List[str](["B1"]), List[Karamba.CrossSections.CroSec](), logger, nodes)

cond = List[bool]([True, True, True, True, True, True])
support = k3d.Support.Support(0, cond)
supports = List[Karamba.Supports.Support]([support])

pload = k3d.Load.PointLoad(1, Karamba.Geometry.Vector3(0, 0, -10), Karamba.Geometry.Vector3())
ploads = List[Karamba.Loads.Load]([pload])

mass = None
cog = Karamba.Geometry.Point3()
flag = None
info = ""
model = k3d.Model.AssembleModel(elems, supports, ploads, info, mass, cog, info, flag)

# calculate Th.I response
max_disp = List[float]()
out_g = List[float]()
out_comp = List[float]()
message = ""
model = k3d.Algorithms.AnalyzeThI(model, max_disp, out_g, out_comp, message)

ucf = Karamba.Utilities.UnitsConversionFactories.Conv()
cm = ucf.cm()
print("max disp: " + cm.toUnit(max_disp[0]) + cm.unitB)

RunScript()

Traceback (most recent call last):
File “file:///D:/Projects/0312-StructuralGrowth02/3d/Scripts/ModelCreation_Python3.py”, line 50, in
File “file:///D:/Projects/0312-StructuralGrowth02/3d/Scripts/ModelCreation_Python3.py”, line 24, in RunScript
TypeError: System.Collections.Generic.List1[Karamba.Geometry.Point3] value cannot be converted to System.Boolean in method System.Collections.Generic.List1[Karamba.Elements.BuilderBeam] LineToBeam(System.Collections.Generic.List1[Karamba.Geometry.Line3], System.Collections.Generic.List1[System.String], System.Collections.Generic.List1[Karamba.CrossSections.CroSec], Karamba.Utilities.MessageLogger, System.Collections.Generic.List1[Karamba.Geometry.Point3] ByRef, Boolean, Double, System.Collections.Generic.List1[Karamba.Geometry.Vector3], System.Collections.Generic.List1[System.Drawing.Color], System.Collections.Generic.List`1[Karamba.Geometry.Point3], Boolean, Boolean)

[‘Algorithms’, ‘CrossSections’, ‘Elements’, ‘GHopper’, ‘Geometry’, ‘Joints’, ‘Licenses’, ‘Loads’, ‘Materials’, ‘Models’, ‘Supports’, ‘Utilities’, ‘class’, ‘delattr’, ‘dir’, ‘doc’, ‘eq’, ‘file’, ‘format’, ‘ge’, ‘getattribute’, ‘gt’, ‘hash’, ‘init’, ‘init_subclass’, ‘le’, ‘loader’, ‘lt’, ‘module’, ‘name’, ‘ne’, ‘new’, ‘package’, ‘path’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘setattr’, ‘sizeof’, ‘spec’, ‘str’, ‘subclasshook’]
[‘Toolkit’, ‘class’, ‘delattr’, ‘dir’, ‘doc’, ‘eq’, ‘file’, ‘format’, ‘ge’, ‘getattribute’, ‘gt’, ‘hash’, ‘init’, ‘init_subclass’, ‘le’, ‘loader’, ‘lt’, ‘module’, ‘name’, ‘ne’, ‘new’, ‘package’, ‘path’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘setattr’, ‘sizeof’, ‘spec’, ‘str’, ‘subclasshook’]
True

May I ask if you happen to have any basic example script in Python 3 that works? Thank you very much!