python copy scripts into script

╄→尐↘猪︶ㄣ 提交于 2020-01-07 06:56:34

问题


Part 1: Copy Script

I have scripts on my computer that I want to continue to develop. These scripts work as embed scripts in maya files, so that people who open the file will get the tool upon opening it. I want to build in an update function, so that when I open the file, the version in the file will check against my local version, and if my local version is a higher version number, it will update to my script. I know how to replace the string value in maya, but i'm not certain how to copy the string value from an existing python file.

How can I copy the text from within a script to a string value? (preferably containing /n and indentation, while I'm at it)

version = 3.1
Try:
    import ToolBox
    if ToolBox.version > version:
        #do copy stuff

Part 2: Multiple Script Files

My script uses several separate files (simplified concept bellow)

file Hammer
    class Type():
        #force stuff
    def strike():
        #strike stuff
file Nail
    Class Type():
        #Type stuff
    def bindTogether():
        #bind stuff
file Wood
    #wood stuff..

file ToolBox
    Import Hammer
    Import Nail
    Import Wood
    def buildBox():
        #buildBox stuff using hammer, nail and wood

Can I import these into a class within my script and use them pretty much the same as I am now? If not, what can I do?

File Toolbox
    Class Hammer:
        class Type():
            #force stuff
        def strike():
            #strike stuff
    Class Nail:
        Class Type():
            #Type stuff
        def bindTogether():
            #bind stuff
    Class Wood:
        #wood stuff

    def buildBox():
        #buildBox stuff using hammer, nail and wood

回答1:


I think you want to reconsider the approach you're proposing. Distributing things piecemeal is asking for trouble: if your users get different code depending on what's going on on your machine and also on the order in which they do things, you can potentially get lots and lots of different states on different machines so each user can get their own unique set of bugs. You want to distribute the tools and download the tools as a unit so the user gets a complete suite of tools at a time, not one at a time.

The simplest way to do give your users an environment variable that points to a network share. Your userSetup.py can use that to copy the contents of the whole share to the users when they start up; that way the users get the same versions of everything. You need to be careful to delete any existing .pyc files as part of the process, since leftover pycs can fool python into using the wrong code.

Another option to consider is using Maya module files. Once you give every user a module file that points at your network share everybody can work directly off a network share. It doesn't scale well for hundreds of users but for a dozen or so it works fine: see http://techartsurvival.blogspot.com/2014/01/mayas-mildy-magical-modules.html

If you want to read up on some of the issues involved try:

  • http://tech-artists.org/forum/showthread.php?3271-Distribution-techniques-for-external-Python-tools&highlight=distribution
  • http://tech-artists.org/forum/showthread.php?3187-Wrapping-DCC-Tools-to-start-them-in-different-quot-environments-quot&highlight=distribution
  • http://techartsurvival.blogspot.com/2014/06/save-environment.html?q=environment
  • http://techartsurvival.blogspot.com/2014/07/save-environment-2-i-am-egg-man.html?q=environment


来源:https://stackoverflow.com/questions/32574334/python-copy-scripts-into-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!