Can I control Environment rotation with a script?

Hi @Holo,

you might look here, it works in V5.

_
c.

Hi @nathanletwory,

could you file a bug for V6 please. My script above also works in V6 but when the dialog to enter the env rotation is open while running the script, the field for the rotation does not update. If i go one step back to the material controls and then return to the texture controls, it is updated.

_
c.

Sounds like Skylight: HDRI Intensity setting discrepency?, in which case it is fixed and will be in next BETA.

@clement, note that the RenderEnvironment.CurrentEnvironment method you use in your script uses obsoleted method. You should instead use RhinoDoc.CurrentEnviroment to access either ForBackground, ForLighting or ForReflectionAndRefraction.

It is for V6, and a good start is just to know how to access either ForBackground, ForLighting or ForReflectionAndRefraction :slight_smile:

I am working on the slider UI now, and will probably hit my head against a few walls there too, but we’ll see.

Without testing, just thinking out loud (because doing some fun Sunday evening/night coding with wrapping ToonBsdf node), I’d use the scriptcontext to access current doc: scriptcontext.doc.CurrentEnvironment.ForBackground I think I actually use that in one of my regression.py scripts for testing changes in a larger data set.

Thanks, perfect!
Happy coding!

import scriptcontext
print scriptcontext.doc.CurrentEnvironment.ForBackground
print scriptcontext.doc.CurrentEnvironment.ForReflectionAndRefraction
print scriptcontext.doc.CurrentEnvironment.ForLighting

Returns this on a new document:

None
<Rhino.Render.NativeRenderEnvironment object at 0x000000000000002B [Rhino.Render.NativeRenderEnvironment]>
<Rhino.Render.NativeRenderEnvironment object at 0x000000000000002C [Rhino.Render.NativeRenderEnvironment]>

@nathanletwory, as i wrote above my script is for Rhino 5 not 6. If i try to access below in V5:

scriptcontext.doc.CurrentEnvironment.ForBackground

it returns an error:

Message: ‘RhinoDoc’ object has no attribute ‘CurrentEnvironment’

When i run my example from above in V6 i do not get any deprication warning. It may be useful für us scripters to be informed like for other outdated code snippets.

_
c.

Hi @Holo, by default a solid color background is used. If you change that to “360° Environment” you should get returned something for

scriptcontext.doc.CurrentEnvironment.ForBackground

_
c.

1 Like

Ok, so I finally took time to sit down and fiddle with this.

And this gives a 360 spin of the environment.

import scriptcontext
import System
import rhinoscriptsyntax as rs

print "ForBackground= "+str( scriptcontext.doc.CurrentEnvironment.ForBackground )
print "ForReflectionAndRefraction= "+str( scriptcontext.doc.CurrentEnvironment.ForReflectionAndRefraction )
print "ForLighting= "+str( scriptcontext.doc.CurrentEnvironment.ForLighting )

re=scriptcontext.doc.CurrentEnvironment.ForReflectionAndRefraction

rt = re.FindChild("texture")
print "RenderTexture= "+str( rt )

azimuth = System.Convert.ToDouble( rt.GetParameter("azimuth") )

print  azimuth

newAzimuth = 0

Rad=6.2831853
steps= 20

for i in range (steps):
    newAzimuth += Rad/steps
    rt.SetParameter("azimuth", newAzimuth)
    rs.Redraw()

But on my systems this laggs instead of giving a steady flow of frames. Do you know what causes that? (gtx1070, so it has enough horse power)

Looks smooth to me, especially if you use more than 20 steps with smaller increase, here a version with a full 360° using a 1° rotation over 360 steps.

Ok, thanks. On my other machine it looks smoother but the moment I move the mouse it goes to a halt, then starts again when I stop moving it.

That is most likely because the script runs on the main thread.

:expressionless: …I wish I understood what that meant and that I had the knowledge to use that info…
Anyway, it is only a step on the way to make a slider. I have the slider hooked up, but I have no idea on how to make it interactive.

Also I get the slider to return whole number (int’s) it seems.
(As a workaround I’ll rewrite it to use degrees instead of radians for now)

ETO slider - rotate environment.py (3.6 KB)

Edit: Here is the degree version:
ETO slider - rotate environment - degrees.py (4.0 KB)

EDIT 2: I just updated the script so it gives an error message if no environment is sat for reflection.
(I have no idea how to find the GLOBAL environment value, only the custom ones)

If you want to see changes while dragging the slider you’ll have to create a modeless dialog. Here some sample to get started:

1 Like

Shoot… this is getting more and more complex.
I wish it was as easy as making scripts in Unity.
There the UI is added automatically as the scripts are tied to the project.
Maybe something for Rhino V8? :slight_smile:

I have to put this project on hold for a few days, but thanks a lot for the help so far! I have gotten some progress and learned a few things too.

[quote=“Holo, post:18, topic:50674”]

Old thread, new discovery :slight_smile:

I see that rs.redraw() is much slower than using view.Redraw() for all current views, so maybe rs.redraw() can be updated?

I have for a long time wondered why that was so slow, so maybe this is it.

Here is a simple test:

import scriptcontext as sc
import rhinoscriptsyntax as rs
import time

Cview = rs.CurrentView()
views = sc.doc.Views


startTime=time.time()
for i in range(100):
    rs.RotateView(Cview,0,3.6)
    for view in views:
        view.Redraw()
    rs.Sleep(0)
print time.time()-startTime

rs.EnableRedraw(False)

startTime=time.time()
for i in range(100):
    rs.RotateView(Cview,0,3.6)
    rs.Redraw()
    rs.Sleep(0)
print time.time()-startTime

rs.EnableRedraw(True)

And the results:
0.81706237793
3.40153503418

@stevebaer maybe this is something for you?

There’s an extra wait call when using rhinoscriptsyntax. From what I can tell, you shouldn’t need a call to Sleep in the second version of your script.

Sure, that was a copy paste bug on my side, but adding 100 sleeps doesn’t take 2.8 seconds :wink:

And using Rhino.RhinoApp.Wait() is faster than rs.Sleep(0) so I upgraded to that. Please take a look at what causes the rs.Redraw() slowness.

import scriptcontext as sc
import rhinoscriptsyntax as rs
import time
import Rhino

Cview = rs.CurrentView()
views = sc.doc.Views

### view.Redraw
startTime=time.time()
for i in range(100):
    rs.RotateView(Cview,0,3.6)
    for view in views:
        view.Redraw()
    Rhino.RhinoApp.Wait()
print time.time()-startTime


### rs.Redraw
rs.EnableRedraw(False)
startTime=time.time()
for i in range(100):
    rs.RotateView(Cview,0,3.6)
    rs.Redraw()
print time.time()-startTime
rs.EnableRedraw(True)

On my laptop now and running on battery makes the difference even bigger:
0.91251373291
7.33575439453
(rtx 2070)

One can inspect the source code and see that it calls time.sleep() prior to Rhino.RhinoApp.Wait():

def Sleep(milliseconds):
    """Suspends execution of a running script for the specified interval
    Parameters:
      milliseconds (number): thousands of a second
    Returns:
      None
    Example:
      import rhinoscriptsyntax as rs
      print "This"
      rs.Sleep(2000)
      print "is"
      rs.Sleep(2000)
      print "a"
      rs.Sleep(2000)
      print "slow"
      rs.Sleep(2000)
      print "message!"
    See Also:
      
    """
    time.sleep( milliseconds / 1000.0 )
    Rhino.RhinoApp.Wait() #keep the message pump alive

That’s interesting,
I recall now that you have shown me inspect before, that I will glue to my monitor now :slight_smile:

And another strange thing is that time.sleep(0) is slower than time.sleep(0.0001)… go figure :slight_smile:

Test this. There seems to be some odd results.
IMO the last run should be 1.00299 seconds, not 1.69, so am I misunderstanding something?

import time
import Rhino

startTime = time.time()
for i in range(1000):
    time.sleep(0)
    Rhino.RhinoApp.Wait()
print time.time()-startTime

startTime = time.time()
for i in range(1000):
    time.sleep(0.0001)
    Rhino.RhinoApp.Wait()
print time.time()-startTime

startTime = time.time()
for i in range(1000):
    time.sleep(0.001)
    Rhino.RhinoApp.Wait()
print time.time()-startTime

Here is the result on my computer:

0.0109634399414
0.00299072265625
1.69309997559