Exemple #1
0
def make_fov(sweep=90, steps=16, scale=100):
    z = 1 + random.uniform(-0.01, 0.01)

    data = EggData()

    vp = EggVertexPool('fan')
    data.addChild(vp)

    poly = EggPolygon()
    data.addChild(poly)

    v = EggVertex()
    v.setPos(Point3D(0, 0, z))
    poly.addVertex(vp.addVertex(v))

    rads = deg2Rad(sweep)

    for i in range(steps + 1):
        a = rads * i / steps
        y = math.sin(a)
        x = math.cos(a)

        v = EggVertex()
        v.setPos(Point3D(x*scale, y*scale, z))
        poly.addVertex(vp.addVertex(v))

    node = loadEggData(data)
    np = NodePath(node)
    np.setH(sweep/2)
    return np
    def __init__(self, in_file, out_file):
        #New container for data:
        self.data = EggData()

        #New Container for vertex:
        self.vertexPool = EggVertexPool('model')

        #Adding vertexPool to the main data container:
        self.data.addChild(self.vertexPool) 
        self.load(in_file)
        self.data.setCoordinateSystem(CSZupRight)
        self.data.writeEgg(out_file)
Exemple #3
0
class createNurbsCurve():
    def __init__(self):
        self.data = EggData()
        self.vtxPool = EggVertexPool('mopath')
        self.data.addChild(self.vtxPool)
        self.eggGroup = EggGroup('group')
        self.data.addChild(self.eggGroup)
        self.myverts = []

    def addPoint(self, pos):
        eggVtx = EggVertex()
        eggVtx.setPos(Point3D(pos[0], pos[1], pos[2]))
        self.myverts.append(eggVtx)
        self.vtxPool.addVertex(eggVtx)

    def getNodepath(self):
        myCurve = EggNurbsCurve()
        myCurve.setup(3, len(self.myverts) + 3)
        myCurve.setCurveType(1)
        for i in self.myverts:
            myCurve.addVertex(i)
        self.eggGroup.addChild(myCurve)
        return NodePath(loadEggData(self.data))
Exemple #4
0
    def __polylinestoegg(self, egg, objname, groupname):
        selectedlines = self.__linesby(objname, groupname)
        if len(selectedlines) == 0:
            return

        eobj = EggGroup(objname)
        egg.addChild(eobj)
        egrp = EggGroup(groupname)
        eobj.addChild(egrp)
        evpool = EggVertexPool(groupname)
        egrp.addChild(evpool)
        for line in selectedlines:
            (vlist, mdata) = line
            (wobj, wgrp, wmat) = mdata
            eline = EggLine()
            egrp.addChild(eline)
            self.__eggifymats(eline, wmat)
            self.__eggifyverts(eline, evpool, vlist)
Exemple #5
0
    def __facestoegg(self, egg, objname, groupname):
        selectedfaces = self.__facesby(objname, groupname)
        if len(selectedfaces) == 0:
            return

        eobj = EggGroup(objname)
        egg.addChild(eobj)
        egrp = EggGroup(groupname)
        eobj.addChild(egrp)
        evpool = EggVertexPool(groupname)
        egrp.addChild(evpool)
        for face in selectedfaces:
            (vlist, mdata) = face
            (wobj, wgrp, wmat) = mdata
            epoly = EggPolygon()
            egrp.addChild(epoly)
            self.__eggifymats(epoly, wmat)
            self.__eggifyverts(epoly, evpool, vlist)
Exemple #6
0
def makeEgg(filename):

    # Egg data
    vp = EggVertexPool('sky')

    data = EggData()
    data.addChild(vp)
    data.setCoordinateSystem(1)  # CS_zup_right

    # Vertices
    vmap = {}
    for idx, (x, y, z) in enumerate(vertices):
        vmap[idx] = makeVertex(vp, x, y, z)

    # Faces
    for (i1, i2, i3) in indices:
        v1 = vmap[i1]
        v2 = vmap[i2]
        v3 = vmap[i3]
        makePoly(data, [v1, v2, v3])

    # Save
    data.recomputePolygonNormals()
    data.writeEgg(Filename(filename))
Exemple #7
0
    def createCuttingPlanes(self):
        self.planes = self.render.attachNewNode("planes")
        pl = self.planes.attachNewNode("pl")
        data = EggData()
        self.planedata = []

        vp = EggVertexPool('plane')
        data.addChild(vp)

        fac = 1.0
        expanse = 10.0
        rng = 4
        for i in range(-rng, rng):
            poly = EggPolygon()
            data.addChild(poly)

            self.planedata.append(i * fac)

            v = EggVertex()
            v.setPos(Point3D(i * fac, -expanse, -expanse))
            poly.addVertex(vp.addVertex(v))
            v = EggVertex()
            v.setPos(Point3D(i * fac, -expanse, +expanse))
            poly.addVertex(vp.addVertex(v))
            v = EggVertex()
            v.setPos(Point3D(i * fac, +expanse, +expanse))
            poly.addVertex(vp.addVertex(v))
            v = EggVertex()
            v.setPos(Point3D(i * fac, +expanse, -expanse))
            poly.addVertex(vp.addVertex(v))

        node = loadEggData(data)
        np = NodePath(node)
        np.reparentTo(pl)
        np.setColor(0, 1, 0, 1)
        np.setTwoSided(True)
        np.setTransparency(TransparencyAttrib.MAlpha)
        np.setAlphaScale(0.1)
        np.setCollideMask(BitMask32(0x0))
        return self.planes
Exemple #8
0
class STL2EGG():
    def __init__(self, in_file, out_file):
        #New container for data:
        self.data = EggData()

        #New Container for vertex:
        self.vertexPool = EggVertexPool('model')

        #Adding vertexPool to the main data container:
        self.data.addChild(self.vertexPool) 
        self.load(in_file)
        self.data.setCoordinateSystem(CSZupRight)
        self.data.writeEgg(out_file)

    def load(self, filename):
        f = open(filename, 'r')
        header = f.read(6)
        isASCII = ( header== 'solid ')
        if isASCII:
            self.name = f.readline()
            self.parseASCII(f)
        else:
            f.close
            f = open(filename, 'rb')
            self.parseBin(f)
                
    def getName(self):
        return self.name
           
    def readVecteur(self, data):
        v = EggVertex()
        x,y,z = struct.unpack('fff', data)        
        v.setPos(Point3D(x,y,z))
        return v 
                  
    def parseBin(self, f):
        print "Parsing Bin File"
        Header = f.read(80)
        Nb_Face = struct.unpack('i', f.read(4))[0]

        print "Nombre de face : %i" % (Nb_Face)
        for i in range(0,Nb_Face):
            self.poly = EggPolygon()
            self.data.addChild(self.poly)

            # Normal struct
            normal = self.readVecteur(f.read(12))
            u      = self.readVecteur(f.read(12))
            v      = self.readVecteur(f.read(12))
            w      = self.readVecteur(f.read(12))
            attrib   = struct.unpack('H'  , f.read(2))[0]   # Attributes

            #Adding Vertex to Triangle 
            self.poly.addVertex(self.vertexPool.addVertex(u))
            self.poly.addVertex(self.vertexPool.addVertex(v))
            self.poly.addVertex(self.vertexPool.addVertex(w))
            self.poly.recomputePolygonNormal()
            self.poly.setColor( VBase4( 0.0, 0.0, 0.5, 0.5) )

    def parseASCII(self,f ):       
        for line in f:
            line = line.lower().strip()
            commande = line.split(" ")
            Nb_Param = len(commande)
            try:
                {'facet'    : self.facet,
                 'outer'    : self.outer,
                 'vertex'   : self.vertex,
                 'endloop'  : self.endloop,
                 'endfacet' : self.endfacet}[commande[0]](commande,Nb_Param)
            except KeyError:
                pass

    def facet(self,commande,Nb_Param):
        if (Nb_Param==5 and commande[1] == "normal"):
            #We are Ignoring normal --> will be computed later
            #Creating a new polygon :
            self.poly = EggPolygon()
            self.data.addChild(self.poly)

    def outer(self,commande,Nb_Param):
        pass

    def vertex(self,commande,Nb_Param):
        if (Nb_Param==4):
            x,y,z = float(commande[1]),float(commande[2]),float(commande[3])
            if (self.poly != None):
                #Creating a new vertex with coords :
                v = EggVertex()
                v.setPos(Point3D(x,y,z))

                #Adding the new Vertex to the polygon :
                self.poly.addVertex(self.vertexPool.addVertex(v))

    def endloop(self,commande,Nb_Param):
        #End of the Loop : 
        self.poly.recomputePolygonNormal()

        #As STL files don't contain colors:
        self.poly.setColor( VBase4( 0.0, 0.0, 0.5, 0.5) )

    def endfacet(self,commande,Nb_Param):
        pass
Exemple #9
0
    def make_geom(self, node, obj, can_merge=False):
        triangulate = not is_collision(obj)
        if self._geom_scale != 1:
            obj.scale.x = self._geom_scale
            obj.scale.y = self._geom_scale
            obj.scale.z = self._geom_scale
        apply_modifiers(obj, triangulate=triangulate)
        mesh = obj2mesh(obj, triangulate=triangulate)

        # get or create materials and textures
        egg_materials = {}
        egg_textures = {}
        egg_material_textures = {}
        if not self._no_materials and not is_collision(obj):
            for material in mesh.materials.values():
                # material
                for child in self._root.get_children():  # existing material
                    if (isinstance(child, EggMaterial) and
                            child.get_name() == material.name):
                        egg_materials[material.name] = child
                        break
                else:  # new material
                    egg_material = self.make_material(material)
                    self._root.add_child(egg_material)
                    egg_materials[material.name] = egg_material

                # material -> textures
                if material.name not in egg_material_textures:
                    egg_material_textures[material.name] = {}

                # textures
                if not self._no_textures:
                    for type_, _, egg_texture in self.make_textures(material):
                        tname = egg_texture.get_name()
                        for child in self._root.get_children():  # existing texture
                            if (isinstance(child, EggTexture) and
                                    child.get_name() == tname):
                                egg_textures[tname] = child
                                egg_material_textures[material.name][tname] = child
                                break
                        else:  # new texture
                            self._root.add_child(egg_texture)
                            egg_textures[tname] = egg_texture
                            egg_material_textures[material.name][tname] = egg_texture

        # get or create vertex pool
        egg_vertex_pool = None
        egg_vertex_id = 0
        if can_merge:
            for child in node.get_children():  # existing vertex pool
                if isinstance(child, EggVertexPool):
                    egg_vertex_pool = child
                    egg_vertex_id = egg_vertex_pool.get_highest_index()
                    break

        if egg_vertex_pool is None:  # new vertex pool
            egg_vertex_pool = EggVertexPool(node.get_name())
            egg_vertex_id = egg_vertex_pool.get_highest_index()
            node.add_child(egg_vertex_pool)

        # get armature and joints
        armature = get_armature(obj)
        egg_joints = {}
        if armature:
            for child in self._root.get_children():
                if (isinstance(child, EggGroup) and
                        child.get_dart_type() == EggGroup.DT_structured and
                        child.get_name() == armature.name):
                    egg_joints = self._get_joints(child)

        sharp_vertices = {}
        uv_tb = {}
        if not is_collision(obj):
            sharp_vertices = self.get_sharp_vertices(mesh)
            uv_tb = self.get_tangent_bitangent(mesh)
        egg_vertices = {}

        obj_matrix = get_object_matrix(obj, armature)
        parent_obj_matrix = obj_matrix
        if armature:
            parent_obj_matrix = get_object_matrix(armature)

        for polygon in mesh.polygons:
            # <-- polygon
            material = None
            mname = None
            if not self._no_materials:
                try:
                    material = mesh.materials[polygon.material_index]
                    mname = material.name
                except IndexError:
                    pass

            # make polygon
            egg_polygon = EggPolygon(mname or node.get_name())

            # set material and textures
            if material and not self._no_materials and not is_collision(obj):
                if mname in egg_materials:
                    egg_polygon.set_material(egg_materials[mname])

                # set textures
                if mname in egg_material_textures and not self._no_textures:
                    for egg_texture in egg_material_textures[mname].values():
                        egg_polygon.add_texture(egg_texture)

            # vertices
            for i, vertex_id in enumerate(polygon.vertices):
                # i is vertex counter inside a polygon
                # (0, 1, 2) for triangle
                # vertex_id is reusable id,
                # because multiple polygons can share the same vertices

                # <-- vertex
                vertex = mesh.vertices[vertex_id]
                use_smooth = (
                    polygon.use_smooth and
                    vertex_id not in sharp_vertices and
                    not is_collision(obj))

                # try to reuse shared vertices
                if (polygon.use_smooth and
                        vertex_id in egg_vertices and
                        not is_collision(obj)):
                    shared = False
                    for egg_vertex in egg_vertices[vertex_id]:
                        loop_id = polygon.loop_indices[i]
                        egg_vertex_uv = egg_vertex.get_uv_obj(
                            self._get_uv_name(mesh.uv_layers.active))

                        if not egg_vertex_uv:
                            egg_polygon.add_vertex(egg_vertex)
                            shared = True
                            break

                        if self.can_share_vertex(mesh, loop_id, egg_vertex_uv.get_uv()):
                            egg_polygon.add_vertex(egg_vertex)
                            shared = True
                            break
                    if shared:
                        continue

                # make new vertex data
                egg_vertex = self.make_vertex(
                    parent_obj_matrix, obj_matrix, polygon, vertex,
                    use_smooth=use_smooth)

                # uv layers
                if not is_collision(obj):
                    for uv_name, uv_layer in mesh.uv_layers.items():
                        # <-- vertex uv
                        loop_id = polygon.loop_indices[i]
                        uv_loop = uv_layer.data[loop_id]

                        # not active layer and extra UV disabled
                        if not uv_layer.active and self._no_extra_uv:
                            continue

                        egg_vertex_uv = self.make_vertex_uv(uv_layer, uv_loop.uv)
                        if uv_name in uv_tb:
                            t, b, s = uv_tb[uv_name][loop_id]
                            tangent = parent_obj_matrix @ t
                            binormal = parent_obj_matrix @ b
                            egg_vertex_uv.set_tangent(tuple(tangent))
                            egg_vertex_uv.set_binormal(tuple(binormal))
                        egg_vertex.set_uv_obj(egg_vertex_uv)
                        # vertex uv -->

                # generate new ID, add vertex and save last ID
                egg_vertex_id += 1
                egg_vertex_pool.add_vertex(egg_vertex, egg_vertex_id)
                egg_vertex_pool.set_highest_index(egg_vertex_id)
                egg_polygon.add_vertex(egg_vertex)

                # save vertex data for sharing
                if vertex_id not in egg_vertices:
                    egg_vertices[vertex_id] = []
                egg_vertices[vertex_id].append(egg_vertex)

                # attach joints to vertex
                if armature:
                    for vertex_group in vertex.groups:
                        obj_vertex_group = obj.vertex_groups[vertex_group.group]
                        if obj_vertex_group.name in egg_joints:
                            egg_joint = egg_joints[obj_vertex_group.name]
                            egg_joint.set_vertex_membership(
                                egg_vertex, vertex_group.weight)

                # vertex -->

            node.add_child(egg_polygon)