Page 1 of 1

Luxcore with Python.net

Posted: Tue May 07, 2019 6:47 pm
by nigec
Ok I was bored/needed a break so I had a play with python.net to see if pyluxcore would work and it does :)
So here's an example for a simple console application in c#
I used VS2013; I seem to have problems with 2017 but I didn't really try to figure out why it had issues, and I used pip to get the latest python.net dll

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Python.Runtime;
using System.Threading;
namespace cslux
{
    public class Program
    {
        static dynamic session;
        static float renTime;
        static decimal renTr;
        static dynamic pyluxcore;
        static dynamic sceneProps;
        static dynamic configProps;
        static dynamic scene;
        static dynamic config;
        public static void Main(string[] args)
        {
            using (Py.GIL())
            {
                pyluxcore = Py.Import("pyluxcore");
                pyluxcore.Init();
                Console.WriteLine(pyluxcore.Version());
                sceneProps = pyluxcore.Properties();
                configProps = pyluxcore.Properties(@"scenes\cornell\cornell.cfg");
                scene = pyluxcore.Scene(configProps.Get("scene.file").GetString());
                sceneProps = scene.ToProperties();
                dynamic cameraPos = sceneProps.Get("scene.camera.lookat.orig").GetFloats();
                configProps.SetFromString("film.outputs.0.filename = image.png");
                config = pyluxcore.RenderConfig(configProps, scene);
                Console.WriteLine(cameraPos);
                session = pyluxcore.RenderSession(config);
                Console.WriteLine(@"
#############################################################
#                   Press r to render.
#############################################################");

                ConsoleKeyInfo chinput = Console.ReadKey();

                switch (chinput.Key)
                {
                    case ConsoleKey.R:
                        session.Start();
                        while (!session.HasDone())
                        {
                            session.UpdateStats();
                            var stats = session.GetStats();
                            renTime = stats.Get("stats.renderengine.time").GetFloat();
                            renTr = Convert.ToDecimal(renTime);
                            renTr = Math.Round(renTr, 3);
                            Console.WriteLine("render time = {0}", renTr);
                            Thread.Sleep(100);
                        }
                        session.Stop();
                        session.GetFilm().Save();
                        Console.WriteLine("saved");
                        break;
                    default:
                        Console.WriteLine("Unknown Command.");
                        break;
                }
            }
        }
    }
}

Re: Luxcore with Python.net

Posted: Tue May 07, 2019 10:59 pm
by Dade
I guess https://wiki.luxcorerender.org/LuxCore_API needs a new paragraph about C# :!:

Re: Luxcore with Python.net

Posted: Tue May 07, 2019 11:01 pm
by B.Y.O.B.
Cool!

Re: Luxcore with Python.net

Posted: Wed May 08, 2019 6:14 am
by nigec
Its better in a form

Code: Select all

using Python.Runtime;
        public Form1()
        {
            using (Py.GIL())

                InitializeComponent();
        }
        public static class Globals
        {
            public static dynamic luxcore = Py.Import("pyluxcore");
        }
              dynamic pyluxcore;
        public void Form1_Load(object sender, EventArgs e)
        {
            try
            {
              pyluxcore = Globals.luxcore;
              pyluxcore.Init();
              response.Text = "LuxCore Version " + pyluxcore.Version();
            }
            catch (Exception ex)
            {
               Console.WriteLine("failed to start: {0}", ex.ToString());
            }
            }
            }
            }
It seems to work well :D

Re: Luxcore with Python.net

Posted: Wed May 08, 2019 1:55 pm
by nigec
@Dade
The best way to be fair would be SWIG wrappers, but that's beyond me, the Thea guy did C#, Java and python ones but it was so hard to get the sdk and un publicised nothing was ever done with them

Re: Luxcore with Python.net

Posted: Wed May 08, 2019 3:07 pm
by Dade
nigec wrote: Wed May 08, 2019 1:55 pm @Dade
The best way to be fair would be SWIG wrappers, but that's beyond me, the Thea guy did C#, Java and python ones but it was so hard to get the sdk and un publicised nothing was ever done with them
Yes but the idea of redoing the python bindings, with all the stuff that can go wrong, makes me sick and it is can of worm I'm not going to open if there is not a veeeeeeeery convincing reason.

Re: Luxcore with Python.net

Posted: Wed May 08, 2019 4:43 pm
by nigec
I think the python.net solution works reasonably well, a render view is possible.. the bummer is the lack of IntelliSense but most things are very close to python.
I thought maybe ruby swig wrappers would be nice for Sketchup but the C api is the way to go