This is definitely quite an in-depth discussion, on the topic of joining paths
and copying files :-)
While str.join() is extremely useful, I tend to avoid joining paths on a
explicit path separator and defer to os.path.join, as it decides the path
separator and also automatically handles the case of an option trailing slash
on a component:
os.path.join("/foo", "bar")
# vs
os.path.join("/foo/", "bar")
os.path.join also has the benefit of taking *args, as opposed to where you are
creating a temporary list to pass your arguments to str.join()
path = '/'.join( [ scenepath , pathname ] )
# vs
path = os.path.join(scenepath, pathname)
Also, you talk about the "os" module as if its not "just python", even though
its the python stdlib. If they made every possible function in the stdlib a
builtin, you would have quite a large initial memory footprint when you started
python, most of which you would never need at the same time. So you import what
you need, but there is really not much "traceability" and "logging" issues to
worry about with when compared to using the maya.cmds alternatives. Any crash
you get with the "os" module is going to give you quite a useful traceback.
As for shutil, I am only claiming it is useful for copying files, again as
opposed to relying on what maya.cmds might provide. Save the file with maya,
then copy it to wherever you want with shutil, as opposed to having to perform
the overhead of multiple scene saves.
On May 14, 2013, at 11:32 PM, Ævar Guðmundsson wrote:
> Good point Justin, thanks.
>
> In an attempt to explain here is my reasoning:
>
> [ operating system path module ] os.path.join()
> -Easy to use and has a lot of catches
> -One extra module import, meaning more need for traceability and logging and
> such.
>
> [ built-in function ] ‘/’.join
> -Is built-in so traceability and extended knowledge requirements on my behalf
> are None, this is just using python.
>
> Example of how easy it is to have fun with joining things, paths or not:
> " ".join(mylist)
> {note the space}
>
> " ".join(['Bob', 'Bobo', 'Bubba'])
> (Note the addition if the square brackets inside the normal brackets.
>
> Easy identification:
> " <--> ".join(mylist())
>
> Alphabetical
> " ".join(sorted(mydict.keys()))
>
> For every n in the list, do things to each one
> print [ n for n in " ".join(sorted(mydict.keys())) ]
>
> Filter
> print [ n for n in " ".join(sorted(mydict.keys())) if "Bobo" in n]
>
> But having said that I feel comfortable using the built-in join function in
> this case as I know what I’m querying will always return a path in Windows
> form or Unix style and all I’m doing is slicing the two elements together.
> For more elaborate path joining depending on variable resolution and such I
> tend to stick to os.path.join() as it has things in there that catch and
> construct bespoke paths quite well. Can you throw me an example of how you
> would approach the same scenario using the os.path module? If there are
> additional catches and a compelling reason to use it I certainly will, just
> haven’t read many cases where the path module is more useful than just simply
> joining things.
>
> As for not using shutil, no real reason actually, got burnt on using it a
> few times in the past so I tend to steer away from it, but it’s more of a
> personal grief than anything technical, what it was being used for was odd so
> the module itself is not really to blame I’d think. Do you have an example
> of how to use shutil to ensure my backed up file is the same file as is in my
> scene directory, anything other than a physical duplicate of the backed up
> file will suffice I’d think as file size becomes an issue quite fast when
> working with large files or working with a project off a small media.
>
> I updated the code a little bit to add automatic versioning as well with a
> prefix choice and cleaned up the path naming. My current issue is still that
> once my file has been saved into the new directory and retains the original
> name the file in the root of my scenes directory doesn’t update, as I
> mentioned above and Justin suggests shutil should be able to sort this but as
> a side gimmick does anyone know if Maya has any notion of multiple files
> being the same file? As in save once , then build a representation of that
> file in another location without it delaying the actual save process?
>
> from os import listdir
> from maya.cmds import file
> from maya.cmds import workspace
>
> class Backup( object ):
>
> def __init__( self , foldername='' ):
> """Saves out an incremented backup file of your scene
>
> -folder argument is the name of the folder the backed up file
> gets saved into.
> If no foldername is given a versioned file gets saved into the
> root of the scenes directory.
>
> """
>
> self.__version__ = '0.0.3'
> self.foldername = foldername
>
> def create_path( self , filerule='scene' , name=False ):
> """Creates a path within the current projects scene directory
>
> -Use filerule to get the path of various workspace settings.
> -Use the name argument as the name of the directory to create
>
> """
>
> if not name:
> name = self.foldername
>
> scene = ''.join( [ workspace( q=1,rd=1 ) , workspace( filerule , q=1 ,
> fre=1 ) ] )
> self.path = '/'.join( [ scene , name ] )
> workspace( create=self.path )
>
> return self.path
>
> def save( self , version=None , type='mayaBinary' , name='_noname_' ):
> """Places a saved out scene on disk
>
> -Version argument adds that version name at the back of the scene
> name.
> -Type argument can be mayaBinary or mayaAscii
> -Use name to set our scene name.
>
> """
>
> path = self.create_path()
>
> if not version:
> version = self.get_version( path )
>
> file( rename='%s %s' % ( '/'.join( [ path , name ] ) , version ) )
> file( save=True , type=type )
> file( rename=name )
>
> return True
>
> def get_version( self ,path , prefix='v' , initialversion='001' ):
> """Returns a valid version number for a given path
>
> -Use the prefix argument to set a prepended name on your
> version ID.
> -initialversion can be used to state a version number to start
> from if the backup directory is empty.
>
> """
>
> backups = sorted( [ n for n in listdir( path ) ] )
>
> if backups==[]:
> return ''.join( [ prefix , initialversion ] )
> else:
> last_backup = backups[-1]
> ver = [ n[ 1: ].split( '.' )[ 0 ] for n in last_backup.split()
> if n.startswith( 'v' ) ][0]
> new = ''.join( [ prefix , str( int( ver ) + 1 ).zfill( 3 ) ] )
>
> return new
>
> if __name__ == '__main__':
> __oo__ = Backup( foldername='Backups' )
> __oo__.save( name='Porks Werfect' )
>
> Comments and discussion of course welcome
>
> p.s. will learn how pastebin works before this gets any longer :)
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
--
You received this message because you are subscribed to the Google Groups
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.