Skip to content
12 changes: 12 additions & 0 deletions import_3dm/converters/render_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def import_render_mesh(context, ob, name, scale, options):
fidx = 0
faces = []
vertices = []
colors = []
# now add all faces and vertices to the main lists
for m in msh:
if not m:
Expand All @@ -73,10 +74,21 @@ def import_render_mesh(context, ob, name, scale, options):

fidx = fidx + len(m.Vertices)
vertices.extend([(m.Vertices[v].X * scale, m.Vertices[v].Y * scale, m.Vertices[v].Z * scale) for v in range(len(m.Vertices))])

if len(m.VertexColors) > 0:
colors.extend([(m.VertexColors[v][0] / 255., m.VertexColors[v][1] / 255., m.VertexColors[v][2] / 255., m.VertexColors[v][3] / 255.) for v in range(len(m.VertexColors))])

mesh = context.blend_data.meshes.new(name=name)
mesh.from_pydata(vertices, [], faces)

# if it has vertex colors, add them
if len(colors) > 0:
vcol_layer = mesh.vertex_colors.new()
for poly in mesh.polygons:
for loop_index in poly.loop_indices:
loop_vert_index = mesh.loops[loop_index].vertex_index
vcol_layer.data[loop_index].color = colors[loop_vert_index]

# done, now add object to blender


Expand Down
69 changes: 69 additions & 0 deletions import_3dm/install_dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import os, sys, bpy
import ctypes

def modules_path():
# set up addons/modules under the user
# script path. Here we'll install the
# dependencies
modulespath = os.path.normpath(
os.path.join(
bpy.utils.script_path_user(),
"addons",
"modules"
)
)
if not os.path.exists(modulespath):
os.makedirs(modulespath)

# set user modules path at beginning of paths for earlier hit
if sys.path[1] != modulespath:
sys.path.insert(1, modulespath)

return modulespath


def install_dependencies():
import sys
import os
try:
try:
import pip
except:
print("Installing pip... "),
from subprocess import run as sprun
res = sprun([bpy.app.binary_path_python, "-m", "ensurepip"])

if res.returncode == 0:
import pip
else:
raise Exception("Failed to install pip.")

modulespath = modules_path()

if not os.path.exists(modulespath):
os.makedirs(modulespath)

print("Installing rhino3dm to {}... ".format(modulespath)),
import subprocess
res = subprocess.run([bpy.app.binary_path_python, "-m", "pip", "install", "-q", "-t", "{}".format(modulespath), "rhino3dm"])

except:
raise Exception("Failed to install dependencies. Please make sure you have pip installed.")


if __name__ == "__main__":
try:
import rhino3dm as r3d
except:
print("Failed to load rhino3dm.")
from sys import platform
if platform == "win32":
if ctypes.windll.shell32.IsUserAnAdmin():
install_dependencies()
import rhino3dm
else:
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)

else:
print("Platform {} cannot automatically install dependencies.".format(platform))
raise
14 changes: 4 additions & 10 deletions import_3dm/read3dm.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,10 @@ def install_dependencies():
# TODO: add update mechanism
try:
import rhino3dm as r3d
except:
print("Failed to load rhino3dm, trying to install automatically...")
try:
install_dependencies()
# let user restart Blender, reloading of rhino3dm after automated
# install doesn't always work, better to just fail clearly before
# that
raise Exception("Please restart Blender.")
except:
raise
except ModuleNotFoundError as error:
print("Rhino3dm not found. Attempting to install dependencies...")
from .install_dependencies import install_dependencies
install_dependencies()

from . import converters

Expand Down