Message 1 of 1
Executing python code inside maxscript ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi!
I found this on the net and it work in 3ds max 2020.
(
global PYTHON_RETURN
fn CollectOldFiles dir threshold_days: =
(
local pyCmd = StringStream ""
format "
import os, time
def collect_old_files(file_dir, threshold_days=None):
if threshold_days == None:
threshold_days = 0
threshold_time = ( time.time() ) - ( 60 * 60* 24 * threshold_days )
file_list = [os.path.join(file_dir, f) for f in os.listdir(file_dir) if '.ps1' in f and ( os.path.getmtime(os.path.join(file_dir, f)) <= threshold_time )]
return file_list
files = collect_old_files(r'%', %)
arr = '#({0})'.format(','.join([str('@\"'+str(n)+'\"') for n in files]))
MaxPlus.Core.EvalMAXScript('PYTHON_RETURN = {0}'.format(arr))
" ( TrimRight dir "\\" ) threshold_days to:pyCmd
python.execute ( pyCmd as string )
::PYTHON_RETURN
)
old_files = CollectOldFiles ( @"H:\E_Desktop\M1\" ) threshold_days:1
format "old_files: %\n" old_files[1]
)
I am trying to use another python code, but I got errors. Here is the code I want to use:
(
global PYTHON_RETURN
function FindWords _myStr _wordToFind: =
(
local pyCmd = StringStream ""
format "
import os
def collect_words(my_str, _wordToFind=None):
spaceStr = ' '
wordArr = []
wordIdxArr = []
strToCheck = my_str.splitlines()
for num, line in enumerate(strToCheck, 1):
if len(line) != 0:
stringArr = line.split()
if stringArr:
first_word = stringArr[0]
f1 = first_word.rstrip('\\n')
f2 = f1.rstrip('\\t')
str1 = (f2.lstrip('\\t')).lower()
if _wordToFind == str1:
wordArr.append(line)
wordIdxArr.append(num)
return wordArr
result = collect_words(%, %)
MaxPlus.Core.EvalMAXScript('PYTHON_RETURN = {0}'.format(result))
" _myStr _wordToFind to:pyCmd
python.execute ( pyCmd as string )
::PYTHON_RETURN
)
_myStr = "alpha alpha1 beta\r\n\tgama delta no name\r\n\t\talpha some\r\n\talpha1 text here"
words = FindWords _myStr _wordToFind:"alpha"
format "words: %\n" words
)The error is: "SyntaxError: invalid syntax (<input>, line 23)"
This is the code I use in python 3.1 and it works without any issues:
import os
strToCheck = (""" alpha alpha1 beta\r\n
\tgama delta no name\r\n
\t\talpha some\r\n\talpha1 text here""")
_wordToFind = "alpha"
def collect_words(my_str, _wordToFind=None):
spaceStr = ' '
wordArr = []
wordIdxArr = []
strToCheck = my_str.splitlines()
for num, line in enumerate(strToCheck, 1):
if len(line) != 0:
stringArr = line.split()
if stringArr:
first_word = stringArr[0]
f1 = first_word.rstrip('\\n')
f2 = f1.rstrip('\\t')
str1 = (f2.lstrip('\\t')).lower()
if _wordToFind == str1:
wordArr.append(line)
wordIdxArr.append(num)
return wordArr
result = collect_words(strToCheck, _wordToFind)
print (result)
https://miauu-maxscript.com/