Coding & Debugging of Keyshot scripts (a mostly state of the art solution)

Started by Numix, December 14, 2020, 10:49:26 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Numix

Good news everyone,

if you code for some time, you may got attached to state of the art stuff like a proper debugging experience (this is definitely more than state of the art) and a helpful code completion like Intellisense in Visual Studio or VS Code. Even more if you're not familiar with an specific API yet. I already took a look at scripting  some years back, but considered it didn't worth for my use cases than(to cumbersome development of to simple tasks). In the meantime I took some opportunities to get deeper into Python and finally getting back to the problem how to write more complex scripts for Keyshot with reasonable effort. As you might already guessed I solved some (most?) of the fundamental problems and want to share my solutions with you. So enough talking.

Autocompletion with type hints and inline documentation
First the easy one. I used reflection to generate stubs/type hints and some manual work obviously (the docs are not machine/guessing friendly in every manner, but many  ;)). These stubs should work with most IDEs which support stubs/.pyi-files for Python (tested with VS Code, PyCharm, Wing). Just install them via pip, details could be found in the corresponding Git repository . After installing the stubs you should be able to import and use lux and luxmath modules mostly like every other module, but obviously these still are only dumb interfaces with no logic implemented. The hints are likely not free from mistakes, but it's open source, so this shouldn't be an issue  ;D

This should also answer this question.

Debugging of Scripts
The likely more important, but also more complicated part, even if I thought it can't be that hard (it's a common use-case for remote debugging on a local machine, like other proprietary APIs e.g. NX Open). But in the end I got one working setup with PyCharm and a matching and "fixed/patched" version of the Pydevd debugger (and just one untested solution remaining). I documented my trials and resulting errors, the final solution and how to set it up in another Git repository. The repository itself is a "Hello World" boiler plate for setting up debugging in PyCharm. The solution works around 80% of the time (feeling, no statistics), in other cases breakpoints will be ignored and the scripts runs without attaching to the debugger (my guess is, that this is related to threading issues, whether on Pydevd side, or the way Keyshot handles its Python threads, idk).

I hope this will also help you saving some time creating scripts for Keyshot too and obiously maybe somebody here has an idea how to get the other 20% hitting.

mafrieger

wow, many thanks for sharing your effort. Will have a deep look in it.