Rhinocommon set environment for cycles

environment

#1

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


Changing Named Rhino Environment by Grasshopper Script?
(Nathan 'jesterKing' Letwory) #3

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


#4

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:


(Nathan 'jesterKing' Letwory) #5

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.


#6

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.


(Nathan 'jesterKing' Letwory) #7

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


(Andrew le Bihan) #8

@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.


(Andrew le Bihan) #9

Or, even better, use this:

RhinoDoc.ActiveDoc.CurrentEnvironment.ForAnyUsage = env;


#10

Hello Andrew,

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

Best Regards