Esempio n. 1
0
 def getInputList(self):
     """Gets a :class:`collada.source.InputList` representing the inputs from a primitive"""
     inpl = InputList()
     for (key, tupes) in self.sources.iteritems():
         for (offset, semantic, source, set, srcobj) in tupes:
             inpl.addInput(offset, semantic, source, set)
     return inpl
Esempio n. 2
0
    def mesh(self, b_mesh):
        vert_srcid = b_mesh.name + '-vertary'
        vert_f = [c for v in b_mesh.vertices for c in v.co]
        vert_src = FloatSource(vert_srcid, np.array(vert_f), ('X', 'Y', 'Z'))

        sources = [vert_src]

        smooth = list(filter(lambda f: f.use_smooth, b_mesh.faces))
        if any(smooth):
            vnorm_srcid = b_mesh.name + '-vnormary'
            norm_f = [c for v in b_mesh.vertices for c in v.normal]
            norm_src = FloatSource(vnorm_srcid, np.array(norm_f), ('X', 'Y', 'Z'))
            sources.append(norm_src)
        flat = list(filter(lambda f: not f.use_smooth, b_mesh.faces))
        if any(flat):
            fnorm_srcid = b_mesh.name + '-fnormary'
            norm_f = [c for f in flat for c in f.normal]
            norm_src = FloatSource(fnorm_srcid, np.array(norm_f), ('X', 'Y', 'Z'))
            sources.append(norm_src)

        name = b_mesh.name + '-geom'
        geom = Geometry(self._collada, name, name, sources)

        if any(smooth):
            ilist = InputList()
            ilist.addInput(0, 'VERTEX', _url(vert_srcid))
            ilist.addInput(1, 'NORMAL', _url(vnorm_srcid))
            # per vertex normals
            indices = np.array([
                i for v in [
                    (v, v) for f in smooth for v in f.vertices
                ] for i in v])
            if _is_trimesh(smooth):
                p = geom.createTriangleSet(indices, ilist, 'none')
            else:
                vcount = [len(f.vertices) for f in smooth]
                p = geom.createPolylist(indices, vcount, ilist, 'none')
            geom.primitives.append(p)
        if any(flat):
            ilist = InputList()
            ilist.addInput(0, 'VERTEX', _url(vert_srcid))
            ilist.addInput(1, 'NORMAL', _url(fnorm_srcid))
            indices = []
            # per face normals
            for i, f in enumerate(flat):
                for v in f.vertices:
                    indices.extend([v, i])
            indices = np.array(indices)
            if _is_trimesh(flat):
                p = geom.createTriangleSet(indices, ilist, 'none')
            else:
                vcount = [len(f.vertices) for f in flat]
                p = geom.createPolylist(indices, vcount, ilist, 'none')
            geom.primitives.append(p)

        self._collada.geometries.append(geom)
        return geom
Esempio n. 3
0
def geometry_node_from_segments(collada, vertices, edges, color, name,
                                description):
    vg.shape.check(locals(), "vertices", (-1, 3))
    vg.shape.check(locals(), "edges", (-1, 2))

    vertex_source_name = f"{name}_verts"
    vertex_source = FloatSource(vertex_source_name, vertices, ("X", "Y", "Z"))
    geometry = Geometry(collada, name, description, [vertex_source])
    input_list = InputList()
    input_list.addInput(0, "VERTEX", f"#{vertex_source_name}")

    material_name = f"{name}_material"
    lineset = geometry.createLineSet(edges.ravel(), input_list, material_name)

    geometry.primitives.append(lineset)
    collada.geometries.append(geometry)

    return GeometryNode(
        geometry, [create_material(collada, name=material_name, color=color)])
Esempio n. 4
0
def geometry_node_from_mesh(collada, mesh, name):
    vertex_source_name = f"{name}_verts"
    geometry = Geometry(
        collada,
        name or "geometry0",
        str(mesh),
        [FloatSource(vertex_source_name, mesh.v.ravel(), ("X", "Y", "Z"))],
    )
    input_list = InputList()
    input_list.addInput(0, "VERTEX", f"#{vertex_source_name}")

    material_name = f"{name}_material"
    triset = geometry.createTriangleSet(mesh.f.ravel(), input_list,
                                        material_name)

    geometry.primitives.append(triset)
    collada.geometries.append(geometry)

    return GeometryNode(geometry,
                        [create_material(collada, name=material_name)])
Esempio n. 5
0
        def encode_mesh(b_mesh, b_mesh_name, b_material_slots):
            def is_trimesh(faces):
                return all([len(f.verts) == 3 for f in faces])

            #end is_trimesh

        #begin encode_mesh
            mesh_name = DATABLOCK.MESH.nameid(b_mesh_name)
            sources = []
            vert_srcid = self.next_internal_id()
            sources.append \
              (
                FloatSource
                  (
                    id = vert_srcid,
                    data = np.array([c for v in b_mesh.verts for c in v.co]),
                    components = ("X", "Y", "Z")
                  )
              )
            vnorm_srcid = self.next_internal_id()
            sources.append \
              (
                FloatSource
                  (
                    id = vnorm_srcid,
                    data = np.array([c for v in b_mesh.verts for c in v.normal]),
                    components = ("X", "Y", "Z")
                  )
              ) # todo: face normal might be different for flat shading
            uv_ids = []
            if b_mesh.loops.layers.uv.active != None:
                active_uv_name = b_mesh.loops.layers.uv.active.name
            else:
                active_uv_name = None
            #end if
            for i, (b_uvname,
                    uvlayer) in enumerate(b_mesh.loops.layers.uv.items()):
                uv_name = self.next_internal_id()
                uv_ids.append((uv_name, b_uvname))
                sources.append \
                  (
                    FloatSource
                      (
                        id = uv_name,
                        data = np.array
                          (
                            [
                                x
                                for f in b_mesh.faces
                                for l in f.loops
                                for x in l[uvlayer].uv
                            ]
                          ),
                        components = ("S", "T")
                      )
                  )
            #end for
            geom = Geometry(self._collada, mesh_name, mesh_name, sources)
            blendstuff = self.blender_technique(True, geom)
            if blendstuff != None:
                names = E.layer_names()
                for u in uv_ids:
                    names.append(E.name(name=u[1], refid=u[0], type="UV"))
                #end for
                blendstuff.append(names)
            #end if

            for slotindex in range(max(len(b_material_slots), 1)):
                slotname = make_slotname(slotindex)
                assigned = \
                    [
                        f
                        for f in b_mesh.faces
                        if f.material_index == slotindex
                    ]
                if any(assigned):
                    ilist = InputList()
                    ilist.addInput(0, "VERTEX", idurl(vert_srcid))
                    ilist.addInput(0, "NORMAL", idurl(vnorm_srcid))
                    setnr = 0
                    for u in uv_ids:
                        setnr += 1
                        ilist.addInput(1, "TEXCOORD", idurl(u[0]),
                                       (setnr, 0)[u[1] == active_uv_name])
                        # always assign set 0 to active UV layer
                    #end for
                    indices = []
                    for face in b_mesh.faces:
                        for face_loop in face.loops:
                            this_face = [face_loop.vert.index, face_loop.index]
                            indices.extend(this_face)
                        #end for
                    #end for
                    indices = np.array(indices)
                    if is_trimesh(assigned):
                        p = geom.createTriangleSet(indices, ilist, slotname)
                    else:
                        vcounts = [len(f.verts) for f in assigned]
                        p = geom.createPolylist(indices, vcounts, ilist,
                                                slotname)
                    #end if
                    geom.primitives.append(p)
                #end if
            #end for

            self._collada.geometries.append(geom)
            return geom