transferring variables across multiple modules

82 views
Skip to first unread message

s...@weacceptyou.com

unread,
Jun 17, 2016, 2:48:19 PM6/17/16
to Python Programming for Autodesk Maya
Hello,

i have a script which i think is split up appropriately into modules. However my main run() module takes variables set in a config() module and calls the other modules, using the configured variables as arguments.

but it is becoming a bit tedious when i need to return back each time with the modified variables, or extra variables, which need to be transferred back to the run() module and then out again to the next module.

eg

createCurves.execute(circle_counter,limb_counter,configured.curves,curr_circ_center,limb_vec,crv_pnt1,crv_pnt2)

is this normal? or is there a cleaner way to communicate the current variable states between modules.


thanks,
Sam

Justin Israel

unread,
Jun 17, 2016, 5:52:42 PM6/17/16
to python_in...@googlegroups.com
Hi Sam,

When you refer to modules, it sounds like you mean functions. A function is this:

def aFunc(arg, arg2, arg3):
    return arg + arg2 + arg3

A module is a file (usually) that can be imported into another, and can provide a namespace for its contents:

# aModule.py
def aFunc(arg, arg2, arg3):
    return arg + arg2 + arg3
# main.py
import aModule
aFunc(1,2,3)

You will need to provide a more complete example for us to get a better idea about your question. 
But yes it is a pretty common workflow for a function to accept parameters and return results. However, if you are talking about configuration variables, it can also be common to have a config class or module (a real module), which gets set up once and can be accessed globally in other parts of your code. Something like this could be one approach:

# conf.py
class Config(object):

    def __init__():
        self.number = 1
        self.name = "Name"

DefaultConfig = Config()

# main.py
import conf

def main(config):
    pass

def doWithDefaults():
    number = conf.DefaultConfig.number 
    name = conf.DefaultConfig.name 
    # ...

if __name__ == "__main__":
    # ... parse command args ...
    config = conf.Config()
    config.number = parsedNumber
    config.name = parsedName

    main(config)
Is this what you are after?

Justin


--
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/ee3e0a6f-f3dc-4cbe-a99d-4b69fa7cd722%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

s...@weacceptyou.com

unread,
Jun 18, 2016, 2:30:52 PM6/18/16
to Python Programming for Autodesk Maya
hi Justin,

sorry let me explain more clearly. I have a package which contains 3 modules (seperate python files) and a subpackage which contains all the utility modules (6 other python files, like 'subdivide_curves' and 'fire_circles' etc).

in the main package the 3 modules 'createGui' which loads my GUI, one 'configure' which sets up all the required variables for my program and another 'run' which actually gets called when the user runs the program through the GUI.

Inside 'run' it basically has all of my modules loaded in (that are being used directly in 'run' like this:

-------------------------------------------

import samDev.rigging.TOPOCOAT.configure as configure
reload(configure)
import samDev.rigging.TOPOCOAT.utility_functions.subdivide_curves as subdivide_curves
reload(subdivide_curves)
import samDev.rigging.TOPOCOAT.utility_functions.general as general
reload(general)
import samDev.rigging.TOPOCOAT.utility_functions.webbing as webbing
reload(webbing)
import samDev.rigging.TOPOCOAT.utility_functions.circle_configure as circle_config
reload(circle_config)

---------------------------------------

and then there is a main function which just calls the required modules one after the other using the variables returned from the 'configure' module.

eg. 'subdivide_curves.execute(configured_curves)'

The problem i think when a module is called which then itself creates new variables (specific to that module) and calls another module and i have to track the current variables' state at that point and feed those modified variables into the module call.

Like inside the module 'circle_configure' the script calls the module 'general' which has a function that fires rays and return collision points. but before it calls it i have to figure out all the variables it requires from the 'subdivide_curves' module and feed them into the module call:
fire_circles.execute(circle_counter,limb_counter,curr_circ_center,limb_vec,crv_pnt1,crv_pnt2)

when this was all one script everything linked up obviously. But as im splitting things up im finding it tedious to feed information from one module into the next one. And im not sure if im setting this up sensibly.

I hope this is clearer? sorry am a bit lost here

thanks alot Justin,
Sam

Justin Israel

unread,
Jun 18, 2016, 5:20:24 PM6/18/16
to Python Programming for Autodesk Maya


On Sun, 19 Jun 2016 6:30 AM <s...@weacceptyou.com> wrote:
hi Justin,

sorry let me explain more clearly. I have a package which contains 3 modules (seperate python files) and a subpackage which contains all the utility modules (6 other python files, like 'subdivide_curves' and 'fire_circles' etc).

in the main package the 3 modules 'createGui' which loads my GUI, one 'configure' which sets up all the required variables for my program and another 'run' which actually gets called when the user runs the program through the GUI.

Sorry for getting it wrong earlier. Without enough info, your example just kept looking like functions. Thanks for explaining this setup. 


Inside 'run' it basically has all of my modules loaded in (that are being used directly in 'run' like this:

-------------------------------------------

import samDev.rigging.TOPOCOAT.configure as configure
reload(configure)
import samDev.rigging.TOPOCOAT.utility_functions.subdivide_curves as subdivide_curves
reload(subdivide_curves)
import samDev.rigging.TOPOCOAT.utility_functions.general as general
reload(general)
import samDev.rigging.TOPOCOAT.utility_functions.webbing as webbing
reload(webbing)
import samDev.rigging.TOPOCOAT.utility_functions.circle_configure as circle_config
reload(circle_config)

---------------------------------------

and then there is a main function which just calls the required modules one after the other using the variables returned from the 'configure' module.

eg. 'subdivide_curves.execute(configured_curves)'

The problem i think when a module is called which then itself creates new variables (specific to that module) and calls another module and i have to track the current variables' state at that point and feed those modified variables into the module call.

This is the bit where I am not able to follow without more examples. When you say your module creates new variables when you call a function, where does it store them? And how are you "tracking" them? Can you show a small example of what is going on here? 

This sounds like the opposite situation of a thread last week where classes were being used without tracking much state. In your your case, from what you describe it sounds like you do need an instance of a class that can track state as you call methods. You shouldn't need to track new variables that are being created in another module, just to pass them to functions of that module. Kinda seems like a code smell. Does your calling module even care care about the details of these variables other than needing to pass them to the other module where they live? Is it just an implementation detail and your calling module is just acting like a proxy for passing the variables around? 

If you organise a class in the subdivide module which accepts a config once in its constructor, then you can work with an instance of that class that is configured and can track it's own implementation details between method method calls. 

I hope that seemed applicable to your situation. 



Like inside the module 'circle_configure' the script calls the module 'general' which has a function that fires rays and return collision points. but before it calls it i have to figure out all the variables it requires from the 'subdivide_curves' module and feed them into the module call:
                                fire_circles.execute(circle_counter,limb_counter,curr_circ_center,limb_vec,crv_pnt1,crv_pnt2)

when this was all one script everything linked up obviously. But as im splitting things up im finding it tedious to feed information from one module into the next one. And im not sure if im setting this up sensibly.

Maybe it all worked better as one script because you are using global variables? If that is the case, yes it would make it harder to split things up because there isn't enough encapsulation. 
Not sure until we see how those variables are created. 


I hope this is clearer? sorry am a bit lost here

thanks alot Justin,
Sam

--
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 python_inside_m...@googlegroups.com.

sam williams

unread,
Jun 19, 2016, 7:13:14 AM6/19/16
to python_in...@googlegroups.com
thanks Justin, 

so inside the configure module, which you help me setup a few weeks ago. is the class which sets up all the variables and then returns just the class instance which contains all the variables as one keyword right? like:

'attribute.limb_num'
'attribute.curve_num'
'attribute.current_surface'

so then i can just return 'attribute' to the main 'run' script. and these initialised variables get used throughout the script.

so in my 'run' module there is a call to another module 'circle_configure',  using the returned 'attribute' variables from the 'configure' module. This module which deals with creating circles for lofting geometry for the fingers of my character. Inside the 'circle_configure' module there is a bunch of code which creates new variables that are specific to the current finger that the script is working on (there is a loop which goes through each finger in the character), like 'circle_num' or 'crv_pnt1' and ''limb_vec' etc. This information is created for the current finger and then i need to send this newly created info to the module which fires rays and returns collision points on the mesh of my character.
fire_circles.execute(circle_counter,limb_counter,curr_circ_center,limb_vec,crv_pnt1,crv_pnt2)
is this the correct way to use the call to another module within a module. Just send it the required variables as arguments? and then return values from that module and store it in a new variable like:

collisionPoints=fire_circles.execute(circle_counter,limb_counter,curr_circ_center,limb_vec,crv_pnt1,crv_pnt2)
Then use the collisionPoints variable as the argument in the call to the next module in my script?
loft_surfaces(collisionPoints)?
this is what i mean by keeping track of the variables and passing them onto the next module. Maybe its fine and this is what i need to be doing, but i just wanted to be sure on it
thanks, 
Sam

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/SvBxVxv7uog/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0yx74NaTzj3imyXVn4AdXJ6S6qyU1vpx6d2XKCdWGJyw%40mail.gmail.com.

Justin Israel

unread,
Jun 19, 2016, 7:21:17 AM6/19/16
to python_in...@googlegroups.com


On Sun, 19 Jun 2016, 11:13 PM sam williams <s...@weacceptyou.com> wrote:
thanks Justin, 

so inside the configure module, which you help me setup a few weeks ago. is the class which sets up all the variables and then returns just the class instance which contains all the variables as one keyword right? like:

'attribute.limb_num'
'attribute.curve_num'
'attribute.current_surface'

so then i can just return 'attribute' to the main 'run' script. and these initialised variables get used throughout the script.

so in my 'run' module there is a call to another module 'circle_configure',  using the returned 'attribute' variables from the 'configure' module. This module which deals with creating circles for lofting geometry for the fingers of my character. Inside the 'circle_configure' module there is a bunch of code which creates new variables that are specific to the current finger that the script is working on (there is a loop which goes through each finger in the character), like 'circle_num' or 'crv_pnt1' and ''limb_vec' etc. This information is created for the current finger and then i need to send this newly created info to the module which fires rays and returns collision points on the mesh of my character.
fire_circles.execute(circle_counter,limb_counter,curr_circ_center,limb_vec,crv_pnt1,crv_pnt2)
is this the correct way to use the call to another module within a module. Just send it the required variables as arguments? and then return values from that module and store it in a new variable like:

collisionPoints=fire_circles.execute(circle_counter,limb_counter,curr_circ_center,limb_vec,crv_pnt1,crv_pnt2)
Then use the collisionPoints variable as the argument in the call to the next module in my script?
loft_surfaces(collisionPoints)?
this is what i mean by keeping track of the variables and passing them onto the next module. Maybe its fine and this is what i need to be doing, but i just wanted to be sure on it

It still sounds like what you are after is an instance of a class that contains all these variables as state. Is it possible for your functions to return a single object instead of a bunch of variables? Can you find a way to hide the details of these calls into objects that can be passed around? 

s...@weacceptyou.com

unread,
Jun 19, 2016, 5:13:41 PM6/19/16
to Python Programming for Autodesk Maya
i guess i can just go through my script and find all the created variables and put them all in the config module. So i can store all my variables under one object name. Then i would only need to reference that one object for access to any variable i needed.

is this sort of what you're saying?

thanks,
Sam

sam williams

unread,
Jun 19, 2016, 5:21:44 PM6/19/16
to python_in...@googlegroups.com
i had already done this for a few variables like you show me before. But is it weird to initialise every variable used throughout the script in a config module?



--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/SvBxVxv7uog/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.com.

Justin Israel

unread,
Jun 19, 2016, 5:28:34 PM6/19/16
to python_in...@googlegroups.com
On Mon, Jun 20, 2016 at 9:21 AM sam williams <s...@weacceptyou.com> wrote:
i had already done this for a few variables like you show me before. But is it weird to initialise every variable used throughout the script in a config module?

Yea it would be weird because they would be out of context and implementation details of the various stuff you are calling. A config module is meant to store setup details and not really a big scratch pad for the ongoing state of the entire operation. 

It seems to me that we wont be able to achieve much more without doing something like a full code review. Are you saying that you need to pass multiple state objects that you have already created, into your functions? From the naming, it seemed like one was like, a list, and a counter, etc. They didn't seem like more complex objects.

Anyways, you just have to find a way for your operations to be grouped into similar behaviour and store state as opposed to passing the entire state around as large collected of basic objects.

 



On 19 June 2016 at 22:13, <s...@weacceptyou.com> wrote:
i guess i can just go through my script and find all the created variables and put them all in the config module. So i can store all my variables under one object name. Then i would only need to reference that one object for access to any variable i needed.

is this sort of what you're saying?

thanks,
Sam

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/SvBxVxv7uog/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.com.

--
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 python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CA%2BGceHUaybqRjxdbtnj55y2gqz2s0%2BtWEPK0kYd_6PKHg-DR%3Dg%40mail.gmail.com.

s...@weacceptyou.com

unread,
Jun 19, 2016, 5:36:48 PM6/19/16
to Python Programming for Autodesk Maya
i will try and find a better way to organise things. i think i will try grouping like things together. maybe i have too many modules.

thanks for all the help with this,
Sam

kevco...@gmail.com

unread,
Jun 22, 2016, 1:18:16 AM6/22/16
to Python Programming for Autodesk Maya, s...@weacceptyou.com
Wow, you're a patient guy Justin. haha. I also think you should do a code review. It sounds like you're way off the beaten path, maybe I misunderstand though. I'm not sure what your tool is doing, but keep things simple IMO.

It sounds like just a module or two with a few functions inside it should suffice, along with a UI class where you could have a few class member variables to keep track of things.

s...@weacceptyou.com

unread,
Jun 22, 2016, 5:01:34 AM6/22/16
to Python Programming for Autodesk Maya, s...@weacceptyou.com, kevco...@gmail.com
ye thanks
Reply all
Reply to author
Forward
0 new messages