Example #1
0
 def extendNode(self, vert, toVert, o, bm):
     """
     Extend the open edge loop from the node set for the template vertex <vert> towards the template vertex <toVert>
     """
     vid = self.getVid(vert)
     toVid = self.getVid(toVert)
     
     verts = getVertsForVertexGroup(o, bm, o.vertex_groups["e_" + vid + "_" +toVid].name)
     
     # perform translation of <verts> along the vector defined by the vertices <vid> and <toVid>
     
     # unit vector for the line defined by the vertices <vid> and <toVid>
     e = (toVert.co - vert.co).normalized()
     # We need to take into account offset when calculating the amount of translation,
     # so pick up an arbitrary vertex from <verts>, get a vector by
     # subtracting <vert.co> and project that vector onto the line defined by
     # the vertices <vert> and <toVert> to calculate the offset relative to the vertex <vert>
     bmesh.ops.translate(
         bm,
         verts=getVertsForVertexGroup(o, bm, o.vertex_groups["e_" + vid + "_" +toVid].name),
         vec=toVert.co - vert.co - projectOntoLine(verts[0].co - vert.co, e)
     )
Example #2
0
    def extendNode(self, vert, toVert, o, bm):
        """
        Extend the open edge loop from the node set for the template vertex <vert> towards the template vertex <toVert>
        """
        vid = self.getVid(vert)
        toVid = self.getVid(toVert)

        verts = getVertsForVertexGroup(
            o, bm, o.vertex_groups["e_" + vid + "_" + toVid].name)

        # perform translation of <verts> along the vector defined by the vertices <vid> and <toVid>

        # unit vector for the line defined by the vertices <vid> and <toVid>
        e = (toVert.co - vert.co).normalized()
        # We need to take into account offset when calculating the amount of translation,
        # so pick up an arbitrary vertex from <verts>, get a vector by
        # subtracting <vert.co> and project that vector onto the line defined by
        # the vertices <vert> and <toVert> to calculate the offset relative to the vertex <vert>
        bmesh.ops.translate(bm,
                            verts=getVertsForVertexGroup(
                                o, bm, o.vertex_groups["e_" + vid + "_" +
                                                       toVid].name),
                            vec=toVert.co - vert.co -
                            projectOntoLine(verts[0].co - vert.co, e))
Example #3
0
    def insertAssets(self, context):
        o = self.o
        nodes = self.nodes
        nodeCache = self.nodeCache

        # scan the template for assets
        for a in o.children:
            if a.get("t") != "asset":
                continue
            vid1 = a["vid1"]
            vid2 = a["vid2"]

            bm = getBmesh(o)
            vert1 = getVertsForVertexGroup(o, bm, vid1)[0]
            vert2 = getVertsForVertexGroup(o, bm, vid2)[0]
            v1 = vert1.co
            v2 = vert2.co

            # get the edge connecting two vertices <v1> and <v2>
            for edge in vert1.link_edges:
                if vert2 in edge.verts:
                    break
            location = projectOntoLine(a.location - v1, (v2 - v1).normalized())
            # relative location on the edge
            location = location.length / (v2 - v1).length
            # If the <edge> is external, take into account offsets for the vertices <v1> and <v2>,
            # otherwise don't take offsets into account
            if len(edge.link_faces) == 1:
                # the only BMLoop for <edge>
                loop = edge.link_loops[0]
                # do we walk from <vert2> to <vert1> or in the opposite direction
                fromVert2 = loop.vert == vert2
                if fromVert2:
                    # temporarily swap <vert1> and <vert2>
                    vert1, vert2 = vert2, vert1
                # edge vector for the corner of the vertex <vert1>
                edgeVec1 = getVectorFromEdge(edge, vert1)
                # edge vector for corner of the <vert2> is the other external edge for <vert2>
                for edgeVec2 in vert2.link_edges:
                    if edgeVec2 != edge and len(edgeVec2.link_faces) == 1:
                        break
                edgeVec2 = getVectorFromEdge(edgeVec2, vert2)
                if fromVert2:
                    # swap the values back
                    vert1, vert2 = vert2, vert1
                    edgeVec1, edgeVec2 = edgeVec2, edgeVec1
                # adding offset to the vectors <v1> and <v2>
                v1 += self.parentTemplate.childOffsets.get(vid1, edgeVec1)
                v2 += self.parentTemplate.childOffsets.get(vid2, edgeVec2)

            location = v1 + location * (v2 - v1)

            # calculate the offset <tOffset> perpedicular to the vector <v2 - v1> (i.e. transverse offset)
            # type of the asset
            t = a.get("t2")

            def processOffset(tOffset, pVid, sVid):
                """
                A helper function to check if we have an appropriate <tOffset>
                
                Args:
                    tOffset (list): Blender EMPTY object serving as asset placeholder
                    pVid (str): Primary vid
                    sVid (str): the other vid
                
                Returns:
                    A tuple with <tOffset> (may be set to None) and transformation matrix (may be None)
                """
                # index of the open end of the node Blender object to which the asset is attached
                e = tOffset.get("e")
                ends = nodes[pVid].ends["e_" + str(e)]
                if pVid in ends and sVid in ends:
                    matrix = nodes[pVid].matrix
                else:
                    matrix = None
                    tOffset = None
                return tOffset, matrix

            # check if the asset placeholder is set for the node with <vid1>
            tOffset = nodeCache.get(o[vid1]).assets[t]
            if tOffset:
                tOffset, matrix = processOffset(tOffset, vid1, vid2)
            if not tOffset:
                # check if the asset placeholder is set for the node with <vid2>
                tOffset = nodeCache.get(o[vid2]).assets[t]
                tOffset, matrix = processOffset(tOffset, vid2, vid1)
            if tOffset:
                tOffset = matrix * tOffset.location if matrix else tOffset.location.copy(
                )
                location += projectOntoPlane(tOffset, (v2 - v1).normalized())
            bm.free()
            # parent object for the hierarchy of assets
            p = createEmptyObject("test", location, True, empty_draw_size=0.01)
            parent_set(self.meshObject.parent, p)
            # import asset
            a = appendFromFile(
                context,
                os.path.join(context.scene.prk.baseDirectory, a["path"]))
            a.location = zeroVector.copy()
            parent_set(p, a)
Example #4
0
 def insertAssets(self, context):
     o = self.o
     nodes = self.nodes
     nodeCache = self.nodeCache
     
     # scan the template for assets
     for a in o.children:
         if a.get("t") != "asset":
             continue
         vid1 = a["vid1"]
         vid2 = a["vid2"]
         
         bm = getBmesh(o)
         vert1 = getVertsForVertexGroup(o, bm, vid1)[0]
         vert2 = getVertsForVertexGroup(o, bm, vid2)[0]
         v1 = vert1.co
         v2 = vert2.co
         
         # get the edge connecting two vertices <v1> and <v2>
         for edge in vert1.link_edges:
             if vert2 in edge.verts:
                 break
         location = projectOntoLine(a.location-v1, (v2-v1).normalized())
         # relative location on the edge
         location = location.length / (v2 - v1).length
         # If the <edge> is external, take into account offsets for the vertices <v1> and <v2>,
         # otherwise don't take offsets into account
         if len(edge.link_faces) == 1:
             # the only BMLoop for <edge>
             loop = edge.link_loops[0]
             # do we walk from <vert2> to <vert1> or in the opposite direction
             fromVert2 = loop.vert == vert2
             if fromVert2:
                 # temporarily swap <vert1> and <vert2>
                 vert1, vert2 = vert2, vert1
             # edge vector for the corner of the vertex <vert1>
             edgeVec1 = getVectorFromEdge(edge, vert1)
             # edge vector for corner of the <vert2> is the other external edge for <vert2>
             for edgeVec2 in vert2.link_edges:
                 if edgeVec2 != edge and len(edgeVec2.link_faces) == 1:
                     break
             edgeVec2 = getVectorFromEdge(edgeVec2, vert2)
             if fromVert2:
                 # swap the values back
                 vert1, vert2 = vert2, vert1
                 edgeVec1, edgeVec2 = edgeVec2, edgeVec1
             # adding offset to the vectors <v1> and <v2>
             v1 += self.parentTemplate.childOffsets.get(vid1, edgeVec1)
             v2 += self.parentTemplate.childOffsets.get(vid2, edgeVec2)
         
         location = v1 + location * (v2 - v1)
         
         # calculate the offset <tOffset> perpedicular to the vector <v2 - v1> (i.e. transverse offset)
         # type of the asset
         t = a.get("t2")
         def processOffset(tOffset, pVid, sVid):
             """
             A helper function to check if we have an appropriate <tOffset>
             
             Args:
                 tOffset (list): Blender EMPTY object serving as asset placeholder
                 pVid (str): Primary vid
                 sVid (str): the other vid
             
             Returns:
                 A tuple with <tOffset> (may be set to None) and transformation matrix (may be None)
             """
             # index of the open end of the node Blender object to which the asset is attached
             e = tOffset.get("e")
             ends = nodes[pVid].ends["e_"+str(e)]
             if pVid in ends and sVid in ends:
                 matrix = nodes[pVid].matrix
             else:
                 matrix = None
                 tOffset = None
             return tOffset, matrix
         
         # check if the asset placeholder is set for the node with <vid1>
         tOffset = nodeCache.get(o[vid1]).assets[t]
         if tOffset:
             tOffset, matrix = processOffset(tOffset, vid1, vid2)
         if not tOffset:
             # check if the asset placeholder is set for the node with <vid2>
             tOffset = nodeCache.get(o[vid2]).assets[t]
             tOffset, matrix = processOffset(tOffset, vid2, vid1)
         if tOffset:
             tOffset = matrix*tOffset.location if matrix else tOffset.location.copy()
             location += projectOntoPlane(tOffset, (v2 - v1).normalized())
         bm.free()
         # parent object for the hierarchy of assets
         p = createEmptyObject("test", location, True, empty_draw_size=0.01)
         parent_set(self.meshObject.parent, p)
         # import asset
         a = appendFromFile(context, os.path.join(context.scene.prk.baseDirectory, a["path"]))
         a.location = zeroVector.copy()
         parent_set(p, a)