From 6d32c64cd9c5d10c987d24fdfecd10acd1954a0e Mon Sep 17 00:00:00 2001 From: Nathan Letwory Date: Wed, 2 Oct 2024 16:56:46 +0300 Subject: [PATCH] Fix material issues. * no more double material entries * properly do a plaster material for default * add default text material --- import_3dm/converters/__init__.py | 14 +++++--------- import_3dm/converters/material.py | 28 +++++++++++++++++++++++++--- import_3dm/read3dm.py | 3 ++- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/import_3dm/converters/__init__.py b/import_3dm/converters/__init__.py index 9022c7a..2cfd455 100644 --- a/import_3dm/converters/__init__.py +++ b/import_3dm/converters/__init__.py @@ -104,11 +104,13 @@ def convert_object( blender_object = utils.get_or_create_iddata(context.blend_data.objects, tags, data) if text_curve: text_tags = utils.create_tag_dict(uuid.uuid1(), f"TXT{ob.Attributes.Name}") + text_curve[0].materials.append(rhinomat) text_object = utils.get_or_create_iddata(context.blend_data.objects, text_tags, text_curve[0]) + text_object.material_slots[0].link = 'OBJECT' + text_object.material_slots[0].material = rhinomat text_object.parent = blender_object - texobpt = text_curve[1] - #text_object.location = (texobpt.X, texobpt.Y, texobpt.Z) - text_object.matrix_world = texobpt + texmatrix = text_curve[1] + text_object.matrix_world = texmatrix else: blender_object = context.blend_data.objects.new(name+"_Instance", None) utils.tag_data(blender_object, tags) @@ -134,12 +136,6 @@ def convert_object( blender_object[pair[0]] = pair[1] if not ob.Attributes.IsInstanceDefinitionObject and ob.Geometry.ObjectType != r3d.ObjectType.InstanceReference and update_materials: - if bpy.app.version>= (4, 1): - override_context = context.copy() - override_context["object"] = blender_object - with context.temp_override(**override_context): - bpy.ops.object.material_slot_add() - blender_object.material_slots[0].link = 'OBJECT' blender_object.material_slots[0].material = rhinomat diff --git a/import_3dm/converters/material.py b/import_3dm/converters/material.py index 538b2ae..40c8fb4 100644 --- a/import_3dm/converters/material.py +++ b/import_3dm/converters/material.py @@ -31,11 +31,15 @@ from pathlib import Path, PureWindowsPath, PurePosixPath import base64 import tempfile +import uuid from typing import Any, Tuple ### default Rhino material name DEFAULT_RHINO_MATERIAL = "Rhino Default Material" +DEFAULT_TEXT_MATERIAL = "Rhino Default Text" +DEFAULT_RHINO_MATERIAL_ID = uuid.UUID("00000000-ABCD-EF01-2345-000000000000") +DEFAULT_RHINO_TEXT_MATERIAL_ID = uuid.UUID("00000000-ABCD-EF01-6789-000000000000") #### material hashing functions @@ -269,6 +273,14 @@ def plaster_material(rhino_material : r3d.RenderMaterial, blender_material : bpy col = get_color_field(rhino_material, "color") plaster.base_color = col +def default_material(blender_material : bpy.types.Material): + plaster = PlasterWrapper(blender_material) + plaster.base_color = (0.9, 0.9, 0.9, 1.0) + +def default_text_material(blender_material : bpy.types.Material): + plaster = PlasterWrapper(blender_material) + plaster.base_color = (0.05, 0.05, 0.05, 1.0) + def metal_material(rhino_material : r3d.RenderMaterial, blender_material : bpy.types.Material): metal = PrincipledBSDFWrapper(blender_material, is_readonly=False) col = get_color_field(rhino_material, "color")[0:3] @@ -490,9 +502,19 @@ def handle_materials(context, model : r3d.File3dm, materials, update): """ """ handle_embedded_files(model) - rdk = rdk_manager.RdkManager(model) - rms = rdk.get_materials() - #for m in rms: + + if DEFAULT_RHINO_MATERIAL not in materials: + tags = utils.create_tag_dict(DEFAULT_RHINO_MATERIAL_ID, DEFAULT_RHINO_MATERIAL) + blmat = utils.get_or_create_iddata(context.blend_data.materials, tags, None) + default_material(blmat) + materials[DEFAULT_RHINO_MATERIAL] = blmat + + if DEFAULT_TEXT_MATERIAL not in materials: + tags = utils.create_tag_dict(DEFAULT_RHINO_TEXT_MATERIAL_ID, DEFAULT_TEXT_MATERIAL) + blmat = utils.get_or_create_iddata(context.blend_data.materials, tags, None) + default_text_material(blmat) + materials[DEFAULT_TEXT_MATERIAL] = blmat + for mat in model.Materials: if not mat.PhysicallyBased: mat.ToPhysicallyBased() diff --git a/import_3dm/read3dm.py b/import_3dm/read3dm.py index 46b305d..177a2e3 100644 --- a/import_3dm/read3dm.py +++ b/import_3dm/read3dm.py @@ -115,7 +115,6 @@ def read_3dm( # Handle layers converters.handle_layers(context, model, toplayer, layerids, materials, update_materials, import_hidden_layers) - materials[converters.DEFAULT_RHINO_MATERIAL] = None #build skeletal hierarchy of instance definitions as collections (will be populated by object importer) if import_instances: @@ -174,6 +173,8 @@ def read_3dm( if matname not in materials.keys(): matname = converters.material.DEFAULT_RHINO_MATERIAL blender_material = materials[matname] + if og.ObjectType == r3d.ObjectType.Annotation: + blender_material = materials[converters.material.DEFAULT_TEXT_MATERIAL] # Fetch layer layer = layerids[str(rhinolayer.Id)][1]