Luxcore with Python.net

Discussion related to the LuxCore functionality, implementations and API.
Post Reply
User avatar
nigec
Posts: 141
Joined: Mon Mar 11, 2019 8:50 am

Luxcore with Python.net

Post 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;
                }
            }
        }
    }
}
User avatar
Dade
Developer
Developer
Posts: 5672
Joined: Mon Dec 04, 2017 8:36 pm
Location: Italy

Re: Luxcore with Python.net

Post by Dade »

I guess https://wiki.luxcorerender.org/LuxCore_API needs a new paragraph about C# :!:
Support LuxCoreRender project with salts and bounties
User avatar
B.Y.O.B.
Developer
Developer
Posts: 4146
Joined: Mon Dec 04, 2017 10:08 pm
Location: Germany
Contact:

Re: Luxcore with Python.net

Post by B.Y.O.B. »

Cool!
User avatar
nigec
Posts: 141
Joined: Mon Mar 11, 2019 8:50 am

Re: Luxcore with Python.net

Post 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
User avatar
nigec
Posts: 141
Joined: Mon Mar 11, 2019 8:50 am

Re: Luxcore with Python.net

Post 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
User avatar
Dade
Developer
Developer
Posts: 5672
Joined: Mon Dec 04, 2017 8:36 pm
Location: Italy

Re: Luxcore with Python.net

Post 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.
Support LuxCoreRender project with salts and bounties
User avatar
nigec
Posts: 141
Joined: Mon Mar 11, 2019 8:50 am

Re: Luxcore with Python.net

Post 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
Post Reply