Notice: Community Accounts Have Changed. What You Need To Know >>

Topic - beginners course of tutorials on scripting in Modo

Hi ,Im interested in scripting in Modo preferably in python ,as Id like to make my own tools.
Ive searched and seen some Perl tuts on the wiki but they arent that informative for me as a beginner .
Does anyone know any good in depth tutorials for scripting in Modo for an absolute beginner.
I can already script in Unityscript and C# so ideally Id like to learn scripting in python for modo.

Any help and info would be greatly appreciated ...and plus I could contribute whatever I make back to the community.

Thanks

M
I don't know sorry, but know others were asking for that too, in time - as here , for example. ;-)

--

(you surely know this place for all those as you but anyway, in case) :-)

Message edited by Dino Zanco on 4/30/2012 - 12:46 PM

thanks mio.

Shame that there isn't more stuff on this as modo would benefit greatly from it
If you can script in one or more other languages you should have no problem at all scripting in modo. The first and most important mantra to grok for modo scripting, regardless of which language you use is:

modo scripting is macros with flow control

As long as you keep that in mind you'll find modo scripting trivially simple. Depending on the language, modo adds less than a dozen methods or functions to each of the languages it supports. Almost all of these are concerned with two things, asking modo for some form of information and asking modo to do something based on that information. In all three languages that are currently supported that's achieved by taking a built in modo command or query, wrapping it as a string and passing it to modo using one of the extra language functions. That's it. No, *really*, that is it, that's where the manta comes from - if you record or write a modo macro (which is just a list of internal modo commands) then all you need to do to turn it into a script is wrap each line of the macro in quote marks and use it as an argument to one of the script functions. For example, this is a macro to create a new mesh layer and rename it to 'MyMesh':


#LXMacro#
item.create mesh
item.name {MyMesh}


The equivalent python code:


lx.eval('item.create mesh')
lx.eval('item.name {MyMesh}')


add some flow control which is just a matter of adding a smattering of *standard* python, nothing specific to modo beyond the internal commands wrapped as strings:


for x in range(10):
   lx.eval('item.create mesh')
   lx.eval('item.name {MyMesh%02d}' % x)


Getting information out of modo works in exactly the same way using exactly the same set of script functions. The main difference is that you would mostly be using the queries defined in the query services but these are just internal modo commands too and nothing specific to the scripting language you're using as 'glue'. The query services are currently best documented (as is individual scripting language support) in the Scripting PDF available from modo's help menu. Eventually all that info will be migrated to the SDK wiki but for now you'll probably need to refer to the PDF for information that isn't in the wiki. Many of modo's built in commands can also be queried, for example, to find out the name of the current selected item:


itemname = lx.eval('item.name ?')


note that the only change from the code setting the name is to replace the name string with a question mark.

One last usefull piece of information regarding python is that, when you read the scripting PDF, you'll see that there are several vesrions of the lx.eval() function. These only differ in the type of return value that they give:

lx.eval() - return value varies, depends on the return value from the query, could be
None, a singleton value, or a tuple. Be carefull using the result from an lx.eval() query as input to a for loop!!!

lx.eval1() - Forces modo to return a single value, even if the query would have returned a collection (tuple).

lx.evalN() - Forces the return value to be a tuple even if the result would have been a single string or number. Try to remember to always use this form when you intend using the return value in a loop.

lx.command() - this is a special case that allows you to specify the arguments *to the modo command* as name/value pairs.

Finally, have a look on my site (address in sig) for a whole bunch of python scripts to use as examples.

Oh yes, at the moment, modo's internal commands (not the query services) are not themselves documented outside of modo. I'm working on a set of scripts to dump the info from modo and add it directly to the wiki but it may be a few weeks before I have that completed. In the meantime you can open a Command History viewport (Layout Menu/Palettes) and click on the 'Commands' button at the top for a filterable list.

Tip: if you can't figure out what commands are required to perform a particular action, do it manually in the UI, with the mouse, like you'd normally do and cut/paste the entries from the command history into your script. Or create and save a macro and use that as a source for the correct modo commands.
Thanks so much for that primer, very informative!

-Will