Referring back in Python

Hi there,

I may be NOT named this thread correctly but all I mean is how it is possible to refer to the upper level of programming model in Python? To explain more the situation, should you use a program (script) with modules, the program (script) is the main program and the module(s) referred in there are at a second level of programming model. Now, from the main program, everything in the module is visible - variables, procedures, functions - and it may call them. That is quite normal. That is why modules are created - to simplify the main program and the programmer to get rid of code which is (a) repeating too much, (b) makes the main code quite long and © avoid messy structure/algorithm in the main program. This principle is applicable further in the modules which in turn may use another modules or sub-modules… NOW, what I am interested is if it is possible for the module called from a main program (script) to refer (and use) values of variables or results of procedures produced and available ONLY at the main program level? Such as values of constants, etc… without these being explicitly passed to the module by the calling script???

Regards,

I think you use an underscore ‘_’ or double-underscore ‘__’ to make methods and variables private in python…something like that, google is your friend.

So, if you have a method bar._foo() and you do a ‘from bar import *’ it won’t automatically import _foo() but you can still call it through bar._foo().

Now if you want data encapsulation and private methods like in C++, I don’t think python does that.

Ok, I’ll see what can I do…

The problem actually comes from the strange (and stupid) behaviour of Blender to not be able to refresh its data, including loaded modules without re-starting… Even re-loading does not work!!! So that, if I need to re-work a procedure placed in a module due to practical reasons explained in the first posting, I’d need to physically move it ENTIRELY into the main program (the script which is in the Blender text window). And not just that, but I’d need to move ENTIRE related content from THAT module and be careful which references I change cause when taken out of the module, references to content which is still in the module should contain module’s name and a point before variable/procedures names… And for testing purposes this is very inconvenient… I think… :eek:

Regards,

If A depends on B, and B depends on A, then A and B should probably be in the same file/module.

Otherwise, Main should import Sub, and Sub should import Main.

What good is a module if it depends on the importing script?

That is not true, at least not all the time, see this thread:

http://blenderartists.org/forum/showthread.php?t=166948

It is true this is badly documented and confusing :slight_smile: but reduce your problem to the minimum that is reproducably not reloading and maybe people on this forum can help.

If you really need to reimplement importing, you don’t have to reinvent the wheel: Python comes with an imp module that provides access to the underlying mechanism.
http://docs.python.org/library/modules.html

I am just looking for a set-up for development purposes such that being able to avoid Blender’s refreshing deficiences… I need this cause I have a script of 4,500 rows of code, about 1,500 are repeated or info code (different versions of procs, etc.) and about 2,000 rows of the remaining 3,000 are dedicated for interface(s). It is suitable then to move those interfaces code somewhere out --> in a module… So that I deal more easy with my essential code which is now about 1,000 rows, ok? :wink: So far the code is working pretty well but it covers only 1/2 or 1/3 of what I have in mind… soooo… I expect some 6,000 rows of code all-together… You can now imagine that it is NOT very good for development to constantly scroll up-down this text to find the right place for a new variable… So I have this divided by moving some code in a module. The problem is that NOW I need to modify 2-3 procedures and apparently, add some more procs to the same module… To do this, I’d need to move back the procs I am interested in BUT this will lead to moving almost 3/4 of the code due to interrelations… Plus this I’d need to re-organise the code moved back in the main script to refer to the module with other interface procs/variables. After developing required procs, I need to move this code to the module and, of course, re-work the references again… This becomes a really great transformation… Sooo… I am looking for alternative… :cool:

I though upon another type of organising the things… For example, if you have in the main script and the Module_1 the following procedures:

Main script:

import Module_1

def Test():
  Module_1.Proc_A()
  Module_1.Proc_B()

Module_1


def Proc_A():
  print "A1"

def Proc_B():
  print "B2"
 

the output will be:

A1
B2

Now I want to change my Proc_B from Module_1 to print “B3”… If I do that in the module itself, Blender wont reflect the change until I close it down and re-start it and then run the main script (which resides in the .blend file). Then and only then, I will have output like this:

A1
B3

So I was thinking if I make the Proc_B to refer to a procedure placed in another module (which will be helpful to concentrate only on THIS specific proc) and change ONLY the new module, this may work… Therefore, I modify my Module_1 like this:


import Module_2

def Proc_A():
  print "A1"

def Proc_B():
#  print "B2"
  Module_2.Proc_B()
  
 

and I dont need to change anything in the main script but I need a new module Module_2 containing:


def Proc_B():
   print "B3"

The idea NOW is to change (and compile) only Module_2 which will be relatively easy for developement purposes and after finishing this to just move the Proc_B from Module_2 to Module_1… But this tactics is also NOT working due to the same refreshing deficiences of Blender!!! :mad:

I don’t see a reload() in your code …

I don’t have the problem if I use reload:

Main script: (in TX:Main.py)


import Module1
reload(Module1)
Module1.do_print()

Module 1 (in TX:Module1.py)


def do_print():
 print "C1"

Now everytime i change something in Module1.py, the change is visible in Main.py
Note that both files are even within the .blend although that is not necessary per se.
Check out my .blend if you like: http://www.swineworld.org/blender/twomodules.blend

Oh, great!!! Thank you, varkenvarken! The “reload(Module1)” is the magic structure and the golden key I was looking for! :spin:

Now I can work directly in the module and with the reload command the LATEST changes are reflected… after compling the module (which is needed in ALL cases), of course :slight_smile: