Exemplo n.º 1
0
def add_elset_from_bmeshes(structure, name, bmeshes=None, layer=None):
    """ Adds the Blender meshes' edges and faces as an element set.

    Parameters
    ----------
    structure : obj
        Structure object to update.
    name : str
        Name of the new element set.
    bmeshes : list
        Blender mesh objects to extract edges and faces.
    layer : int
        Layer to get bmeshes from if bmeshes are not given.

    Returns
    -------
    None

    Notes
    -----
    - Either bmeshes or layer should be given, not both.

    """

    if layer is not None:
        bmeshes = [
            object for object in get_objects(layer=layer)
            if object.type == 'MESH'
        ]

    elements = []

    for bmesh in bmeshes:

        blendermesh = BlenderMesh(bmesh)
        vertices = blendermesh.get_vertex_coordinates()
        edges = blendermesh.get_edge_vertex_indices()
        faces = blendermesh.get_face_vertex_indices()

        for u, v in edges:
            sp = structure.check_node_exists(vertices[u])
            ep = structure.check_node_exists(vertices[v])
            element = structure.check_element_exists([sp, ep])
            if element is not None:
                elements.append(element)

        for face in faces:
            nodes = [structure.check_node_exists(vertices[i]) for i in face]
            element = structure.check_element_exists(nodes)
            if element is not None:
                elements.append(element)

    structure.add_set(name=name, type='element', selection=elements)
Exemplo n.º 2
0
def network_from_bmesh(bmesh):
    """ Create a Network datastructure from a Blender mesh.

    Parameters:
        bmesh (obj): Blender mesh object.

    Returns:
        obj: Network object.
    """
    blendermesh = BlenderMesh(bmesh)
    vertices = blendermesh.get_vertex_coordinates()
    edges = blendermesh.get_edge_vertex_indices()
    network = Network.from_vertices_and_edges(vertices=vertices, edges=edges)
    return network
Exemplo n.º 3
0
def add_nodes_elements_from_bmesh(structure,
                                  bmesh,
                                  line_type=None,
                                  mesh_type=None,
                                  acoustic=False,
                                  thermal=False):
    """ Adds the Blender mesh's nodes, edges and faces to the Structure object.

    Parameters
    ----------
    structure : obj
        Structure object to update.
    bmesh : obj
        Blender mesh object.
    line_type : str
        Element type for lines (bmesh edges).
    mesh_type : str
        Element type for meshes.
    acoustic : bool
        Acoustic properties on or off.
    thermal : bool
        Thermal properties on or off.

    Returns
    -------
    list
        Node keys that were added to the Structure.
    list
        Element keys that were added to the Structure.

    """

    blendermesh = BlenderMesh(bmesh)
    vertices = blendermesh.get_vertex_coordinates()
    edges = blendermesh.get_edge_vertex_indices()
    faces = blendermesh.get_face_vertex_indices()

    try:
        name = blendermesh.guid
        if name[-5:-3] == '}.':
            name = name[:-4]
    except:
        pass

    created_nodes = set()
    created_elements = set()

    for vertex in vertices:
        node = structure.add_node(vertex)
        created_nodes.add(node)

    if line_type and edges:

        try:
            dic = json.loads(name.replace("'", '"'))
            ex = dic.get('ex', None)
            ey = dic.get('ey', None)
        except:
            ex = None
            ey = None
        axes = {'ex': ex, 'ey': ey}

        for u, v in edges:
            sp_xyz = vertices[u]
            ep_xyz = vertices[v]
            sp = structure.check_node_exists(sp_xyz)
            ep = structure.check_node_exists(ep_xyz)
            ez = subtract_vectors(ep_xyz, sp_xyz)
            if ex and not ey:
                ey = cross_vectors(ex, ez)
            axes['ey'] = ey
            axes['ez'] = ez
            e = structure.add_element(nodes=[sp, ep],
                                      type=line_type,
                                      acoustic=acoustic,
                                      thermal=thermal,
                                      axes=axes)
            if e is not None:
                created_elements.add(e)

    if mesh_type:

        if mesh_type in [
                'HexahedronElement', 'TetrahedronElement', 'SolidElement',
                'PentahedronElement'
        ]:
            nodes = [structure.check_node_exists(i) for i in vertices]
            e = structure.add_element(nodes=nodes,
                                      type=mesh_type,
                                      acoustic=acoustic,
                                      thermal=thermal)
            if e is not None:
                created_elements.add(e)

        else:
            try:
                dic = json.loads(name.replace("'", '"'))
                ex = dic.get('ex', None)
                ey = dic.get('ey', None)
                if ex and ey:
                    ez = cross_vectors(ex, ey)
                else:
                    ez = None
            except:
                ex = None
                ey = None
                ez = None
            axes = {'ex': ex, 'ey': ey, 'ez': ez}

            for face in faces:
                nodes = [
                    structure.check_node_exists(vertices[i]) for i in face
                ]
                e = structure.add_element(nodes=nodes,
                                          type=mesh_type,
                                          acoustic=acoustic,
                                          thermal=thermal,
                                          axes=axes)
                if e is not None:
                    created_elements.add(e)

    return list(created_nodes), list(created_elements)