Skip to content

Commit

Permalink
Fix material issues.
Browse files Browse the repository at this point in the history
* no more double material entries
* properly do a plaster material for default
* add default text material
  • Loading branch information
jesterKing committed Oct 2, 2024
1 parent 63f5b7f commit 6d32c64
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
14 changes: 5 additions & 9 deletions import_3dm/converters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
28 changes: 25 additions & 3 deletions import_3dm/converters/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion import_3dm/read3dm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 6d32c64

Please sign in to comment.