3ds Max Python开发:获取选择列表及FPValues使用疑问
Hey there! I get it—FPValues can feel like a confusing black box at first, especially when you’re juggling Python development and MaxScript interactions. Let’s break this down with practical examples tied to your workflow (like rollouts calling Python functions) so you can start pulling useful data from them easily.
First: What Are FPValues?
FPValues are MaxScript’s go-to container for packaging function parameters, return values, or data collections when working with external tools like Python. Think of them as a flexible middleman that holds everything from simple numbers to references to 3ds Max objects. When your Python code talks to MaxScript (or vice versa), data often comes wrapped in an FPValue—your job is to unwrap it.
How to Extract Data from FPValues
Let’s cover common scenarios you’re likely dealing with, especially given your rollout setup.
1. Pulling a Single Basic Value
If your FPValue holds a simple type (integer, string, float), you can grab its actual content using the .value property directly. Here’s a quick Python example:
import MaxPlus def get_current_frame(): # Call MaxScript to get the current frame, wrapped in an FPValue frame_fp = MaxPlus.Core.EvalMAXScript("currentTime") # Extract the integer value from the FPValue current_frame = frame_fp.value return current_frame
2. Handling Collections (Like Selected Objects)
Since you mentioned working with selected items, this is probably your most common use case. FPValues often hold MaxScript collections—once you extract them with .value, you can treat them like regular Python lists:
import MaxPlus def list_selected_objects(): # Fetch the selected objects via MaxScript, wrapped in an FPValue selection_fp = MaxPlus.Core.EvalMAXScript("selection") # Unwrap the collection into a iterable list selected_objects = selection_fp.value if not selected_objects: print("No objects selected!") return print("Selected objects:") for obj in selected_objects: # Use MaxPlus methods to access object properties print(f"- {obj.GetName()} (Type: {obj.GetClassName()})")
3. Tying It to Your Rollout Button
You said you’ve got a rollout where a button calls a Python function—here’s a full end-to-end example to connect the dots.
First, the MaxScript rollout code:
rollout pySelectionRollout "Python Selection Tool" width:250 ( button btnListSelection "List Selected Objects" on btnListSelection pressed do ( # Call your Python function from the rollout python.execute "max_py_tools.list_selected_objects()" ) ) createDialog pySelectionRollout
Then your Python script (saved as max_py_tools.py):
import MaxPlus def list_selected_objects(): # Get selection wrapped in FPValue selection_fp = MaxPlus.Core.EvalMAXScript("selection") selected_objects = selection_fp.value if not selected_objects: print("⚠️ No objects selected in 3ds Max!") return print("\n📋 Selected Objects:") for idx, obj in enumerate(selected_objects, 1): obj_name = obj.GetName() obj_type = obj.GetClassName() print(f"{idx}. {obj_name} ({obj_type})")
Quick Troubleshooting Tips
- Always check the type of your FPValue first with
type(your_fp_value)in Python—this tells you exactly what kind of container you’re dealing with. - If you’re stuck, print the FPValue itself before accessing
.value—this helps you see how MaxScript is structuring the data. - For complex objects (like modifiers or materials), the
.valuewill give you a MaxPlus wrapper object—use MaxPlus’s built-in methods (like.GetParamValue()) to dig into its properties.
内容的提问来源于stack exchange,提问作者Ennio Sialiti




