PDA

View Full Version : Some basic information



KANUSO
03-30-2018, 03:58 AM
I think there are users which get in contact with python the first time (like me, I was always running away as I saw the strange and text-intensive code).
That is the reason for me to write something about stumbling blocks I had reached. I hope this will aid somebody in the first steps.

First the picky art of python with indentation:
The mix of tab and space for indentation often leads to an "Indentation Error", so try to use only tabs or only spaces for indentation.
Do not mix tab and space at least within a def-block.

Next potential hurdle is the handling of global variables.
It seems that you can read them in a def-block without usinge the "global" keyword, but if you have to change the varibles value, than you must use the "global" keyword.
Example:

...
name=""
...
def: example()
usevar=name # seems to work
name="XYZ" # seems to use a local variable named name



...
name=""
...
def: example()
global name
usevar=name # ok
name="XYZ" # this will write to the global variable named name

Next hurdle:
If you want to write your own lib, than you should use the "reload" mechanism because python remembers the imported libs. And if you do not reload, than changes you made during the developement are not recognized.
During developement of a lib you should use:
import libname; reload(libname)

This all is really very basic, but if someone had told me, it had saved me hours on searching and "try and error" the things out.

I will also write some hints on creating an UI with lwsdk (to find information on this is very hard)
I will gather information on doing basic UI stuff with the lwsdk. This will coming soon.
For example:
How to implement mouse actions
How to build some sort of custom buttons
How to simplify the things with controls
How to build some sort of "globalstore" and "globalrecall"
How to reuse contols with tab controls (because you have to erase them during the switch to an other tab) without loosing the information in them
As you will see, these are really basic things, but they can save you a lot of working time
Yes, maybe there are more elegant ways to do this, but it works.

Regards,
KANUSO

BigHache
03-30-2018, 10:16 PM
Thank you for sharing. I had run into some of the same issues but haven't looked at Python in LightWave for some time.


First the picky art of python with indentation:
The mix of tab and space for indentation often leads to an "Indentation Error", so try to use only tabs or only spaces for indentation.
Do not mix tab and space at least within a def-block.

In addition, I believe if you have written Python code in anything outside of the Python Console, and you edit that code in the Console and add tabs/spaces, they are not recognized as the same.



Next hurdle:
If you want to write your own lib, than you should use the "reload" mechanism because python remembers the imported libs. And if you do not reload, than changes you made during the developement are not recognized.
During developement of a lib you should use:
import libname; reload(libname)

Thanks, I have run into this but was not aware of "reload".

Diepgroen
06-21-2018, 01:25 PM
During developement of a lib you should use:
import libname; reload(libname)


Great info, i had this issue before and didn't know what to do other than restart LW all the time. Thanks