Rhinocommon set environment for cycles

Hello,

This is the way to set an environment from code in Rhino 5:

var environment = (RenderEnvironment)RenderContent.LoadFromFile(path);
RenderEnvironment.CurrentEnvironment = environment;

But for Rhino 6, it’ doesn’t work (only one time per execution, the first time) the class is marked as deprecated and says: Use RhinoDoc.ActiveDoc.CurrentEnvironment

But this property doesn’t have a setter:

Is that an error? or is there any other way to set an environment using RhinoCommon?

Thanks and Best Regards

If you ‘dot’ into RhinoDoc.ActiveDoc.CurrentEnvironment you’ll find ForBackground, ForLighting and ForReflectionAndRefraction. These are the three you’ll want to use. They correspond to 360° Environment, Custom Skylight Environment and Custom Reflection Environment.

http://developer.rhino3d.com/api/RhinoCommon/html/T_Rhino_Render_ICurrentEnvironment.htm

Hello Nathan,

Thanks, it works but not at all, so if I change any property (or all of them) in raytraced display mode this is the result:

        RhinoDoc.ActiveDoc.CurrentEnvironment.ForBackground = RenderContent.LoadFromFile(filePath) as RenderEnvironment;
        RhinoDoc.ActiveDoc.CurrentEnvironment.ForLighting = RenderContent.LoadFromFile(filePath) as RenderEnvironment;
        RhinoDoc.ActiveDoc.CurrentEnvironment.ForReflectionAndRefraction = RenderContent.LoadFromFile(filePath) as RenderEnvironment;

So the environment is actually changing but it’s not set as background, furthermore if you take a look to rendering tab the environment does not appear there:

I just ran my regression tester script in the (regression.py (23.0 KB)) in which I extensively use environments. Here also bundled with the necessary data files raytraced_regression_tests.7z (9.7 MB).

Although this is in Python the same holds still for other .NET languages like C#.

The interesting section is method prepare_model from lines 238 through 318, specifically 278-306. You can ensure the correct background style is set in the render settings (279-280).

With the script I generate all combinations of settings I’m interested in, and generate a huge amount of 3dm-files based on that. This is all to test environments, skylight, sun usage and groundplane. This all seems to work fine with the current public Release Candidate ( Rhino 6 SR0 2018-1-12 (Rhino WIP, 6.0.18012.13241, Git hash:master @ 00ae144185bd9d3bd331a5464cb37a9e051f7258)
)

If you have a small plug-in code that still shows the problem can you please share it so I can test?

/Nathan

P.S. Never mind the messy test code. Evolution wasn’t kind to it.

Hi Nathan,

Thanks for the code, it very close to the code I did but it’s still not setting it as background, I attach you an example plugin, sometimes it crashes, without throwing any exception and sometimes work but without applying it as background.

Using same RenderContent for all:

Making a copy:

Here the project:
SetEnvironment.7z (204.7 KB)

Thanks for your help.

I see problems already in Rendered mode, and I think that Raytraced only exacerbates the underlying problem. Reported as RH-43554.

@henrydm This is the correct code:

var env = RenderContent.LoadFromFile(path) as RenderEnvironment;

            if (env != null && RhinoDoc.ActiveDoc.RenderEnvironments.Add(env))
            {
                RhinoDoc.ActiveDoc.CurrentEnvironment.ForBackground = env;
                RhinoDoc.ActiveDoc.CurrentEnvironment.ForLighting = env;
                RhinoDoc.ActiveDoc.CurrentEnvironment.ForReflectionAndRefraction = env;
            }

The environment must be added to the document first before it is set as current. Otherwise, the current environment is pointing to an object that will - at some point - get collected.

Or, even better, use this:

RhinoDoc.ActiveDoc.CurrentEnvironment.ForAnyUsage = env;

Hello Andrew,

Ok I’ll add it to Doc RenderEnvironments first.
Thanks for the answer.

Best Regards

@nathanletwory Im poking a stick at cycles specifically, to build a little window for all its settings and a few utilities like rotating the environmet etc live. Is there a rhinocommon settings document for cycles anywhere? Im coming up empty.

Am I even close (for example I cant find the setting that controls caustics in cycles? )

Not much documented per se. One way is to check Tools > Options > Advanced, search for RhinoCycles. Another way is to check the code directly:

Note that environment control can already be done directly through Rhino GUI in the render settings panel, drilling into the environment texture.

ahh ok, sweet thanks!

And just so you know: Raytraced/Cycles doesn’t react on the EngineSettings automatically - you’ll have to toggle the display mode to say Wireframe and back to Raytraced.

This may be improved in the future (v7 or later).

Also settings may change over time as well, so don’t get too hung up on them :slight_smile: