Playing 3dmax to learn Python-01

Versions prior to 3ds Max 2022 needed to use the Python API to implement Python programming to achieve the function of editing drawing scripts, but the good news is that starting from 3ds Max 2022, MaxPlus is no longer included as the Python API of 3ds Max. Instead, 3ds Max includes the standard version of Python 3.7, located under [3ds Max Install]\Python37. The standard precompiled extension library can be used with this version of Python.

Refer to the official website: https://help.autodesk.com/view/MAXDEV/2024/ENU/?guid=MAXDEV_Python_using_pymxs_html

1. Let 3dmax take me to write code

When we use 3dmax to write Python scripts for the first time, we will definitely encounter various difficulties. The key is that we will face the dilemma and embarrassment of not knowing who to ask. Well now, we can let 3dmax's macro recording take us to write Python code.

1. Turn on macro recording

First, we can press the shortcut key F11 to open the script listener, as shown below (remember to check the macro recording as Enable):

insert image description here

2. Supported python version

insert image description here

3. Start our python

insert image description here

4. Create a geometry

To create geometry we have to import the pymxs runtime library in the following way:

from pymxs import runtime as mxs

For example, we want to create a geometric box, in one sentence:

 pymxs.runtime.box(name="box001")

Here, the string representing the name after name can be single quotes or double quotes, which does not affect the correctness.
In addition to this you can also create sharp

 pymxs.runtime.circle(name="circle001")
pymxs.runtime.rectangle(name="rect001")

insert image description here

Import library files

Of course, if you write a pymxs.runtime every time, it will be very troublesome. We still introduce this library for later use. The code is as follows:

from pymxs import runtime as mxs
mxs.box(name="box001")

insert image description here

Attribute modification: official website attribute modification

Modify location:

https://help.autodesk.com/view/MAXDEV/2024/ENU/?guid=MAXDEV_Python_using_pymxs_pymxs_module_html
Let’s move, the following code is to move box001 to the position of 200, 200, 200:

t1=mxs.getNodeByName("box001") #通过节点名称获取box001对象
t1.position = rt.Point3(200,200,200)

Create with parameters

Or, when we create a new one, give some parameters, otherwise it will be at the origin every time.

mxs.box(pos=rt.Point3(75,75,0))

Creates a teapot with parameters, and specifies its radius and position and segments after the fact.

teapot_position=pymxs.runtime.point3(100,20,10)
my_teapot = pymxs.runtime.teapot(radius=50, pos=teapot_position, segments=2)
from pymxs import runtime as rt

t1 = rt.teapot()
t2 = rt.teapot(pos=rt.point3(20,20,0))

move by selection

pp=pymxs.runtime.selection[0]
pp.position=rt.point3(200,200,200);

change the color

sb=rt.selection[0]
sb.wirecolor=rt.color(120,0,0)

Give me a material!

For material modification, you can directly use the maxscript script
rt.execute(“$Box*.material = standard()”)
https://help.autodesk.com/view/MAXDEV/2024/ENU/?guid=MAXDEV_Python_using_pymxs_pymxs_materials_html
Of course, since we If you choose a python script, then we still hope to be able to achieve it through a Python script

m = pymxs.runtime.standardMaterial()
m.diffuse = pymxs.runtime.color(100,10,10)
m.specular = pymxs.runtime.color(100,0,0)

With the above material, we can give material to the geometry we created

m = pymxs.runtime.standardMaterial()
m.diffuse = pymxs.runtime.color(100,10,10)
m.specular = pymxs.runtime.color(100,0,0)
t = pymxs.runtime.teapot()
t.material=m

We just took the standard material as an example to show the creation and modification of materials. Of course, we know that there are three typical materials in 3dmax, Standard, Physical, and MultiMaterial, which we will explain one by one later.

add modifier

Actually, we can edit the box

Load modifier code:

my_modifier = pymxs.runtime.taper()
my_modifier.amount = 2.0
my_modifier.curve = 1.5
pymxs.runtime.addmodifier(my_teapot, my_modifier)
from pymxs import runtime as rt

my_teapot = rt.teapot()
rt.convertTo(my_teapot, rt.editable_poly)
my_mod = rt.UVWMap()
rt.addModifier(my_teapot, my_mod)

It is also possible to directly call the maxscript command. The following code is to call maxscript's box() to create a box

pymxs.runtime.execute('box()')

If open the file

pymxs.runtime.execute('max file open')

copy

 sb=rt.selection[0]
rt.copy(sb)

set coordinate system

pymxs.runtime.toolMode.coordsys(pymxs.runtime.Name("world"))

2. Switch to maxscript editor

Based on the above tests and experiments, we have a preliminary understanding of how the Python script calls the basic instructions of 3dmax. We cannot continue to test like this forever, and it is okay to play. If we want to write a plug-in or a script with official functions, we still need to write in maxscript. Although his perception and syntax prompts are not good (if we really want to write more complex scripts, we will switch to pycharm or sublime text or VSCode later).

2. The whole animation

rt = pymxs.runtime
t = rt.teapot()
with pymxs.animate(True):
    # go to frame 0
    with pymxs.attime(0):
        t.pos = rt.point3(0,0,0)
    # go to frame 100
    with pymxs.attime(100):
        t.pos = rt.point3(100,100,100)

The above animation moves the teapot we created from the origin to the position of 100,100,100, which takes 100 frames.

3. Try stretching the whole graph

rt.rectangle()
shp=rt.selection[0]
shp.position=rt.point3(100,100,0)
rt.convertTo(shp, rt.editable_poly)

Converted to an editable polygon by convertTo

4. Rotate

https://help.autodesk.com/view/MAXDEV/2024/ENU/?guid=MAXDEV_Python_using_pymxs_pymxs_objects_html

# define function for rotating only the pivot point
def RotatePivotOnly( obj, rotation):
    rotValInv=rt.inverse (rt.r2q(rotation))
    with pymxs.animate(False):
        obj.rotation*=rotValInv
        obj.objectoffsetpos*=rotValInv
        obj.objectoffsetrot*=rotValInv


b=rt.box(pos=rt.Point3(75,75,0)) # create a 25x25x25 box, vertex 1 at [62.5,62.5,0] (world)
rt.convertToMesh (b) # convert box to mesh so we can access the vertex location
DumpXForms(b) # print transforms
b.pivot= rt.Point3(50,50,0)# move pivot only to [50,50,0]
DumpXForms (b) # print transforms
rotation = rt.EulerAngles( 0, 0, 35)
RotatePivotOnly (b, rotation) # rotate pivot only 35 degrees about local Z
DumpXForms (b)# print transforms

Guess you like

Origin blog.csdn.net/haigear/article/details/130142461