## Python for 3ds max – Mesh manipulation

Software:
3ds max 2019

An example of creating a mesh ripple deformation using Python for 3ds max: 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.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

## Python for 3ds max – replace selected objects with a merged object

Software:
3ds max 2019 The following Python for 3ds max snippet replaces all selected objects with an object merged from an external 3ds max file.

Notes:

1. The ‘model_path’ string variable should be set to the path of the 3ds max file containing the replacement model.
2. This example takes into account that the merged file contains only a single object, otherwise the first of the merged objects will be used.
3. The script actually replaces the Object node of the selected objects with the Object node of the merged model (creating instances), assigning them the merged material giving them the merged object’s name appended with numbers.
```from MaxPlus import FileManager
from MaxPlus import SelectionManager

place_holders = []
for o in SelectionManager.Nodes:
place_holders.append(o)

FileManager.Merge(model_path,True,True)

model = SelectionManager.GetNode(0)

for i in range(len(place_holders)):
place_holders[i].SetObject(model.GetObject())
place_holders[i].SetMaterial(model.GetMaterial())
place_holders[i].SetName(model.Name + "_" + str(i))

model.Delete()```

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

## Python for 3ds max – list scene objects

Software:
3ds max 2019

Iterating through a list of all of an object’s hierarchy or all of the objects in the scene is a very common requirement for scripted tools and utilities in 3D animation software.
Coming from a MaxScript background, I was used to just have to write the word ‘objects‘ to reference a list of all of the objects in the scene.

Unless I’m missing something obvious,
There isn’t a shortcut like this available in the 3ds max Python API.

In order to list all the objects in the scene you need to use a recursive function that will return a list of all of a node’s children, and children’s children, etc.

In the following example, the function ‘list_children()’ will return a list all of the objects in the supplied node’s hierarchy, and when given the scene’s root node via MaxPlus.Core.GetRootNode() it will return a list of all the objects in the scene.
Wrapping ‘list_children()’ within the ‘scene_objects()’ function, provides a convenient way get a list of all the objects in the scene just by calling ‘scene_objects()’.

```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():
print o.Name```

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

## Python for 3ds max – Animated Mesh

Software:
3ds max 2019

This is an example of procedurally animating a mesh’s vertices via Python script. Notes:
1. The model has to be converted to Editable Mesh before the script is run.
* unless the scrip will be extended to do it.
2. The model must be selected for the script to work.

```import MaxPlus
import math
from MaxPlus import INode
from MaxPlus import TriObject
from MaxPlus import SelectionManager
from MaxPlus import Factory
from MaxPlus import Animation
from MaxPlus import Point3
from MaxPlus import Control

ticks_frame = 160

#Selection
sel = []
for n in SelectionManager.Nodes:
sel.append(n)
node = sel

#Setup Controllers
obj = node.GetObject()
Tri = TriObject._CastFrom(obj)
mesh = Tri.GetMesh()
num_verts = mesh.GetNumVertices()
mesh_anim = obj.GetSubAnim(0)
pnt_ctrl = Factory.CreateDefaultMasterPointController()
node.AssignController(pnt_ctrl,1)
for i in range(num_verts):
bezp3 = Factory.CreateDefaultPoint3Controller()
bezp3.SetPoint3Value(mesh.GetVertex(i))
mesh_anim.AssignController(bezp3,i)

#Animation
Animation.SetAnimateButtonState(True)
for t in range(100):
time = t * ticks_frame
Animation.SetTime(time)
for i in range(num_verts):
vert_anim = mesh_anim.GetSubAnim(i)
vert_ctrl = Control._CastFrom(vert_anim)
vert_val = mesh.GetVertex(i)
vert_val.SetZ(vert_val.GetZ() + math.sin(((Animation.GetTime()*0.5)/(ticks_frame))+i))
vert_ctrl.SetPoint3Value(vert_val)
Animation.SetAnimateButtonState(False)```

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

## Python for 3ds max – Create Objects

Software:
3ds Max 2019 ```from MaxPlus import ClassIds
from MaxPlus import Point3
import random

# Define Sphere geometry object:
sphere_obj = Factory.CreateGeomObject(ClassIds.Sphere)
sphere_obj.ParameterBlock.Segs.Value = 64

# Create a list of 10 sphere instanced objects:
spheres = []
for i in range(10):
spheres.append(Factory.CreateNode(sphere_obj))

# Move spheres to random positions
for s in spheres:
s.SetLocalPosition(Point3( random.randint(-50,50),
random.randint(-50,50),
random.randint(-50,50)))
scale = 5.0 * (random.randint(30,100)/100.0)
s.SetLocalScale(Point3(scale,scale,scale))```

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

## 3ds max – Quickly clearing materials in multiple objects

Software:
3ds max 2017

To move clear the material in multiple objects:

1. Select the objects.
2. In the MaxScript Mini Listener at the bottom left of the 3ds max window,
Type:
for o in \$ do (o.material = undefined)
And then press Enter ## 3ds max – Quickly move multiple objects pivot points to a specified location

Software:
3ds max 2017

To move the pivot point of multiple objects to 0,0,0 location:

1. Select the objects.
2. In the MaxScript Mini Listener at the bottom left of the 3ds max window,
Type:
\$.pivot = [0,0,0]
And then press Enter 