Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def read3dm(self):
rc = rhino3dm.File3dm._TestRead(self._path)
self.assertTrue(rc, self._path)
if top_collection_name in context.blend_data.collections.keys():
toplayer = context.blend_data.collections[top_collection_name]
else:
toplayer = context.blend_data.collections.new(name=top_collection_name)
model = r3d.File3dm.Read(filepath)
layerids = {}
materials = {}
handle_materials(context, model, materials)
handle_layers(context, model, toplayer, layerids, materials)
for ob in model.Objects:
og=ob.Geometry
if og.ObjectType not in [r3d.ObjectType.Brep, r3d.ObjectType.Mesh, r3d.ObjectType.Extrusion]: continue
attr = ob.Attributes
if not attr.Visible: continue
if attr.Name == "" or attr.Name==None:
n = str(og.ObjectType).split(".")[1]+" " + str(attr.Id)
else:
n = attr.Name
if attr.LayerIndex != -1:
rhinolayer = model.Layers[attr.LayerIndex]
else:
rhinolayer = model.Layers[0]
matname = None
if attr.MaterialIndex != -1:
matname = material_name(model.Materials[attr.MaterialIndex])
def read_3dm(context, filepath, import_hidden):
top_collection_name = os.path.splitext(os.path.basename(filepath))[0]
if top_collection_name in context.blend_data.collections.keys():
toplayer = context.blend_data.collections[top_collection_name]
else:
toplayer = context.blend_data.collections.new(name=top_collection_name)
model = r3d.File3dm.Read(filepath)
layerids = {}
materials = {}
handle_materials(context, model, materials)
handle_layers(context, model, toplayer, layerids, materials)
for ob in model.Objects:
og=ob.Geometry
if og.ObjectType not in [r3d.ObjectType.Brep, r3d.ObjectType.Mesh, r3d.ObjectType.Extrusion]: continue
attr = ob.Attributes
if not attr.Visible: continue
if attr.Name == "" or attr.Name==None:
n = str(og.ObjectType).split(".")[1]+" " + str(attr.Id)
else:
n = attr.Name
rhinolayer = model.Layers.FindIndex(attr.LayerIndex)
if not rhinolayer.Visible and not import_hidden_layers:
continue
# Create object name
if attr.Name == "" or attr.Name is None:
n = str(og.ObjectType).split(".")[1]+" " + str(attr.Id)
else:
n = attr.Name
# Get render material
mat_index = ob.Attributes.MaterialIndex
if ob.Attributes.MaterialSource == r3d.ObjectMaterialSource.MaterialFromLayer:
mat_index = rhinolayer.RenderMaterialIndex
rhino_material = model.Materials.FindIndex(mat_index)
# Handle default material and fetch associated Blender material
if rhino_material.Name == "":
matname = converters.material.DEFAULT_RHINO_MATERIAL
else:
matname = converters.material_name(rhino_material)
# Handle object view color
if ob.Attributes.ColorSource == r3d.ObjectColorSource.ColorFromLayer:
view_color = rhinolayer.Color
else:
view_color = ob.Attributes.ObjectColor
else:
matname = converters.material_name(rhino_material)
# Handle object view color
if ob.Attributes.ColorSource == r3d.ObjectColorSource.ColorFromLayer:
view_color = rhinolayer.Color
else:
view_color = ob.Attributes.ObjectColor
rhinomat = materials[matname]
# Fetch layer
layer = layerids[str(rhinolayer.Id)][1]
if og.ObjectType==r3d.ObjectType.InstanceReference and import_instances:
n = model.InstanceDefinitions.FindId(og.ParentIdefId).Name
# Convert object
converters.convert_object(context, ob, n, layer, rhinomat, view_color, scale, options)
#convert_rhino_object(og, context, n, attr.Name, attr.Id, layer, rhinomat, scale)
if import_groups:
converters.handle_groups(context,attr,toplayer,import_nested_groups)
if import_instances:
converters.populate_instance_definitions(context, model, toplayer, "Instance Definitions")
# finally link in the container collection (top layer) into the main
# scene collection.
try:
import rhino3dm as r3d
from .material import handle_materials, material_name
from .layers import handle_layers
from .render_mesh import import_render_mesh
from .curve import import_curve
from .views import handle_views
from .groups import handle_groups
from .instances import import_instance_reference, handle_instance_definitions, populate_instance_definitions
'''
Dictionary mapping between the Rhino file types and importer functions
'''
RHINO_TYPE_TO_IMPORT = {
r3d.ObjectType.Brep : import_render_mesh,
r3d.ObjectType.Extrusion : import_render_mesh,
r3d.ObjectType.Mesh : import_render_mesh,
r3d.ObjectType.Curve : import_curve,
#r3d.ObjectType.InstanceReference : import_instance_reference
}
# TODO: Decouple object data creation from object creation
# and consolidate object-level conversion.
def convert_object(context, ob, name, layer, rhinomat, view_color, scale, options):
"""
Add a new object with given data, link to
collection given by layer
"""
matname = None
if attr.MaterialIndex != -1:
matname = material_name(model.Materials[attr.MaterialIndex])
layeruuid = rhinolayer.Id
rhinomatname = rhinolayer.Name + "+" + str(layeruuid)
if matname:
rhinomat = materials[matname]
else:
rhinomat = materials[rhinomatname]
layer = layerids[str(layeruuid)][1]
# concatenate all meshes from all (brep) faces,
# adjust vertex indices for faces accordingly
# first get all render meshes
if og.ObjectType==r3d.ObjectType.Extrusion:
msh = [og.GetMesh(r3d.MeshType.Any)]
elif og.ObjectType==r3d.ObjectType.Mesh:
msh = [og]
elif og.ObjectType==r3d.ObjectType.Brep:
msh = [og.Faces[f].GetMesh(r3d.MeshType.Any) for f in range(len(og.Faces)) if type(og.Faces[f])!=list]
else:
continue
fidx=0
faces = []
vertices = []
# now add all faces and vertices to the main lists
for m in msh:
if not m: continue
faces.extend([list(map(lambda x: x + fidx, m.Faces[f])) for f in range(len(m.Faces))])
fidx = fidx + len(m.Vertices)
vertices.extend([(m.Vertices[v].X, m.Vertices[v].Y, m.Vertices[v].Z) for v in range(len(m.Vertices))])
def import_render_mesh(context, ob, name, scale, options):
# concatenate all meshes from all (brep) faces,
# adjust vertex indices for faces accordingly
# first get all render meshes
og = ob.Geometry
oa = ob.Attributes
if og.ObjectType == r3d.ObjectType.Extrusion:
msh = [og.GetMesh(r3d.MeshType.Any)]
elif og.ObjectType == r3d.ObjectType.Mesh:
msh = [og]
elif og.ObjectType == r3d.ObjectType.Brep:
msh = [og.Faces[f].GetMesh(r3d.MeshType.Any) for f in range(len(og.Faces)) if type(og.Faces[f])!=list]
fidx = 0
faces = []
vertices = []
# now add all faces and vertices to the main lists
for m in msh:
if not m:
continue
faces.extend([list(map(lambda x: x + fidx, m.Faces[f])) for f in range(len(m.Faces))])
# Rhino always uses 4 values to describe faces, which can lead to
# invalid faces in Blender. Tris will have a duplicate index for the 4th
converters.handle_materials(context, model, materials, update_materials)
# Handle layers
converters.handle_layers(context, model, toplayer, layerids, materials, update_materials, import_hidden_layers)
materials[converters.material.DEFAULT_RHINO_MATERIAL] = None
#build skeletal hierarchy of instance definitions as collections (will be populated by object importer)
if import_instances:
converters.handle_instance_definitions(context, model, toplayer, "Instance Definitions")
# Handle objects
for ob in model.Objects:
og = ob.Geometry
# Skip unsupported object types early
if og.ObjectType not in converters.RHINO_TYPE_TO_IMPORT and og.ObjectType != r3d.ObjectType.InstanceReference:
print("Unsupported object type... ")
continue
#convert_rhino_object = converters.RHINO_TYPE_TO_IMPORT[og.ObjectType]
# Check object and layer visibility
attr = ob.Attributes
if not attr.Visible and not import_hidden_objects:
continue
rhinolayer = model.Layers.FindIndex(attr.LayerIndex)
if not rhinolayer.Visible and not import_hidden_layers:
continue
# Create object name
def import_render_mesh(context, ob, name, scale, options):
# concatenate all meshes from all (brep) faces,
# adjust vertex indices for faces accordingly
# first get all render meshes
og = ob.Geometry
oa = ob.Attributes
if og.ObjectType == r3d.ObjectType.Extrusion:
msh = [og.GetMesh(r3d.MeshType.Any)]
elif og.ObjectType == r3d.ObjectType.Mesh:
msh = [og]
elif og.ObjectType == r3d.ObjectType.Brep:
msh = [og.Faces[f].GetMesh(r3d.MeshType.Any) for f in range(len(og.Faces)) if type(og.Faces[f])!=list]
fidx = 0
faces = []
vertices = []
# now add all faces and vertices to the main lists
for m in msh:
if not m:
continue
faces.extend([list(map(lambda x: x + fidx, m.Faces[f])) for f in range(len(m.Faces))])
# Rhino always uses 4 values to describe faces, which can lead to
# invalid faces in Blender. Tris will have a duplicate index for the 4th
# value.
for f in faces: