PDA

View Full Version : Python documentation?



Mr. Wilde
04-07-2012, 07:41 AM
I'm trying to wrapping my head around Expressions, LScript and Python Scripting, and yeah, I'm kinda confused because there's not much reference for Python scripting in LightWave. Where can I find examples, resources, docs? Can I use Python scripts in Expressions just as I can use LScripts in Expressions? What's the advantage of using Python over LScript?

There's a video (http://www.youtube.com/watch?v=WX1k94pkzjU#t=115s) that shows an interactive chess game against a robot within Layout. Looks like Python scripting is extremely powerful and useful, yet there's virtually no proper documentation for it.

As a complete beginner to scripting in LightWave, I feel pretty much lost here.

:help:

Celshader
04-07-2012, 11:56 AM
I'm trying to wrapping my head around Expressions, LScript and Python Scripting, and yeah, I'm kinda confused because there's not much reference for Python scripting in LightWave.

I went through this EXACT same experience just in the past few days. I feel your pain. :D


Where can I find examples, resources, docs?

To learn Python itself, go to this page:

http://wiki.python.org/moin/BeginnersGuide/Programmers

I also recommend downloading one of the many free Python editors (http://wiki.python.org/moin/PythonEditors) that highlight Python syntax. I use DrPython (http://drpython.sourceforge.net/), but there's a ton of great options out there.

To specifically learn LightWave Python, I recommend the following resources:


The Python Scripting (http://forums.newtek.com/forumdisplay.php?f=308) section of the NewTek forum.
BigHache's video tutorials (http://youtu.be/9NPWg2sQ4xI) on how to write Python scripts for LightWave.
The "commands.html" section of the LightWave SDK. This will make more sense when you start using "evaluate(<command>)" for everything, because Python uses the exact same "evaluate" commands as C plug-ins for LightWave. You will find "commands.html" under "sdk\lwsdk11.0\html" in your LightWave 11 installation.
About 30-40 example Python scripts ship with LightWave 11. These are great for study. You will find these example scripts in the LW11 installation folder under "support\plugins\scripts\Python"



Can I use Python scripts in Expressions just as I can use LScripts in Expressions?

I don't know. I'm not sure about that one.


What's the advantage of using Python over LScript?

It ain't brevity. ;D From what I've seen this week, LScripts are better for casual scripting. Python scripts follow more formal rules that are similar to C plug-ins for LightWave. For example, every Python script must include a list of credits at the top, every Python script must include installation instructions at the bottom, and every Python script must be formatted as a class (http://docs.python.org/tutorial/classes.html) instead of as a simple function like LScript.

Where LightWave Python kicks LScript's *** is deeper access to LightWave, incredibly flexible built-in data types and third-party support.

Because Python scripts follow the formal standards of C plug-ins, they get to enjoy C-level access. LScript does not have access to everything that C plug-ins do in LightWave. LightWave Python does.

The native Python data types and methods are an absolute joy to use in a script. You can mix and match its lists and dictionaries to tackle any problem you might find. You can use slices (http://stackoverflow.com/questions/509211/good-primer-for-python-slice-notation) and negative indexes (http://www.careerride.com/python-negative-index.aspx) instead of having to write custom functions to do the same things. Python also ships with at least a hundred stock modules (http://docs.python.org/modindex.html) that a coder can drop into his script at any time. I can't say enough good things about the Python language itself.

The third-party support (http://www.lfd.uci.edu/~gohlke/pythonlibs/) of Python is mind-blowing. If you need to tackle a specific problem, chances are good that a common language like Python already offers a third-party solution for free. For example, there's third-party modules out there that make it easy to read and write OpenEXR data. (http://excamera.com/articles/26/doc/openexr.html) The chances of finding third-party support for a proprietary language like LScript are slim.


As a complete beginner to scripting in LightWave, I feel pretty much lost here.

I'm almost in the same boat. I do know Python scripting, and I've used it since 2006 to edit LightWave files outside of LightWave. Until this year I've only written one LScript for LightWave, and that was just a script to launch an external Python script to process the LightWave scene file.

I've always been lost when it came to programming and scripting tools meant to be used inside LightWave. I did not understand most of the SDK documentation or tutorials that I did find, and I never found the "right" documentation to help me learn how to write LScripts. I only learned last month about Mike Green's amazing LScript Index (http://www.mikegreen.name/Lscript/Lscript%20Index.html), for example.

That said, a day or two of :bangwall::bangwall::bangwall: with LightWave Python did bring results. I started with the "add_null.py" script that ships with LightWave to see how much code I could strip out before the script stopped working altogether, and I studied the error messages I got on PCore. I also used the PCore console to save out help files (http://forums.newtek.com/showthread.php?t=127194), and I asked questions on the Python Scripting (http://forums.newtek.com/forumdisplay.php?f=308) forum here on the NewTek site whenever I've been stuck. The more knowledgeable people on that forum have been generous with their time and with their experience, and they helped me a lot.

"add_null.py" made a lot more sense to me after a day of studying it. However, if you do not already know Python you might be confused by the ServerRecord dictionary (http://www.tutorialspoint.com/python/python_dictionary.htm) at the bottom, the ServerTagInfo list (http://www.tutorialspoint.com/python/python_lists.htm) of tuples (http://www.tutorialspoint.com/python/python_tuples.htm) above it, the "import (http://www.tutorialspoint.com/python/python_modules.htm) lwsdk" command at the top and the use of a class (http://www.tutorialspoint.com/python/python_classes_objects.htm) to define "add_null" itself. If possible, I recommend learning Python scripting before you learn LightWave Python scripting. As you advance in Python, consider writing tools that analyze and/or process the text of LightWave scene files. That will get you familiar with Python basics like lists, splits, find(), read(), and write(), and you can start using Python externally in a LightWave pipeline before you advance to writing internal LightWave tools.

Mr. Wilde
04-08-2012, 07:23 AM
Thank you so much. That's a great help. :thumbsup: