Page 1 of 1

replace MaxwellScatter setting from one object to another one by pymaxwell

Posted: Tue Aug 10, 2021 6:22 am
by Mingfae Wong
I'm trying to replace the MaxwellScatter setting from one object to another one by pymaxwell, but it just doesn't work. Could someone help me or point me in the right direction? I'm using pymaxwell4.

from pymaxwell4 import *
def replaceScatterSetting():
filepath = "C:/Users/twong/Desktop/test.mxs"
replaceDict = {'FloorA_old': 'FloorA_new', 'FloorB_old': 'FloorB_new'}
scene = Cmaxwell(mwcallback)
scene.readMXS(filepath)
for a in replaceDict:
objOld = scene.getObject(a)
objnew = scene.getObject(replaceDict[a])
if objOld.hasGeometryModifiers()[0]: # True = have Modifiers.
modifyQty = objOld.getGeometryModifierExtensionsNumber()[0] # check how many Modifiers in the object.
for i in range(modifyQty): # check Modifiers ono by one.
extData = objOld.getGeometryModifierExtensionParamsAtIndex(i)[0]
if extData.getFloat('Density')[1]: # Ture = MaxwellScatter
print('MaxwellScatter found')
scatter_Object = extData.getString('Object')
scatter_ObjectID = extData.getByte('Inherit ObjectID')
scatter_Density = extData.getFloat('Density')
scatter_DensityMap = extData.getTextureMap('Density Map')[0]
scatter_Overlapped = extData.getByte('Remove Overlapped')
scatter_Seed = extData.getUInt('Seed')
scatter_Scale_X = extData.getFloat('Scale X')
scatter_Scale_Y = extData.getFloat('Scale Y')
scatter_Scale_Z = extData.getFloat('Scale Z')
scatter_ScaleMap = extData.getTextureMap('Scale Map')[0]
scatter_UniformScale = extData.getByte('Uniform Scale')
scatter_Scale_Xvar = extData.getFloat('Scale X Variation')
scatter_Scale_Yvar = extData.getFloat('Scale Y Variation')
scatter_Scale_Zvar = extData.getFloat('Scale Z Variation')
scatter_Rotate_X = extData.getFloat('Rotation X')
scatter_Rotate_Y = extData.getFloat('Rotation Y')
scatter_Rotate_Z = extData.getFloat('Rotation Z')
scatter_RotateMap = extData.getTextureMap('Rotation Map')[0]
scatter_Rotate_Xvar = extData.getFloat('Rotation X Variation')
scatter_Rotate_Yvar = extData.getFloat('Rotation Y Variation')
scatter_Rotate_Zvar = extData.getFloat('Rotation Z Variation')
scatter_DirectionY = extData.getFloat('Direction Type')
scatter_LOD = extData.getByte('Enable LOD')
scatter_LOD_MinDist = extData.getFloat('LOD Min Distance')
scatter_LOD_MaxDist = extData.getFloat('LOD Max Distance')
scatter_LOD_MaxDist_Density = extData.getFloat('LOD Max Distance Density')
scatter_DirectionType = extData.getFloat('Direction Type')
scatter_InitialAngle = extData.getFloat('Initial Angle')
scatter_InitialAngVar = extData.getFloat('Initial Angle Variation')
scatter_InitialAngleMap = extData.getTextureMap('Initial Angle Map')[0]
scatter_DisplayPercent = extData.getUInt('Display Percent')
scatter_DisplayMaxIns = extData.getUInt('Display Max. Instances')


print('copy setting to new object')
extManger = CextensionManager.instance()
extManger.loadAllExtensions()
scatterExt = extManger.createDefaultGeometryModifierExtension('MaxwellScatter')
scatterExtData = scatterExt.getExtensionData()
scatterExtData.setString('Object', scatter_Object[0])
scatterExtData.setByte('Inherit ObjectID', scatter_ObjectID[0])
scatterExtData.setFloat('Density', scatter_Density[0])
scatterExtData.setTextureMap('Density Map', scatter_DensityMap)
scatterExtData.setUInt('Seed', scatter_Seed[0])
scatterExtData.setByte('Remove Overlapped', scatter_Overlapped[0])
scatterExtData.setFloat('Scale X', scatter_Scale_X[0])
scatterExtData.setFloat('Scale Y', scatter_Scale_Y[0])
scatterExtData.setFloat('Scale Z', scatter_Scale_Z[0])
scatterExtData.setTextureMap('Scale Map', scatter_ScaleMap)
scatterExtData.setFloat('Scale X Variation', scatter_Scale_Xvar[0])
scatterExtData.setFloat('Scale Y Variation', scatter_Scale_Yvar[0])
scatterExtData.setFloat('Scale Z Variation', scatter_Scale_Zvar[0])
scatterExtData.setByte('Uniform Scale', scatter_UniformScale[0])
scatterExtData.setFloat('Rotation X', scatter_Rotate_X[0])
scatterExtData.setFloat('Rotation Y', scatter_Rotate_Y[0])
scatterExtData.setFloat('Rotation Z', scatter_Rotate_Z[0])
scatterExtData.setTextureMap('Rotation Map', scatter_RotateMap)
scatterExtData.setFloat('Rotation X Variation', scatter_Rotate_Xvar[0])
scatterExtData.setFloat('Rotation Y Variation', scatter_Rotate_Yvar[0])
scatterExtData.setFloat('Rotation Z Variation', scatter_Rotate_Zvar[0])
scatterExtData.setUInt('Direction Type', int(scatter_DirectionY[0]))
scatterExtData.setByte('Enable LOD', scatter_LOD[0])
scatterExtData.setFloat('LOD Min Distance', scatter_LOD_MinDist[0])
scatterExtData.setFloat('LOD Max Distance', scatter_LOD_MaxDist[0])
scatterExtData.setFloat('LOD Max Distance Density', scatter_LOD_MaxDist_Density[0])
scatterExtData.setFloat('Direction Type', scatter_DirectionType[0])
scatterExtData.setFloat('Initial Angle', scatter_InitialAngle[0])
scatterExtData.setFloat('Initial Angle Variation', scatter_InitialAngVar[0])
scatterExtData.setTextureMap('Initial Angle Map', scatter_InitialAngleMap)
scatterExtData.setUInt('Display Percent', scatter_DisplayPercent[0])
scatterExtData.setUInt('Display Max. Blades', scatter_DisplayMaxIns[0])

objnew.applyGeometryModifierExtension(scatterExtData)

scene.writeMXS('C:/Users/twong/Desktop/output.mxs')

replaceScatterSetting()

Re: replace MaxwellScatter setting from one object to another one by pymaxwell

Posted: Tue Aug 10, 2021 7:18 pm
by Harvey Fong
Hi, I tried your script in Maxwell 4.2.0.3 on Windows and it added a new MaxwellScatter modifier to FloorA_new and I could see that it had a 'Density' of 10 (same as the old) so I think your code works

BUT , pymaxwell.exe crashed constantly until I removed FloorB from your dictionary. I don't know why but I suspect the problem is pymaxwell.exe and not your code.

I would also move CextensionManager.instance() and loadAllExtensions() outside your loop since it only needs to run once.

Sorry I couldn't be more helpful.

Re: replace MaxwellScatter setting from one object to another one by pymaxwell

Posted: Wed Aug 11, 2021 4:01 am
by Mingfae Wong 20210811011621
Hi Harvey, I moved it outside of the loop as your advice, it doesn't crash anymore in my editor (PyCharm) and saved successfully, but it crashed when I open the result mxs file in Studio, I can see the materials but no objects and then stop working.



from pymaxwell4 import *

def replaceScatterSetting():
filepath = "C:/Users/twong/Desktop/test.mxs"
replaceDict = {'FloorA_old': 'FloorA_new', 'FloorB_old': 'FloorB_new'}
scene = Cmaxwell(mwcallback)
scene.readMXS(filepath)

# ---- move outside here
extManger = CextensionManager.instance()
extManger.loadAllExtensions()

for a in replaceDict:
objOld = scene.getObject(a)
.........
............

Re: replace MaxwellScatter setting from one object to another one by pymaxwell

Posted: Wed Aug 11, 2021 6:54 am
by Harvey Fong
I decided to try the script in Maxwell 5.2.1.49 and both FloorA_new and FloorB_new worked fine. The crashing is definitely a maxwell4 problem. Sorry.

Image

Re: replace MaxwellScatter setting from one object to another one by pymaxwell

Posted: Wed Aug 11, 2021 8:00 am
by Mingfae Wong 20210811011621
oh... 5.2! Is time to move forward.
Thank you so much for your test!

Re: replace MaxwellScatter setting from one object to another one by pymaxwell

Posted: Wed Aug 11, 2021 10:50 am
by Mingfae Wong 20210811011621
Sorry, another problem.
If the object has only one MaxwellScatter modifier then it works fine, but with more than one Scatter modifier in an object, it returns an error when the loop tries to 'getGeometryModifierExtensionParamsAtIndex(i)[0]' of the second Scatter modifier.
Is that Ver 4.x problem again?

Re: replace MaxwellScatter setting from one object to another one by pymaxwell

Posted: Wed Aug 11, 2021 3:09 pm
by Harvey Fong
I was able to run the script with maxwell4 if I just did FloorA and removed FloorB from the dictionary and in both 4 and 5 FloorA_new just got a second MaxwellScatter modifier added and no error. So no the loop works fine in 4/5, just 4 crashes if it loops over a second object or succeeds and makes a corrupt .mxs file that crashes on load.

Re: replace MaxwellScatter setting from one object to another one by pymaxwell

Posted: Wed Aug 11, 2021 3:15 pm
by Harvey Fong
FYI Maxwell 5 python script occasionally crashes like that too and can be frustrating because it is hard to figure out. A lot of head banging :(

Re: replace MaxwellScatter setting from one object to another one by pymaxwell

Posted: Thu Aug 12, 2021 2:59 am
by Mingfae Wong 20210811011621
Thank you Harvey!