Need Help: python code run from Jupyter and can NOT run from dynamo python

Hi all,
I have python code run in Jupyter and Colab:


i use the same Env in dynamo but dynamo has error and can not compile this code

# Load the Python Standard and DesignScript Libraries
import sys
import clr
import re
import System
reDir = System.IO.DirectoryInfo(re.__file__)
path_py3_lib = reDir.Parent.Parent.FullName
#sys.path.append(path_py3_lib + r'\Lib\site-packages')
sys.path.append(r'C:\Users\rmohareb\Dynamo3-9\Lib\site-packages')

clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import trimesh
import rtree
import scipy
# The inputs to this node will be stored as a list in the IN variables.
mesh1 = 'C:/Users/rmohareb/Desktop/Neuer Ordner (6)/model8.stl'
mesh2 = 'C:/Users/rmohareb/Desktop/Neuer Ordner (6)/modeltans1.stl'
Tmesh1=trimesh.load(mesh1)
Tmesh2=trimesh.load(mesh2)
# Place your code below this line
kwargs = {}
mesh_to_other ,ff = trimesh.registration.mesh_other(Tmesh1, Tmesh2, samples=500, scale=False, icp_first=10, icp_final=50, **kwargs)
# Assign your output to the OUT variable.
OUT = mesh_to_other

here i use trimesh pak. and i use the same ENV to make sure Jupyter and dynamo use the same env
model8.stl (536.3 KB)
modeltans1.stl (541.1 KB)
Home.dyn (3.8 KB)

This could be a couple of things
Dynamo will most likely be using the embedded version
You can check the python executable directory and the (site-packages) library path with this

import sysconfig
OUT = [sysconfig.get_path(pth) for pth in ["data", "platlib"]]

If you are using an embedded version of Python check that import site line is not commented in the python39._pth file located in the same directory as the Python executable
Try adding the 'platlib' path to sys.path

2 Likes

@Mike.Buttery thanks for reply

I use the embedded version and import site is not commented

Can i use anothe python engine (Python.exe which i use it in Jupyter)? HOW??
in this case i use the same env and Python.exe !!!

hi @RMohareb
why not use the default dynamo directory for python packages? This causes dependency problems

I’m not sure about the end goal, but your code seems to work fine

import sys
import clr
import System
clr.AddReference('Python.Included')
import Python.Included as pyInc
path_py3_lib = pyInc.Installer.EmbeddedPythonHome
sys.path.append(path_py3_lib + r'\Lib\site-packages')

clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import trimesh
import rtree
import scipy
import numpy as np

def computePoint(a):
    if np.isnan(a[0]):
        return None
    else:
        return Point.ByCoordinates(a[0], a[1], a[2])

def computeTriangleSurface(a, DS_ptarr):
    lstPt = [DS_ptarr[a[i]] for i in range(3)]
    if all(isinstance(x, Point) for x in lstPt):
        try:
            return Surface.ByPerimeterPoints(lstPt)
        except:
            return None
    else:
        return None

# The inputs to this node will be stored as a list in the IN variables.
mesh1 = IN[0]
mesh2 = IN[1]
Tmesh1=trimesh.load(mesh1)
Tmesh2=trimesh.load(mesh2)
# compute ICP registration this functionnality requires to install the optional package rtree
kwargs = {}
transf_mat, cost = trimesh.registration.mesh_other(Tmesh1, 
                                                    Tmesh2, 
                                                    samples=500, 
                                                    scale=False, 
                                                    icp_first=10, 
                                                    icp_final=50)
#apply the estimated rigid transformation to the mesh
Tmesh1.apply_transform(transf_mat)
# add meshes
dec_mesh1  = Tmesh1 + Tmesh2

arr_vertices = np.array(dec_mesh1.vertices)
arr_triangles = np.array(dec_mesh1.faces)
DS_ptarr = np.apply_along_axis(computePoint, 1, arr_vertices)
DS_surface_arr = np.apply_along_axis(computeTriangleSurface, 1, arr_triangles, DS_ptarr)
#
OUT = DS_surface_arr
3 Likes

hi @c.poupin
thank, that what exact i need. the final goal is to apply ICP registration between mesh and mesh or mesh and point.

1 Like