style.add_surface_texture to work without blender material provided

This commit is contained in:
Andrej730
2023-12-12 17:32:49 +05:00
parent a8078e38d2
commit c4080626df
3 changed files with 132 additions and 10 deletions
@@ -21,10 +21,8 @@ import ifcopenshell.api
class Usecase:
def __init__(self, file, material=None, uv_maps=None):
"""Add a surface texture based on a Blender material definition
Warning: this API can only be used with Blender data structures.
def __init__(self, file, material=None, uv_maps=None, textures=None):
"""Add surface texture based on a Blender material definition or texture data.
:param material: The Blender material definition with a node tree that
is compatible with glTF. See one of the valid combinations here:
@@ -34,12 +32,26 @@ class Usecase:
IfcTessellatedFaceSets that the representation has, obtained from
the HasTextures attribute.
:type uv_maps: list[ifcopenshell.entity_instance.entity_instance]
:param textures: A list of dictionaries containing:
1. Attributes to create IfcImageTexture.
2. One additional parameter `uv_mode` to map IfcImageTexture to correct
IfcTextureCoordinate type.
Possible `uv_mode` values:
* `UV` - use IfcTextureCoordinate from `uv_maps` parameter;
* `Generated` - IfcTextureCoordinateGenerator with mode COORD (autogenerated UV
based on geometry);
* `Camera` - IfcTextureCoordinateGenerator with mode COORD_EYE (autogenerated UV
based on camera position)
:type textures: list[dict]
:return: A list of IfcImageTexture
:rtype: list[ifcopenshell.entity_instance.entity_instance]
"""
# TODO: This usecase currently depends on Blender's data model
self.file = file
self.settings = {"material": material, "uv_maps": uv_maps or []}
self.settings = {"material": material, "uv_maps": uv_maps or [], "textures": textures or []}
def execute(self):
if self.file.schema == "IFC2X3":
@@ -51,6 +63,23 @@ class Usecase:
# glTF, X3D, and IFC are compatible. As long as they have something that
# loosely resembles the node tree, we treat it as valid.
self.textures = []
for texture in self.settings["textures"]:
uv_mode = texture.get("uv_mode", None)
texture_data = texture.copy()
texture_data.pop("uv_mode", None)
texture = self.file.create_entity("IfcImageTexture", **texture_data)
if uv_mode == "Generated":
self.file.create_entity("IfcTextureCoordinateGenerator", Maps=[texture], Mode="COORD")
elif uv_mode == "Camera":
self.file.create_entity("IfcTextureCoordinateGenerator", Maps=[texture], Mode="COORD-EYE")
elif uv_mode == "UV":
self.apply_uv_map_to_texture(texture)
self.textures.append(texture)
if self.settings["material"] is None:
return self.textures
output = {n.type: n for n in self.settings["material"].node_tree.nodes}.get("OUTPUT_MATERIAL", None)
if not output:
@@ -73,6 +102,7 @@ class Usecase:
self.detect_occlusion_map()
self.detect_diffuse_map(bsdf)
# We do not support Phong shading. What year is this, 1995?
return self.textures
def detect_unlit_emissive_map(self, bsdf):