creating modules/packages


#1

Hi there,

i have a very long script that basically rebuilds a bad mesh with a god topology. each function deals with a different part of the body eg. torso/foot/leg/arm/head etc. However each section of the body requires alot of work. the hand function is almost 1000 lines long of code and there are alot of similar processes going on so it can be hard to work my way through the script and am constantly losing my place.

i was wondering is it good practice to seperate each of these functions off into their own python script so i have ‘hand.py’, ‘leg.py’ etc and just have a ‘main’ script which triggers everything off. This way i think the main script will be readable and i can organise things better.

is this a smart thing to do or are you only supposed to create modules for functions that need to be called frequently.

thanks alot,
Sam


#2

You answered your own question correctly. You mentioned packages in the title so I presume you have read about how to set them up with init.py in each folder etc.

Think carefully about the structure of your package and organize it in levels of functionality, so that you can import low level modules into higher level which in turn are imported into the top level. Its sounds obvious when you start out, but not as easy as it sounds, especially when you are used to a monolithic code style.

David


#3

You mention “a lot of similiar processes”. Here it is always a good idea to read carefully through the code and try to identify similiar processes. Maybe you can make a seperate function of it and instead repeating the code, you can call the function. This shortens the code a lot and you only have one place left where you have to look for problems.


#4

thanks guys,

i will have a go to split this up. I guess i was really just checking if the different functions that construct the clean topo for the body parts needs to remain in the main script.

They are repetitive in that it is just lines storing points from the original mesh and then calling a curve function which builds curves from those points over and over again. I guess i will have to carefully organise the package so it can see the functions it needs to work. here we go…ha

cheers,
Sam