V-Ray Scene Editor/Environment Maps in Python

I’m wondering how to control the V-Ray Scene Editor using a script. I want to create a script that will load an environment map (.hdri file) and then render. Does anyone know of any scripting methods for this purpose?

This is a question for @matt_newberg he is the script chief.

… and Matt: would be good to be able to script also the light position and properties !

You should be able to modify the light position using standard Rhino scripting/commands etc.

You can modify the light properties using the Rhinoscript methods:

SetLightParameterFloat
SetLightParameterBool
SetLightParameterInteger
GetLightParameterFloat
GetLightParameterBool
GetLightParameterInteger
SetLightParameterCompTransform
GetLightParameterCompTransform
SetLightParameterString
GetLightParameterString

These were added to the latest stable release of VRayForRhino. Below is a rhinoscript method that uses these functinons to move a texture on a light.

Option Explicit
'Script copyrighted by Chaos Group USA
'Moving the uvw_transform of a light slightly without opening texture editor'

Call BumpLightTexture()
Sub BumpLightTexture()
	Dim VRay
	Dim strObject
	Dim lightTexture
	
	Dim result
	
	Dim rotx
	Dim roty
	Dim rotz
	Dim scalex
	Dim scaley
	Dim scalez
	Dim offsetx
	Dim offsety
	Dim offsetz
	Dim mirrorx
	Dim mirrory
	Dim mirrorz
	Dim uvwgen
	
	Dim dblOffset
	
	Set VRay = Rhino.GetPluginObject("V-Ray for Rhino")
	
	strObject = Rhino.GetObject("Select Light")
	
	lightTexture = "/" & strObject & "/Texture"
		
	result = VRay.GetLightParameterString(lightTexture, "uvwgen", "plugin", uvwgen)
	
	If result Then
	
		result = VRay.GetLightParameterCompTransform(uvwgen, "uvw_transform", rotx, roty, rotz, scalex, scaley, scalez, mirrorx, mirrory, mirrorz, offsetx, offsety, offsetz)
	
		If  result Then
			
			dblOffset = Rhino.GetReal("Angle to rotate the offset", 45)
			
			If Not IsNull(dblOffset) Then 
				rotx = 0
				roty = 0
				offsetx = offsetx + (dblOffset / 360.0)
				Call VRay.SetLightParameterCompTransform(uvwgen, "uvw_transform", rotx, roty, rotz, 1.0, 1.0, 1.0, mirrorx, mirrory, mirrorz, offsetx, offsety, offsetz)
			End If
		End If
		
	End If
		
End Sub

You should be able to find more settings to change by looking at the file: C:\ProgramData\ASGVIS\VfR564\current_scene_lights.xml. This file is generated/updated everytime you render with the lights in the current scene.

2 Likes

THAT’S GREAT!!!
THANKS MATT!
I’m so happy and exited, this it means I can script a Lightlister !!!
Finally! :smiley:

Wait… is this working also with Python?

Yeah, not too sure what language this is written in but doesn’t look like Python.

This is a (vb) Rhinoscript. You can test it by loading it into the script editor (EditScript). --Mitch

1 Like

The example above was written in VBScript Below is a Python version.

from Rhino import *
import rhinoscriptsyntax as rs
import System
import clr


def test():
    
    vRay = rs.GetPlugInObject("V-Ray for Rhino")

    object = rs.GetObject("Select Light")

    lightTexture = System.String("/" + str(object) + "/Texture")

    uvwgen = clr.Reference[System.Object]()
    result = vRay.GetLightParameterString( str(lightTexture), "uvwgen", "plugin", uvwgen)
    
    
    RhinoApp.WriteLine(str(uvwgen))
    
    rotx = clr.Reference[System.Object]()
    roty = clr.Reference[System.Object]()
    rotz = clr.Reference[System.Object]()
    offsetx = clr.Reference[System.Object]()
    offsety = clr.Reference[System.Object]()
    offsetz = clr.Reference[System.Object]()
    scalex = clr.Reference[System.Object]()
    scaley = clr.Reference[System.Object]()
    scalez = clr.Reference[System.Object]()
    mirrorx = clr.Reference[System.Object]()
    mirrory = clr.Reference[System.Object]()
    mirrorz = clr.Reference[System.Object]()
    
    if result:	
        vRay.GetLightParameterCompTransform(uvwgen, "uvw_transform", rotx, roty, rotz, scalex, scaley, scalez, mirrorx, mirrory, mirrorz, offsetx, offsety, offsetz)
        RhinoApp.WriteLine(str(scalex) + str(offsety) + str(offsetz))
        if result:
            
            dblOffset = rs.GetReal("Angle to rotate the offset", 45)
            
            if dblOffset != 0:
                rotx = 0
                roty = 0
                offsetx = offsetx.Value + (dblOffset / 360.0)
                
                vRay.SetLightParameterCompTransform(uvwgen, "uvw_transform", rotx, roty, rotz, 1.0, 1.0, 1.0, mirrorx, mirrory, mirrorz, offsetx, offsety, offsetz)


test()
4 Likes

Hi there,
Sorry, I know it’s an old post but I’m very interested in this script. I’ve modified it to rotate the map with the rotation parameters instead of the offset although I know that the results is basically the same but I like it better because I have some existing projects that already have dome lights with maps rotated this way.
I’m interested only in setting the parameters: horizontal rotation angle and the y mirror which I sometimes use. Also I make sure that the UV Transformations (right side values in the texture editor) are all reset to defaults (offsets 0, mirrors off).
How can I set the default value to the existing ‘rotz’ and ‘mirrorx’. Right now defaults always to the fix value of 45.
Somehow with the changes below it keeps reseting to 0. I suspect that it has something to do with the uvwgen variable but I can’t figure it out …
Can any python expert help ? My programming skills are rudimentary … :slight_smile:
@matt_newberg @clement ?
Thanks

from Rhino import *
import rhinoscriptsyntax as rs
import System
import clr
def setDomeEnvRotation():
    vRay = rs.GetPlugInObject("V-Ray for Rhino")
    light = rs.GetObject("Select a light", rs.filter.light)
    while not rs.IsPointLight(light): 
        print "Not a dome light"
        light = rs.GetObject("Select a light", rs.filter.light)
    lightTexture = System.String("/" + str(light) + "/Texture")
    uvwgen = clr.Reference[System.Object]()
  # uvw_matrix = clr.Reference[System.Object]()
    result = vRay.GetLightParameterString( str(lightTexture), "uvwgen", "plugin", uvwgen)
    #RhinoApp.WriteLine(str(uvwgen))
    rotx = clr.Reference[System.Object]()
    roty = clr.Reference[System.Object]()
    rotz = clr.Reference[System.Object]()
    offsetx = clr.Reference[System.Object]()
    offsety = clr.Reference[System.Object]()
    offsetz = clr.Reference[System.Object]()
    scalex = clr.Reference[System.Object]()
    scaley = clr.Reference[System.Object]()
    scalez = clr.Reference[System.Object]()
    mirrorx = clr.Reference[System.Object]()
    mirrory = clr.Reference[System.Object]()
    mirrorz = clr.Reference[System.Object]()
    if result:    
       vRay.GetLightParameterCompTransform(uvwgen, "uvw_matrix",rotx, roty, rotz, scalex, scaley, scalez, mirrorx, mirrory, mirrorz, offsetx, offsety, offsetz)
       rotz = rs.GetReal("Angle to set the environment", rotz.Value)
       mirrorx = rs.GetReal("v mirror? 1=on, 0=off", mirrorx.Value)
# Make sure that UV Transformation parameters are reset to defaults:
       vRay.SetLightParameterCompTransform(uvwgen, "uvw_transform", 0, 0, 0, 1.0, 1.0, 1.0, 0, 0, 0, 0, 0, 0)
# Apply horiz rotation and vertical mirror:
       vRay.SetLightParameterCompTransform(uvwgen, "uvw_matrix", 0, 0, rotz, 1.0, 1.0, 1.0, mirrorx, 0, 0, 0, 0, 0)

setDomeEnvRotation()

Looks like there is room for improvement in that script. Here give this one a try:

from Rhino import *
import rhinoscriptsyntax as rs
import System
import clr
def setDomeEnvRotation():
	vRay = rs.GetPlugInObject("V-Ray for Rhino")
	light = rs.GetObject("Select a light")
	while not rs.IsPointLight(light): 
	  print "Not a dome light"
	  light = rs.GetObject("Select a light", rs.filter.light)
	
	url = clr.Reference[System.Object]()
	result = vRay.GetLightParameterString("/" + str(light), "dome_tex", "acolor texture", url)
	
	uvwgenUrl = clr.Reference[System.Object]()
	
	result = vRay.GetLightParameterString(url, "uvwgen", "plugin", uvwgenUrl)
	
	uvw_matrix = clr.Reference[System.Object]()
	
	rotx = clr.Reference[System.Object]()
	roty = clr.Reference[System.Object]()
	rotz = clr.Reference[System.Object]()
	offsetx = clr.Reference[System.Object]()
	offsety = clr.Reference[System.Object]()
	offsetz = clr.Reference[System.Object]()
	scalex = clr.Reference[System.Object]()
	scaley = clr.Reference[System.Object]()
	scalez = clr.Reference[System.Object]()
	mirrorx = clr.Reference[System.Object]()
	mirrory = clr.Reference[System.Object]()
	mirrorz = clr.Reference[System.Object]()
	
	uvTmpX = clr.Reference[System.Object]()
	uvTmpY = clr.Reference[System.Object]()
	uvTmpZ = clr.Reference[System.Object]()
	
	uvRepeatX = clr.Reference[System.Object]()
	uvRepeatY = clr.Reference[System.Object]()
	uvRepeatZ = clr.Reference[System.Object]()
	
	uvOffsetX = clr.Reference[System.Object]()
	uvOffsetY = clr.Reference[System.Object]()
	uvOffsetZ = clr.Reference[System.Object]()
	
	uvMirrorX = clr.Reference[System.Object]()
	uvMirrorY = clr.Reference[System.Object]()
	uvMirrorZ = clr.Reference[System.Object]()
	
	if result:    
	   vRay.GetLightParameterCompTransform(uvwgenUrl, "uvw_matrix",rotx, roty, rotz, scalex, scaley, scalez, mirrorx, mirrory, mirrorz, offsetx, offsety, offsetz)
	   vRay.GetLightParameterCompTransform(uvwgenUrl, "uvw_transform",uvTmpX, uvTmpY, uvTmpZ, uvRepeatX, uvRepeatY, uvRepeatZ, uvMirrorX, uvMirrorY, uvMirrorZ, uvOffsetX, uvOffsetY, uvOffsetZ)
	   rotz = rs.GetReal("Angle to set the environment", rotz.Value)
	   mirrorx = rs.GetReal("v mirror? 1=on, 0=off", mirrorx.Value)
# Set UV Transformation parameters:
	   vRay.SetLightParameterCompTransform(uvwgenUrl, "uvw_transform", uvTmpX, uvTmpY, uvTmpZ, uvRepeatX, uvRepeatY, uvRepeatZ, uvMirrorX, uvMirrorY, uvMirrorZ, uvOffsetX, uvOffsetY, uvOffsetZ)
# Apply horiz rotation and vertical mirror:
	   vRay.SetLightParameterCompTransform(uvwgenUrl, "uvw_matrix", 0, 0, rotz, 1.0, 1.0, 1.0, mirrorx, 0, 0, 0, 0, 0)

setDomeEnvRotation()

Thanks Matt for looking into this. However, still doesn’t seem to work.
The values are entered correctly with SetLightParameterCompTransform(uvwgenUrl, “uvw_matrix”… etc)
but rotz and mirrorx keep becoming 0 on the next run instead of being extracted with GetLightParameterCompTransform from the existing values.
Perhaps uvwgenUrl and url are mixed up ? sorry I tried to figure it out but I couldn’t …

Looks like you found a bug in the Rhinoscript GetLightParameter code where it would work sometimes and not others.

I have rewritten the script to use the NETInterface DLL and this should work correctly for you.

from Rhino import *
import Rhino
import rhinoscriptsyntax as rs
import System
import clr

def LoadPlugin(path):
	if path == None:
		return False
	# MRhinoPlugIn will be null if it is a C++ plugin
	plugin = Rhino.RhinoApp.GetPlugInObject(path)
	return plugin

def setDomeEnvRotation():
	light = rs.GetObject("Select a light")
	while not rs.IsPointLight(light): 
	  print "Not a dome light"
	  light = rs.GetObject("Select a light", rs.filter.light)
	
	hPlugIn = LoadPlugin("V-Ray for Rhino")
	clr.AddReferenceToFileAndPath("C:\ProgramData\ASGVIS\VfR564\VRayForRhinoNETInterface.dll")
	import VRayForRhinoNETInterface
	from VRayForRhinoNETInterface import *
	
	lightTexture = VRayInterface.GetParameterStringBSTR(VRayInterface.DataLocations.Lights,"/" + str(light), "dome_tex", "acolor texture")
	uvwgen = VRayInterface.GetParameterStringBSTR(VRayInterface.DataLocations.Lights, str(lightTexture), "uvwgen", "plugin")
	
	rotX = clr.Reference[System.Single]()
	rotY = clr.Reference[System.Single]()
	rotZ = clr.Reference[System.Single]()
	offsetX = clr.Reference[System.Single]()
	offsetY = clr.Reference[System.Single]()
	offsetZ = clr.Reference[System.Single]()
	scaleX = clr.Reference[System.Single]()
	scaleY = clr.Reference[System.Single]()
	scaleZ = clr.Reference[System.Single]()
	mirrorX = clr.Reference[System.Single]()
	mirrorY = clr.Reference[System.Single]()
	mirrorZ = clr.Reference[System.Single]()
	
	uvTmpX = clr.Reference[System.Single]()
	uvTmpY = clr.Reference[System.Single]()
	uvTmpZ = clr.Reference[System.Single]()
	
	uvRepeatX = clr.Reference[System.Single]()
	uvRepeatY = clr.Reference[System.Single]()
	uvRepeatZ = clr.Reference[System.Single]()
	
	uvOffsetX = clr.Reference[System.Single]()
	uvOffsetY = clr.Reference[System.Single]()
	uvOffsetZ = clr.Reference[System.Single]()
	
	uvMirrorX = clr.Reference[System.Single]()
	uvMirrorY = clr.Reference[System.Single]()
	uvMirrorZ = clr.Reference[System.Single]()
	
	
	if len(uvwgen) > 1:
	   
	   result = VRayInterface.GetParameterCompTransform(VRayInterface.DataLocations.Lights, uvwgen, "uvw_matrix", "componentTransform", rotX,rotY,rotZ, scaleX,scaleY,scaleZ, mirrorX,mirrorY,mirrorZ, offsetX,offsetY,offsetZ)
	   
	   if not result:
		result = VRayInterface.GetParameterCompTransform(VRayInterface.DataLocations.Lights, uvwgen, "uvw_matrix", "componentMatrix", rotX,rotY,rotZ, scaleX,scaleY,scaleZ, mirrorX,mirrorY,mirrorZ, offsetX,offsetY,offsetZ)
	   
	   result = VRayInterface.GetParameterCompTransform(VRayInterface.DataLocations.Lights,uvwgen, "uvw_transform", "componentTransform" , uvTmpX, uvTmpY, uvTmpZ, uvRepeatX, uvRepeatY, uvRepeatZ, uvMirrorX, uvMirrorY, uvMirrorZ, uvOffsetX, uvOffsetY, uvOffsetZ)
	   
	   if not result:
		result = VRayInterface.GetParameterCompTransform(VRayInterface.DataLocations.Lights, uvwgen, "uvw_transform", "componentMatrix", uvTmpX, uvTmpY, uvTmpZ, uvRepeatX, uvRepeatY, uvRepeatZ, uvMirrorX, uvMirrorY, uvMirrorZ, uvOffsetX, uvOffsetY, uvOffsetZ)
	   
	   newRotZ = rs.GetReal("Angle to set the environment", rotZ.Value)
	   newMirrorX = rs.GetReal("v mirror? 1=on, 0=off", mirrorX.Value)
	   
# Set UV Transformation parameters:
	   VRayInterface.SetParameterCompTransform(VRayInterface.DataLocations.Lights,uvwgen, "uvw_transform","componentTransform", uvTmpX.Value, uvTmpY.Value, uvTmpZ.Value, uvRepeatX.Value, uvRepeatY.Value, uvRepeatZ.Value, uvMirrorX.Value, uvMirrorY.Value, uvMirrorZ.Value, uvOffsetX.Value, uvOffsetY.Value, uvOffsetZ.Value)
# Apply horiz rotation and vertical mirror:
	   VRayInterface.SetParameterCompTransform(VRayInterface.DataLocations.Lights,uvwgen, "uvw_matrix","componentTransform", rotX.Value, rotY.Value, newRotZ, scaleX.Value, scaleY.Value, scaleZ.Value, newMirrorX, mirrorY.Value, mirrorZ.Value, offsetX.Value, offsetY.Value, offsetZ.Value)

setDomeEnvRotation()
1 Like

Perfect ! Thanks Matt, I really appreciate your efforts.
Juan

1 Like