Everything related to SDK.
User avatar
By seghier
#395459
it work like that
Code: Select all
from pymaxwell import *
import os

def changeGrassMaterial(inPath,outPath,oldMat,newMat,new_Mat):
  scene = Cmaxwell(mwcallback)
  if not scene.readMXS(inPath):
    print('Error reading '+inPath)
    return 0
  
  material1 = scene.getMaterial(oldMat)
  if material1.isNull():
    print('Error: material %s not exists' % oldMat)
    return 0

  material2 = scene.createMaterial(new_Mat,True);
  if not material2.setReference(True,newMat):
    print('Error importing MXM')
    return 0

  mgr = CextensionManager.instance()
  mgr.loadAllExtensions()
  
  oit = CmaxwellObjectIterator()
  object = oit.first(scene)
  while not object.isNull():
    nModifiers,ok = object.getGeometryModifierExtensionsNumber()
    for i in range(nModifiers):
      modifierParams,ok = object.getGeometryModifierExtensionParamsAtIndex(i)
      name,ok = modifierParams.getString('EXTENSION_NAME')
      #print name
      if name == 'MaxwellGrass':
        for j in range(modifierParams.getNumItems()):
          param = modifierParams.getByIndex(j)
          #print('-------------------------')
          #print param
          if param[0] == 'Material':
            modifierParams.setString('Material',new_Mat)
            #print('New value:')
            #print modifierParams.getByIndex(j)
    object = oit.next()
  
  if not scene.writeMXS(outPath):
    print("Cannot write "+outPath)
    return 0
  
  print(outPath)
  return 1

### MAIN ###

if __name__ == "__main__":
	newMat = 'c:/input/leather.mxm'
	oldMat = 'grass'
	inPath = 'c:/input/t.mxs'
	outPath = 'c:/intest/t_edit.mxs'
	new_Mat = 'new_mat'
	changeGrassMaterial(inPath,outPath,oldMat,newMat,new_Mat)
User avatar
By Brany
#395460
Code example to change the hair material:
Code: Select all
from pymaxwell import *
import os

def changeGrassMaterial(inPath,outPath,newMaterialName):

  scene = Cmaxwell(mwcallback)
  if not scene.readMXS(inPath):
    print('Error reading '+inPath)
    return 0
 
  material = scene.getMaterial(newMaterialName)
  if material.isNull():
    print('Error: material %s not exists' % newMaterialName)
    return 0
  
  oit = CmaxwellObjectIterator()
  object = oit.first(scene)
  while not object.isNull():
    if object.isGeometryProcedural()[0]:
      proceduralParams,ok = object.getGeometryProceduralExtensionParams()
      name,ok = proceduralParams.getString('EXTENSION_NAME')
      print name
      if name == 'MaxwellHair':
        print('%s: hair material updated' % object.getName()[0])
        object.setMaterial(material)
    object = oit.next()
  
  if not scene.writeMXS(outPath):
    print("Cannot write "+outPath)
    return 0
  
  print(outPath)
  return 1

### MAIN ###

if __name__ == "__main__":
	inPath = 'G:/scenes/4.1/hair.mxs'
	outPath = 'G:/scenes/4.1/hair_edit.mxs'
	changeGrassMaterial(inPath,outPath,'red')
User avatar
By Brany
#395462
seghier wrote:
Fri Sep 15, 2017 2:44 pm
the new material is reference and have the full name of the path
i rename it but how i can assign it to the grass ?
Code: Select all
from pymaxwell import *
import os

def changeGrassMaterial(inPath,outPath,oldMat,newMat):
  scene = Cmaxwell(mwcallback)
  if not scene.readMXS(inPath):
    print('Error reading '+inPath)
    return 0
  
  material1 = scene.getMaterial(oldMat)
  if material1.isNull():
    print('Error: material %s not exists' % oldMat)
    return 0

  material2 = scene.createMaterial(newMat,True);
  if not material2.setReference(True,newMat):
    print('Error importing MXM')
    return 0

  material3 = scene.getMaterial(newMat)
  if material3.isNull():
     print('Error: material %s not exists' % newMat)
     return 0
  
  mat_name = material3.getName()
  print mat_name
  if mat_name == newMat:
      material3.setName('new_Mat')

  mgr = CextensionManager.instance()
  mgr.loadAllExtensions()
  
  oit = CmaxwellObjectIterator()
  object = oit.first(scene)
  while not object.isNull():
    nModifiers,ok = object.getGeometryModifierExtensionsNumber()
    for i in range(nModifiers):
      modifierParams,ok = object.getGeometryModifierExtensionParamsAtIndex(i)
      name,ok = modifierParams.getString('EXTENSION_NAME')
      #print name
      if name == 'MaxwellGrass':
        for j in range(modifierParams.getNumItems()):
          param = modifierParams.getByIndex(j)
          #print('-------------------------')
          #print param
          if param[0] == 'Material':
            modifierParams.setString('Material',newMat)
            #print('New value:')
            #print modifierParams.getByIndex(j)
    object = oit.next()
  
  if not scene.writeMXS(outPath):
    print("Cannot write "+outPath)
    return 0
  
  print(outPath)
  return 1

### MAIN ###

if __name__ == "__main__":
	newMat = 'c:/input/leather.mxm'
	oldMat = 'grass'
	inPath = 'c:/input/t.mxs'
	outPath = 'c:/intest/t_edit.mxs'
	changeGrassMaterial(inPath,outPath,oldMat,newMat)
You have to set the -name- of the material, the safest way to do that is asking the material itself's name:
Code: Select all
modifierParams.setString('Material',material2.getName()[0])
User avatar
By seghier
#395463
thanks you very much Brany ; work fine without the [0] ; this take the first letter
Code: Select all
from pymaxwell import *
import os

def changeGrassMaterial(inPath,outPath,oldMat,newMat):
  scene = Cmaxwell(mwcallback)
  if not scene.readMXS(inPath):
    print('Error reading '+inPath)
    return 0
  
  material1 = scene.getMaterial(oldMat)
  if material1.isNull():
    print('Error: material %s not exists' % oldMat)
    return 0

  material2 = scene.createMaterial(newMat,True);
  if not material2.setReference(True,newMat):
    print('Error importing MXM')
    return 0
  getMat = scene.getMaterial(newMat)
  new_name = getMat.getName()
  if new_name == newMat:
      material2.setName('new_Mat')


  mgr = CextensionManager.instance()
  mgr.loadAllExtensions()
  
  oit = CmaxwellObjectIterator()
  object = oit.first(scene)
  while not object.isNull():
    nModifiers,ok = object.getGeometryModifierExtensionsNumber()
    for i in range(nModifiers):
      modifierParams,ok = object.getGeometryModifierExtensionParamsAtIndex(i)
      name,ok = modifierParams.getString('EXTENSION_NAME')
      #print name
      if name == 'MaxwellGrass':
        for j in range(modifierParams.getNumItems()):
          param = modifierParams.getByIndex(j)
          #print('-------------------------')
          #print param
          if param[0] == 'Material':
            modifierParams.setString('Material',material2.getName())
            #print('New value:')
            #print modifierParams.getByIndex(j)
    object = oit.next()
  
  if not scene.writeMXS(outPath):
    print("Cannot write "+outPath)
    return 0
  
  print(outPath)
  return 1

### MAIN ###

if __name__ == "__main__":
	newMat = 'c:/input/leather.mxm'
	oldMat = 'grass'
	inPath = 'c:/input/t.mxs'
	outPath = 'c:/intest/t_edit.mxs'
	changeGrassMaterial(inPath,outPath,oldMat,newMat)
User avatar
By seghier
#395464
Brany wrote:
Fri Sep 15, 2017 2:56 pm
Code example to change the hair material:
work fine :); thank you
Code: Select all
from pymaxwell import *
import os

def changeGrassMaterial(inPath,outPath,oldMat,newMatPath):

  scene = Cmaxwell(mwcallback)
  if not scene.readMXS(inPath):
    print('Error reading '+inPath)
    return 0
 
  material = scene.getMaterial(oldMat)
  if material.isNull():
    print('Error: material %s not exists' % oldMat)
    return 0

  newMatName, descr, im, ok = CmaxwellMaterial.getMxmInfo(newMatPath)
  newMat = scene.createMaterial(newMatName)
  newMat.read(newMatPath)
  
  it = CmaxwellObjectIterator()
  object = it.first(scene)
  while not object.isNull():
    if object.isGeometryProcedural()[0]:
      proceduralParams,ok = object.getGeometryProceduralExtensionParams()
      name,ok = proceduralParams.getString('EXTENSION_NAME')
      print name
      if name == 'MaxwellHair':
        print('%s: hair material updated' % object.getName()[0])
        object.setMaterial(newMat)
    object = it.next()
  
  if not scene.writeMXS(outPath):
    print("Cannot write "+outPath)
    return 0
  
  print(outPath)
  return 1

### MAIN ###

if __name__ == "__main__":
	inPath = 'c:/input/hair.mxs'
	outPath = 'c:/intest/t_edit.mxs'
	oldMat = 'hair'
	newMatPath = 'c:/input/leather.mxm'
  #oldMat = 'grass'
  #newMat = 'c:/input/leather.mxm'
	changeGrassMaterial(inPath,outPath,oldMat,newMatPath)
User avatar
By seghier
#395465
and this for grass also work fine
Code: Select all
from pymaxwell import *
import os

def changeGrassMaterial(inPath,outPath,oldMat,oldMatBf,backFace,newMatPath):
  scene = Cmaxwell(mwcallback)
  if not scene.readMXS(inPath):
    print('Error reading '+inPath)
    return 0
  
  material = scene.getMaterial(oldMat)
  if material.isNull():
    print('Error: material %s not exists' % oldMat)
    return 0

  materialBf = scene.getMaterial(oldMat)
  if material.isNull():
    print('Error: material %s not exists' % oldMatBf)
    return 0

  newMatName, descr, im, ok = CmaxwellMaterial.getMxmInfo(newMatPath)
  newMat = scene.createMaterial(newMatName)
  newMat.read(newMatPath)

  newMatBack, descr, im, ok = CmaxwellMaterial.getMxmInfo(backFace)
  newMatBf = scene.createMaterial(newMatBack)
  newMatBf.read(newMatPath)

  mgr = CextensionManager.instance()
  mgr.loadAllExtensions()
  
  oit = CmaxwellObjectIterator()
  object = oit.first(scene)
  while not object.isNull():
    nModifiers,ok = object.getGeometryModifierExtensionsNumber()
    for i in range(nModifiers):
      modifierParams,ok = object.getGeometryModifierExtensionParamsAtIndex(i)
      name,ok = modifierParams.getString('EXTENSION_NAME')
      if name == 'MaxwellGrass':
        for j in range(modifierParams.getNumItems()):
          param = modifierParams.getByIndex(j)
          #print('-------------------------')
          #print param
          if param[0] == 'Material':
            modifierParams.setString('Material',newMat.getName())

          elif param[0] == 'Double Sided Material':
            modifierParams.setString('Double Sided Material',newMatBf.getName())

            #print('New value:')
            #print modifierParams.getByIndex(j)
    object = oit.next()
  
  if not scene.writeMXS(outPath):
    print("Cannot write "+outPath)
    return 0
  
  print(outPath)
  return 1


if __name__ == "__main__":
	newMatPath = 'c:/input/carpaint_black.mxm'
	backFace = 'c:/input/leather.mxm'
	oldMat = 'grass'
	oldMatBf = 'scatter'
	inPath = 'c:/input/t.mxs'
	outPath = 'c:/intest/t_edit.mxs'
	changeGrassMaterial(inPath,outPath,oldMat,oldMatBf,backFace,newMatPath)
User avatar
By Brany
#395504
That's was you first question of the thread, and this was my answed ;)
Code: Select all
from pymaxwell import *
import os

def changeGrassMaterial(inPath,outPath,newMaterialName):
  scene = Cmaxwell(mwcallback)
  if not scene.readMXS(inPath):
    print('Error reading '+inPath)
    return 0
  
  material = scene.getMaterial(newMaterialName)
  if material.isNull():
    print('Error: material %s not exists' % newMaterialName)
    return 0
  
  mgr = CextensionManager.instance()
  mgr.loadAllExtensions()
  
  oit = CmaxwellObjectIterator()
  object = oit.first(scene)
  while not object.isNull():
    nModifiers,ok = object.getGeometryModifierExtensionsNumber()
    for i in range(nModifiers):
      modifierParams,ok = object.getGeometryModifierExtensionParamsAtIndex(i)
      name,ok = modifierParams.getString('EXTENSION_NAME')
      print name
      if name == 'MaxwellGrass':
        for j in range(modifierParams.getNumItems()):
          param = modifierParams.getByIndex(j)
          print('-------------------------')
          print param
          if param[0] == 'Material':
            modifierParams.setString('Material',newMaterialName)
            print('New value:')
            print modifierParams.getByIndex(j)
    object = oit.next()
  
  if not scene.writeMXS(outPath):
    print("Cannot write "+outPath)
    return 0
  
  print(outPath)
  return 1

### MAIN ###

if __name__ == "__main__":
	inPath = 'G:/scenes/4.1/grass.mxs'
	outPath = 'G:/scenes/4.1/grass_edit.mxs'
	changeGrassMaterial(inPath,outPath,'green')

I think that most features that are needed on a re[…]

Maxwell keeps returning an error when I try to ren[…]

Hello good day! I wanted to know if you can help[…]

I chose a new default folder location for the Maxw[…]