User avatar
By seghier
#399670
Hello, i remember that the script worked fine years ago when i made it for maxwell v3 but i don't know why it didn't work now,
the new material added to the new scene but for grass nothing change.
What i miss here?
Code: Select all
    def MXMchangeMaterialFromMXM(self):
        oldMatName = str(self.chosen_material.text())
        newMatFolder = str(self.input_load_mxm.text())
        matName = str(self.input_mxm.currentText())
        newMatPath = str(newMatFolder + '/' + str(matName))
        mxsList = [self.input_mxs_scene.text()]
        outPath = self.output_save_scene.text()
        scene = Cmaxwell(mwcallback)
        if not len(self.input_mxs_scene.text()) == 0:
            if len(outPath) > 0:
                    if not len(oldMatName) == 0:
                        if self.input_mxm.count() > 0:
                            for mxs in mxsList:
                                scene.readMXS(str(mxs))
                                newMatName, descr, im, ok = CmaxwellMaterial.getMxmInfo(newMatPath)
                                newMat = scene.createMaterial(newMatName)
                                newMat.read(newMatPath)

                                it = CmaxwellObjectIterator()
                                obj = it.first(scene)

                                while not obj.isNull():
                                    objMat1 = obj.getMaterial()[0]
                                    objMat2 = obj.getBackfaceMaterial()[0]
                                    if not objMat1.isNull():
                                        if objMat1.getName() == oldMatName:
                                            obj.setMaterial(newMat)
                                    if not objMat2.isNull():
                                        if objMat2.getName() == oldMatName:
                                            obj.setBackfaceMaterial(newMat)


                                    ismesh, ok = obj.isMesh()
                                    if ismesh:
                                        itGroup = CmaxwellObjectTrianglesGroupIterator()
                                        group = itGroup.first(obj)

                                        while not group.isNull():

                                            triangles = group.getTriangles()
                                            groupMat = obj.getTriangleMaterial(triangles[0])[0]

                                            if groupMat.getName() == oldMatName:
                                                group.setMaterial(newMat)

                                            group = itGroup.next()

                                    if obj.isGeometryProcedural()[0]:  # hair

                                        proceduralParams, ok = obj.getGeometryProceduralExtensionParams()
                                        name, ok = proceduralParams.getString('EXTENSION_NAME')
                                        if objMat1.getName() == oldMatName:
                                            if name == 'MaxwellHair':
                                                obj.setMaterial(newMat)

                                    nModifiers, ok = obj.getGeometryModifierExtensionsNumber()  # grass
                                    for i in range(nModifiers):
                                        modifierParams, ok = obj.getGeometryModifierExtensionParamsAtIndex(i)
                                        name, ok = modifierParams.getString('EXTENSION_NAME')
                                        if name == 'MaxwellGrass':
                                            for j in range(modifierParams.getNumItems()):
                                                param = modifierParams.getByIndex(j)
                                                par1 = modifierParams.getString('Material')
                                                par2 = modifierParams.getString('Double Sided Material')
                                                if par1[0] == oldMatName: 
                                                    if param[0] == 'Material':
                                                        modifierParams.setString('Material', newMat.getName())
                                                elif par2[0] == oldMatName:
                                                    if param[0] == 'Double Sided Material':
                                                        modifierParams.setString('Double Sided Material',newMat.getName())
                                    obj = it.next()

                            scene.writeMXS(str(outPath))
User avatar
By seghier
#399673
I made modification but still the same problem, setString, save scene ..>> the material added to the scene but grass material don't change
Code: Select all
nModifiers, ok = obj.getGeometryModifierExtensionsNumber()  # grass
                                    for i in range(nModifiers):
                                        modifierParams, ok = obj.getGeometryModifierExtensionParamsAtIndex(i)
                                        name, ok = modifierParams.getString('EXTENSION_NAME')
                                        if name == 'MaxwellGrass':
                                            par1 = modifierParams.getByIndex(0)[1]
                                            par2 = modifierParams.getByIndex(1)[1]
                                            print 'mat  '+par1
                                            print 'back  '+par2
                                            print 'old  '+oldMatName
                                            print  'new  '+newMat.getName()
                                            if par1 == oldMatName:
                                                print 'par1'
                                                print modifierParams.getString('Material')
                                                modifierParams.setString('Material', newMat.getName())
                                            elif par2 == oldMatName:
                                                print 'par2'
                                                print modifierParams.getString('Double Sided Material')
                                                modifierParams.setString('Double Sided Material', newMat.getName())
User avatar
By seghier
#399724
Still the same problem
Code: Select all
from pymaxwell5 import *

def MXMchangeMaterialFromMXM(mxsScene,newMatPath,outPath):
	
	scene = Cmaxwell(mwcallback)
	scene.readMXS(mxsScene)
	newMat = scene.createMaterial('leather')
	newMat.read(newMatPath)
		
	it = CmaxwellObjectIterator()
	obj = it.first(scene)
		
	while not obj.isNull():
			nModifiers, ok = obj.getGeometryModifierExtensionsNumber()  # grass
			for i in range(nModifiers):
					modifierParams, ok = obj.getGeometryModifierExtensionParamsAtIndex(i)
					name, ok = modifierParams.getString('EXTENSION_NAME')
					if name == 'MaxwellGrass':
						par1 = modifierParams.getByIndex(0)[1]
						par2 = modifierParams.getByIndex(1)[1]
						if par1 == oldMatName:
							print 'par1 yes'
							modifierParams.setString('Material', newMat.getName())
						elif par2 == oldMatName:
							print 'par2 yes'
							modifierParams.setString('Double Sided Material', newMat.getName())
					
			obj = it.next()
	scene.writeMXS(outPath)

if __name__ == "__main__":
	oldMatName = 'grass'
	newMatPath = 'C:/input/leather.mxm'
	mxsScene = 'C:/input/examples/grass.mxs'
	outPath = 'C:/output/grass_new.mxs'
	MXMchangeMaterialFromMXM(mxsScene,newMatPath,outPath)
User avatar
By Brany
#399741
We are aware of this bug. Found and fixed for the (coming soon) next release :wink:

The problem was that obj.getGeometryModifierExtensionParamsAtIndex (among other methods requesting MXParamlist data) was returning a copy of the data, instead of a reference (the original behaviour).

...and 3 Days later, 82.528 Views !!! ...NL, every[…]

Hello dear customers, We have just released a new[…]

grass generator extension

Just downloaded MWR5 for Rhino 6 and want to use g[…]

Hello everyone, I have a new bug with the latest M[…]