Пример #1
0
    def exportFile(self, filename, object=None, mode="ascii"):
        """Export a PLY file.

        object is the object to export. If it is None, it will be taken
        from the scene. If there is more than one object in the scene,
        an exception is thrown.
        mode specifies whether the output file will be ascii or binary.
        The values can be "ascii", "little_endian", "big_endian".
        """

        if object==None:
            # Get a list of all objects that have a geom
            objs = list(getScene().walkWorld())
            objs = filter(lambda obj: obj.geom!=None, objs)
            if len(objs)==0:
                raise ValueError("No object to export.")
            elif len(objs)>1:
                raise ValueError("Only a single object can be exported.")
            object = objs[0]
            
        object = cmds.worldObject(object)
        if object.geom==None:
            raise ValueError("No geometry attached to object %s"%object.name)
        geom = self.convertObject(object)
        if geom==None:
            raise ValueError("Cannot export geometry of type %s as a PLY file"%(object.geom.__class__.__name__))

        # Open the file...
        ply = _core.PLYWriter()
        try:
            mode = eval ("_core.PlyStorageMode.%s"%mode.upper())
        except:
            raise ValueError("Invalid mode: %s"%mode)
        ply.create(filename, mode)

        # Set comment
        var = geom.findVariable("comment")
        if var!=None and var[1]==CONSTANT and var[2]==STRING and var[3]==1:
            slot = geom.slot("comment")
            for s in slot[0].split("\n"):
                ply.addComment(s)
        # Set obj_info
        var = geom.findVariable("obj_info")
        if var!=None and var[1]==CONSTANT and var[2]==STRING and var[3]==1:
            slot = geom.slot("obj_info")
            for s in slot[0].split("\n"):
                ply.addObjInfo(s)

        # Write the model
        ply.write(geom, object.worldtransform)
        ply.close()
Пример #2
0
    def exportFile(self, filename, root=None, mtlname=None, exportmtl=True):
        """Export an OBJ file.

        root is the root of the subtree that should be exported.
        If exportmtl is False, no MTL file is generated.
        mtlname determines the name of the MTL file (default: the same base
        name than filename).
        If exportmtl is False and no mtlname is given, then no material
        information will be written.
        """

        self.fhandle = file(filename, "w")

        self.use_materials = (exportmtl or mtlname!=None)
        self.root = cmds.worldObject(root)
        self.v_offset = 0
        self.vt_offset = 0
        self.vn_offset = 0

        self.group_offset = 0

        # This dictionary is used to find name clashes and store the materials
        # that have to be exported.
        # Key is the name of the material, value is the material object
        self.materials = {}

        # Determine the name of the MTL file (by changing the suffix)
        if mtlname==None:
            name, ext = os.path.splitext(filename)
            mtlname = name+".mtl"

        if self.use_materials:
            print >>self.fhandle, "mtllib %s"%os.path.basename(mtlname)

        # Export objects...
        if root!=None:
            self.group_offset = len(self.getGroups(self.root))-1
            self.exportObject(self.root)
        for obj in getScene().walkWorld(self.root):
            self.exportObject(obj)

        self.fhandle.close()

        # Export the MTL file...
        if exportmtl:
            self.exportMTL(mtlname)
Пример #3
0
    def exportFile(self, filename, root=None, mtlname=None, exportmtl=True):
        """Export an OBJ file.

        root is the root of the subtree that should be exported.
        If exportmtl is False, no MTL file is generated.
        mtlname determines the name of the MTL file (default: the same base
        name than filename).
        If exportmtl is False and no mtlname is given, then no material
        information will be written.
        """

        self.fhandle = file(filename, "w")

        self.use_materials = (exportmtl or mtlname!=None)
        self.root = cmds.worldObject(root)
        self.v_offset = 0
        self.vt_offset = 0
        self.vn_offset = 0

        self.group_offset = 0

        # This dictionary is used to find name clashes and store the materials
        # that have to be exported.
        # Key is the name of the material, value is the material object
        self.materials = {}

        # Determine the name of the MTL file (by changing the suffix)
        if mtlname==None:
            name, ext = os.path.splitext(filename)
            mtlname = name+".mtl"

        if self.use_materials:
            print >>self.fhandle, "mtllib %s"%os.path.basename(mtlname)

        # Export objects...
        if root!=None:
            self.group_offset = len(self.getGroups(self.root))-1
            self.exportObject(self.root)
        for obj in getScene().walkWorld(self.root):
            self.exportObject(obj)

        self.fhandle.close()

        # Export the MTL file...
        if exportmtl:
            self.exportMTL(mtlname)
Пример #4
0
    def exportFile(self, filename, root=None):
        """Export an OFF file.

        root is the root of the subtree that should be exported.
        """
        self.N_flag = False
        self.C_flag = False
        self.ST_flag = False

        # Create a list of objects that should be exported
        scene = getScene()
        self.objects = []
        root = cmds.worldObject(root)
        if root != None:
            self.objects.append(root)
        self.objects += list(scene.walkWorld(root))

        # Initialize variable flags and return number of verts and faces
        numverts, numfaces = self.getNumVertsNFaces()

        self.fhandle = file(filename, "w")
        # Write header line
        kw = ""
        if self.ST_flag:
            kw += "ST"
        if self.C_flag:
            kw += "C"
        if self.N_flag:
            kw += "N"
        kw += "OFF"
        print >> self.fhandle, kw

        # Write number of vertices and faces
        print >> self.fhandle, "%d %d 0" % (numverts, numfaces)

        # Write vertices...
        self.writeVertices()

        # Write faces
        self.writeFaces()

        self.fhandle.close()
Пример #5
0
    def exportFile(self, filename, root=None):
        """Export an OFF file.

        root is the root of the subtree that should be exported.
        """
        self.N_flag = False
        self.C_flag = False
        self.ST_flag = False

        # Create a list of objects that should be exported
        scene = getScene()
        self.objects = []
        root = cmds.worldObject(root)
        if root!=None:
            self.objects.append(root)
        self.objects += list(scene.walkWorld(root))

        # Initialize variable flags and return number of verts and faces
        numverts, numfaces = self.getNumVertsNFaces()

        self.fhandle = file(filename, "w")
        # Write header line
        kw = ""
        if self.ST_flag:
            kw += "ST"
        if self.C_flag:
            kw += "C"
        if self.N_flag:
            kw += "N"
        kw += "OFF"
        print >>self.fhandle, kw

        # Write number of vertices and faces
        print >>self.fhandle, "%d %d 0"%(numverts, numfaces)

        # Write vertices...
        self.writeVertices()

        # Write faces
        self.writeFaces()

        self.fhandle.close()
Пример #6
0
    def __init__(self,
                 name="group",
                 dynamics=True,
                 static=False,
                 childs=[],
                 **params):
        """Constructor.

        \param name (\c str) Object name
        \param childs (\c list) A list of WorldObjects or object names.
        """
        WorldObject.__init__(self, name=name, **params)

        self.dynamics_slot = BoolSlot(dynamics)
        self.static_slot = BoolSlot(static)
        self.addSlot("dynamics", self.dynamics_slot)
        self.addSlot("static", self.static_slot)

        for c in childs:
            c = cmds.worldObject(c)
            if c.parent != None:
                c.parent.removeChild(c)
            self.addChild(c)
Пример #7
0
    def __init__(self,
                 name="group",
                 dynamics=True,
                 static=False,
                 childs=[],
                 **params):
        """Constructor.

        \param name (\c str) Object name
        \param childs (\c list) A list of WorldObjects or object names.
        """
        WorldObject.__init__(self, name=name, **params)

        self.dynamics_slot = BoolSlot(dynamics)
        self.static_slot = BoolSlot(static)
        self.addSlot("dynamics", self.dynamics_slot)
        self.addSlot("static", self.static_slot)
        
        for c in childs:
            c = cmds.worldObject(c)
            if c.parent!=None:
                c.parent.removeChild(c)
            self.addChild(c)