Using OSL Shaders in V-Ray for 3ds max

Software:
3ds max 2020 | V-Ray next

V-Ray for 3ds max supports compiling and rendering OSL shaders,
And also offers some handy shaders for download on the V-Ray documentation website.
Note:
OSL shaders are supported only in V-Ray Advanced and not in V-Ray GPU.

 

To load an external OSL shader:

  1. For a material (color closure) shader, create a:
    Materials > V-Ray > VRayOSLMtl
    For a texture shader create a:
    Maps > V-Ray > VRayOSLTex
  2. In the VRayOSLMtl or VRayOSLTex‘s General properties,
    Click the Shader File slot-button to locate and load the *.osl file.
  3. Provided that the shader has loaded and compiled successfully,
    You will now be able to set it’s custom parameters in its Parameters section:
    Annotation 2020-05-25 200439
  4. If compile errors will be found you’l be able to read the error messages in the V-Ray messages window:
    Annotation 2020-05-25 200625

 

To write an OSL shader:

  1. To write a material shader (color closure) create a:
    Materials > V-Ray > VRayOSLMtl
    To write a texture shader create a:
    Maps > V-Ray > VRayOSLTex
  2. Expend the Quick Shader section of the node’s properties,
    And check the Enable option.
  3. Write you’r OSL code, and press Compile.
    Annotation 2020-05-25 204835
  4. Provided that the shader compiled successfully,
    You will now be able to set it’s custom parameters in its Parameters section:
    Annotation 2020-05-25 205225
  5. If compile errors will be found you’l be able to read the error messages in the V-Ray messages window.

 

Related:

  1. What are OSL shaders?
  2. Using OSL shaders in Arnold for maya 
  3. Using OSL shaders in Blender & Cycles
  4. Writing a basic OSL color shader

3ds max & V-Ray to UE4 – Datasmith workflow basics and tips

Software:
3ds max 2020 | V-Ray Next | Unreal Engine 4.25

This post details basic steps and tips for exporting models from 3ds max & V-Ray to Unreal Engine using the Datasmith plugin.
The Datasmith plugin from Epic Games is revolutionary in the relatively painless workflow it enables for exporting 3ds max & V-Ray architectural scenes into Unreal Engine.
Bear in mind however, that Datasmith‘s streamlined workflow can’t always free us from the need to meticulously prepare models as game assets by the book (UV unwrapping, texture baking, mesh and material unifying etc.) (especially if we need very high game performance).
That being said, the Datasmith plugin has definitely revolutionized the process of importing assets into Unreal, making it mush more convenient and accessible.

 

Preparation:
Download and Install the Datasmith exporter plugin compatible with your modeling software and Unreal Engine version:
https://www.unrealengine.com/en-US/datasmith/plugins

 

In 3ds max & V-Ray:

  1. Make sure all materials are VRayMtl type (these get interpreted relatively accurately by Datasmith)
  2. Make sure all material textures are properly located so the Datasmith exporter ill be able to export them properly.
  3. In Rendering > Exposure Control:
    Make sure Exposure control is disabled.
    Explanation:
    If the Exposure Control will be active it will be exported to the Datasmith file, and when imported to Your Unreal Level/Map a “Global_Exposure” actor will be created with the same exposure settings.
    Sounds good, right? So what’s the problem?
    The problem with this is that these exposure setting will usually be compatible with photo-metric light sources like a VRaySun for example, but when imported to Unreal, the VRaySun does not keep its photo-metric intensity. (in my tests it got 10lx intensity on import). the result is that the imported exposure settings cause the level to be displayed completely dark.
    Of-course you can simply delete the “Global_Exposure” actor after import, but honestly, I always forget its there, and start looking for a reason why would everything be black for no apparent reason…
    * If your familiar with photo-metric units, you can set the VRaySun to its correct intensity of about 100000lx, and also adjust other light sources intensity to be compatible with the exposure setting.
  4. Annotation 2020-05-12 192439
  5. Select all of the models objects intended for export,
    And File > Export > Export Selected:
    * If you choose File > Export > Export you’l still have an option to export only selected objects..
    Annotation 2020-05-12 192506
  6. In the File Export window,
    Select the export location, name the exported file,
    And in the File type drop-down select Unreal Datasmith:
    Annotation 2020-05-12 192550
  7. In the Datasmith Export Options dialog,
    Set export options, and click OK.
    * Here you select whether to export only selected object or all objects (again)
    Annotation 2020-05-12 192654
  8. Depending on the way you prepared your model,
    You may get warning messages after the export has finished:
    Explanation:
    Traditionally, models intended for use in a game engine should be very carefully prepared with completely unwrapped texture UV coordinates and no overlapping or redundant geometry UV space.
    Data-smith allows for a significantly forgiving and streamlined (and friendly) workflow but still warns for problem it locates.
    In many cases these warnings will not have an actual effect (especially if Lightmap UV’s are generated by Unreal on import), but take into account that if you do encounter material/lighting issues down the road, these warnings may be related.
    Annotation 2020-05-12 192730
  9. Note that the Datasmith exporter created both a Datasmith (*.udatasmith) file, and a corresponding folder containing assets.
    It’s important to keep both these items in their relative locations:
    Annotation 2020-05-12 204541

 

In Unreal Editor:

  1. Go to Edit > Plugins to open the Plugins Manager:
    Annotation 2020-05-12 192802
  2. In the Plugins Manager search field, type “Datasmith” to find the Datasmith Importer plugin in the list, and make sure Enabled checked for it.
    * Depending on the project template you started with, it may already be enabled.
    * If the plugin wasn’t enabled, the Unreal Editor will prompt you to restart it.
    Annotation 2020-05-12 192901
  3. In the Unreal project Content, create a folder to which the now assets will be imported:
    * You can also do this later in the import stage
    Annotation 2020-05-12 193030
  4. In the main toolbar, Click the Datasmith button to import your model:
    Annotation 2020-05-12 193043
  5. Locate the the *.udatasmith file you exported earlier, double click it or select it and press Open:
    Annotation 2020-05-12 193129
  6. In the Choose Location… dialog that opens,
    Select the folder to which you want to import the assets:
    * If you didn’t create a folder prior to this stage you can right click and create one now.
    Annotation 2020-05-12 193301
  7. The Datasmith Import Options dialog lets you set import options:
    * This can be a good time to raise the Lightmap resolution for the models if needed.
    Annotation 2020-05-12 193326
  8. Wait for the new imported shaders (materials) to compile..
    Annotation 2020-05-12 193408
  9. The new assets will automatically be placed into the active Map\Level in the Editor.
    All of the imported actors will be automatically parented to an empty actor names the same as the imported Datasmith file.
    In the Outliner window, locate the imported parent actor, and transform it in-order to transform all of the imported assets together:
    * If your map’s display turns completely dark or otherwise weird on import, locate the “Global_Exposure” actor that was imported and delete (you can of-course set new exposure setting or adjust the light settings to be compatible)
    Annotation 2020-05-12 193517

 

 

Related:

  1. Preparing an FPS project for archviz
  2. Unreal – Architectural glass material
  3. Unreal – Camera animation
  4. UE4 – Archviz Light calculaion tips

Python for 3ds max – Select objects of type

Software:
3ds max 2020

Continuing this example,
If you need to select objects (nodes) of a certain type i.e. lights, cameras etc.
You can use the INode class’s GetObject() method to get a reference to the node’s object, or “modified object” in 3ds max terms, and use the object’s GetSuperClassID() method to get the integer id representing the object’s superclass.

In addition, the MaxPlus SuperClassIds class contains convenient constants that can be used to avoid having to check and remember the superclasses numeric ids.
See reference here:
https://help.autodesk.com/view/3DSMAX/2017/ENU/?guid=__py_ref_class_max_plus_1_1_super_class_ids_html

An example of a script that selects all light objects in the scene:

from MaxPlus import SuperClassIds
from MaxPlus import SelectionManager

def scene_objects():
   def list_children(node):
      list = []
      for c in node.Children:
         list.append(c)
         list = list + list_children(c)
      return list
   return list_children(MaxPlus.Core.GetRootNode())

for o in scene_objects():
   obj = o.GetObject()
   if obj.GetSuperClassID() == SuperClassIds.Light:
      SelectionManager.SelectNode(o, False)

* note that when copying and pasting a script from this example, the indentation may not be pasted correctly.

tyFlow for 3ds max – Basic Tutorial

Software:
3ds max 2019 | tyFlow

rocket_balls_new.gif

tyFlow is a new advanced event driven particle / dynamics system plugin for 3ds max that can somewhat be described as the “next generation of Particle Flow*“.
An ‘event driven particle system‘ is a particle system in which the behavior of the particles is defined in ‘events‘. each event contains a collection of ‘operators‘ that control the different aspects of the particle behavior, like their shape, their speed, direction etc. special ‘test operators‘ are used to check if the particles should switch to a different event and so change their behavior.
Setting up an event driven particle system in 3ds max is done through a node based visual interface, in which the nodes in the flow graph are the particle events, each event is a collection of operators and their parameters.

This tutorial demonstrates some fundamental concepts of event driven particles and tyFlow:

  1. Using Property Test operators to switch between different motions and shapes.
  2. Using the PhysX Shape and PhysX Switch operators to switch between physical and non-physical particle behavior.
  3. Spawning new particles and deleting them.

> Note that while understanding the logic of this example is relatively easy, tweaking all the parameters to get it right takes a lot of tests and fine tuning.
Download the example file and playing with the system parameters to get to know how the different operators affect the particles.

Download the tutorial example 3ds max file here

Untitled-1.jpg

Explanation of the flow graph:

> To create a tyFlow particle system:
In Command Panel > Create > Standard Primitives,
Click tyFlow, and then click and drag in the viewport to create a tyFlow object.
When the tyFlow object is selected, in the Modify panel press the Open Editor button to open the tyFlow editor. Drag operators from the library below to the work space in order to create a new event containing them, or drag them into existing events.

> Note that almost all of the system’s events contain a Display operator set to Geometry mode so that the particle’s actual animated shapes will be displayed in the viewport, and also a Mesh operator needed for particles to be exported to a render engine for final rendering.

  1. Particles are initially created in event: ‘Birth’ using a Birth Objects Operator, and are immediately sent to the next event: ‘Physical Falling’ by the Send Out operator.
    > The Birth Objects operator is used to initialize particles directly from scene objects.
    Untitled-4
    Initial setup of the spheres as sources of particles for the Birth Objects operator:Untitled-3
  2. The event titled ‘Physical Falling’ is the only event in the system in which the particles behave as PhysX objects who’s motion is governed by physical simulation i.e they fall and collide with the slide model (named Box001).
    The PhysX Shape operator turns the particles into physical simulation objects, and the PhysX Collision operator adds the slide model as a collision object to the simulation.
    A Property Test operator checks if the falling balls fell below -35 in the Z axis, and if so sends them to the event titled ‘Rocket Upward’.
    > Note that in the PhysX Collision operator, the Hull Type of the slide model is set to Mesh rather than Convex to allow the sphere particles to slide inside the slide (a convex hull type would behaves as if it’s filled making the balls fall to the sides)Untitled-5.jpg
    > Note that in the tyFlow PhysX settings, the default ground collider has been disabled to allow the balls to fall freely below the 0.0 Z axis position.Untitled-6
    > The (standard) Shape Operator and Speed operators are in this event in order to reset the shape and speed of particles in case they entered this event after already having different shapes ans speeds.
  3. The event titled ‘Rocket Upwards’ uses a PhysX Switch operator to deactivate the particles participation in physical simulation, assigns them a new (rocket shape),
    A Force operator referring to a tyWind object causes the particles to accelerate upwards, and a Spawn operator to have each original particle spawn new particles as the rocket trail.
    A Property Test operator checks if the particles have reach a height of 200 and if so sends them to the event titled ‘Chopper back to start’.
    > Note that the Slow, and Rotation operators are in this event to force the particles back to default orientation so the rockets will be aimed upwards.
    > The Spawn operator creates new particles and at the original particle location and sends them to the event titled ‘Rocket flare’.Untitled-8.jpg
    rocket_ball_switch
  4. The event titled ‘Chopper back to start’ assigns the particles a new ‘little chopper’ animated shape, and uses a Force operator to make them affected by the tyWind object pointing back to the start.
    > Note that the Shape operator has the Animated Geometry option checked, and set to sync by particle age. the animation speed is played back 3 times faster than the original animation.
    > A Property Test operator checks if the particles Y position higher than -150 and if so sends the particles to the event titled ‘find drop location’.untitled-9.jpg
  5. The event titled ‘find drop location’ uses a Find Target test operator to make the particles move to the Point Helper object located above the slide and when they have reached it send them back to the event titled ‘Physical falling’ where they start their journey all over again.
    > Note that the Slow operator slows the particles velocity that was inherited for the previous event so they wont overshoot and ‘fly’ further beyond the point helper.Untitled-10.jpg
  6. The event titled ‘Rocket flare’ defines the shape, speed and direction of the spawned rocket trail particles and uses a Time Test operator to check if the trail particles have existed more than 3 frames and if so send them to the event titles ‘Delete Flare’
    Untitled-11
  7. The event titles ‘Delete Flare’ uses a Delete operator to delete the trail particles.

System setup with the tyWind objects responsible for moving the particles in the non-physical events of the system, and a Point helper object that serves as a ‘find target’ for the particles to get back to the initial location above the slide:

Untitled-2

Particle shapes for the ‘Rocket Upward’, ‘Chopper back to start’ and ‘find drop location’ events.
Note that the shape pivots are consistently at the center of the ball for the shape switching to work well, and that the ‘chopper’ animation is done at the mesh level using Poly Select and XForm:

rocket_balls_shapes

* Particle Flow is a powerful event driven particle system that was originally developed by ThinkBox software, ans later bacame 3ds max’s built in particle system.

Python for 3ds max – Mesh manipulation

Software:
3ds max 2019

An example of creating a mesh ripple deformation using Python for 3ds max:

mesh_manipulation.gif

Script steps:

  1. Define the effect intensity and a helper point object that will serve as the effect center.
  2. Collapse the object to an Editable-Mesh so its vertices will be accessible by Python.
    Note that the Node‘s Object has to be cast as a TriObject, to access the object’s Mesh data.
  3.  Loop through the Mesh’s vertices, get their world position, and set a new Z position as a sine function of the distance from the effect center.
import math
from MaxPlus import Factory
from MaxPlus import ClassIds
from MaxPlus import INode
from MaxPlus import TriObject
from MaxPlus import Matrix3
from MaxPlus import Point3

# Intensity:
effecr_mult = 1.0

# Effect center:
effector = INode.GetINodeByName('Point001')
effect_pos = effector.GetWorldPosition()

# Prepare object and eccess it's mesh data:
node = INode.GetINodeByName('Teapot001')
new_edit_mesh_mod = Factory.CreateObjectModifier(ClassIds.Edit_Mesh)
node.AddModifier(new_edit_mesh_mod)
node.Collapse(True)
node_tm = node.GetWorldTM()
node_pos = node.GetWorldPosition()
obj = node.GetObject()
triobj = TriObject._CastFrom(obj)
mesh = triobj.GetMesh()

# Process the object's vertices:
for i in range(mesh.GetNumVertices()):
     # Get vertex in world space
     vert_pos = mesh.GetVertex(i)
     vert_world_pos = node_tm.VectorTransform(vert_pos)
     vert_world_pos = vert_world_pos + node_pos
     # Get vertex distance from effect center:
     diff_vec = vert_world_pos - effect_pos 
     diff_vec.Z = 0
     dist = diff_vec.GetLength()
     # Set new vertex position:
     mesh.SetVert(i,vert_pos.X,vert_pos.Y,vert_pos.Z + math.sin(dist)*effecr_mult)

* note that when copying and pasting a script from this example, the indentation may not be pasted correctly.

Related Post:
Python for 3ds max – Animated Mesh

3ds max – Use the Particle-Flow Lock/Bond test to have particles move on a surface in

Software:
3ds max 2019

The Lock/Bond Particle Flow test can be used to have particle movement restricted to a surface.

lock_bond.gif

This example shows a simple setup in which the particle teapots have a Speed operator set to Random 3D mode that causes them to move in random directions,
While at the same time the Lock/Bond test forces them to ‘stick’ to the surface:

Untitled-1