
The getUserProp function in MaxScript has presented errors since old versions of 3ds Max, affecting the retrieval of values stored in objects' user properties. To solve this issue, an improved version has been developed that optimizes data extraction and ensures more precise handling.
Solution to a long-standing error
The problem with the original MaxScript function lies in the fact that it does not always return the correct values when dealing with boolean, numeric, or text string data. This flaw has been present since 3ds Max 9 and has never been officially fixed. To avoid these errors, a new implementation has been designed that improves stability in data retrieval.
Main features of the improved getUserProp
- Numeric content verification in the retrieved values.
- Text format correction to avoid conversion errors.
- Compatibility with old versions of 3ds Max, including version 9.
- Optimized handling of boolean, numeric, and text string values.
Applications in automation scripts
This script is especially useful for developers working with custom tools in 3ds Max, as it allows more precise access to data stored in objects' user properties. Its application extends to:
- Retrieval of information from 3D models within the scene.
- Metadata automation to improve project organization.
- Efficient data export in custom scripts.
Optimization for advanced workflows
The implementation of this new version facilitates the obtaining of reliable data within the 3ds Max environment. Its goal is to improve consistency and stability in the use of user properties, avoiding common problems in information extraction.
Code of the improved script
-- Checks if a string contains only numbers
fn isNumeral txt = (
s= txt as string
for i = 1 to s.count do(
if(findString "0123456789" s[i]) == undefined then(
return false
)
)
return true
)
-- Enhanced getUserProp function
fn getUserProp2 obj =(
local ss = (getUserPropBuffer obj) as stringStream
if ss != undefined then(
while (eof ss) != true do(
local txt = readline ss
local arr = filterstring txt " = "
if arr[2] != undefined then(
if (arr[2] == "true") or (arr[2] == "false") or ((isNumeral arr[2]) == true ) then(
txt = arr[1] + "=" + arr[2]
execute txt
readline ss
)else(
arr[2] = "@\"" + arr[2]
append arr[2] "\""
txt = arr[1] + "=" + arr[2]
execute txt
readline ss
)
)else(
txt = arr[1] + "=" + "\"\""
execute txt
readline ss
)
)
)else(
return();
)
Code explanation
- isNumeral(txt): Checks if a string contains only numbers.
- getUserProp2(obj):
- Retrieves the values stored in the user properties.
- Verifies if they are numbers, booleans, or text strings.
- Corrects conversion errors and structures the data correctly.
- Executes the value assignment safely.
This script is an effective solution for developers working with data stored in 3ds Max, eliminating errors and improving reliability in information retrieval within their projects.