Вот код, который я использую:
Вот код, который я использую:
Код: Выделить всё
import bpy
import ifcopenshell
import ifcopenshell.geom
# IFC and GLTF paths
ifc_file_path = "C:/Users/eduar/Downloads/TallBuilding.ifc"
gltf_output_path = "C:/Users/eduar/Downloads/TallBuilding.gltf"
# Blender and IfcOpenShell setup
settings = ifcopenshell.geom.settings()
settings.set(settings.USE_WORLD_COORDS, True)
bpy.ops.wm.read_factory_settings(use_empty=True)
ifc_file = ifcopenshell.open(ifc_file_path)
# Create material based on IFC element properties
def create_material(element):
material_name = element.GlobalId
if material_name not in bpy.data.materials:
mat = bpy.data.materials.new(name=material_name)
mat.use_nodes = True
bsdf = mat.node_tree.nodes.get("Principled BSDF")
if bsdf:
bsdf.inputs["Base Color"].default_value = (0.8, 0.8, 0.8, 1)
return mat
return bpy.data.materials[material_name]
# Collection creation helper
def get_or_create_collection(name, parent_collection=None):
if name in bpy.data.collections:
return bpy.data.collections[name]
new_collection = bpy.data.collections.new(name)
if parent_collection:
parent_collection.children.link(new_collection)
else:
bpy.context.scene.collection.children.link(new_collection)
return new_collection
# Create objects from IFC elements and assign materials
for element in ifc_file.by_type("IfcProduct"):
try:
if hasattr(element, "Representation") and element.Representation:
shape = ifcopenshell.geom.create_shape(settings, element)
vertices = shape.geometry.verts
faces = shape.geometry.faces
verts = [(vertices[i], vertices[i + 1], vertices[i + 2]) for i in range(0, len(vertices), 3)]
faces = [(faces[i], faces[i + 1], faces[i + 2]) for i in range(0, len(faces), 3)]
mesh = bpy.data.meshes.new(name=element.GlobalId)
mesh.from_pydata(verts, [], faces)
mesh.update()
obj = bpy.data.objects.new(name=element.GlobalId, object_data=mesh)
if hasattr(element, "Name") and element.Name:
collection = get_or_create_collection(element.Name)
else:
collection = get_or_create_collection("Uncategorized")
collection.objects.link(obj)
material = create_material(element)
obj.data.materials.append(material)
except Exception as e:
print(f"Error in element {element.GlobalId}: {e}")
# GLTF export
bpy.ops.export_scene.gltf(
filepath=gltf_output_path,
export_format='GLTF_SEPARATE',
export_texcoords=True,
export_normals=True,
export_materials='EXPORT',
export_yup=True
)
Подробнее здесь: https://stackoverflow.com/questions/791 ... l-settings
Мобильная версия