- Wed Sep 06, 2017 7:12 pm
#395327
Hello ; i tried to convert "change material script" to standalone executable ;
the open button load all materials in the scene and than with double click we can select one of them and the name appear next to replace button
the new scene saved but nothing change ; the materials are the same and the function add 19 new empty materials have the same name of the new material.
the open button load all materials in the scene and than with double click we can select one of them and the name appear next to replace button
the new scene saved but nothing change ; the materials are the same and the function add 19 new empty materials have the same name of the new material.
Code: Select all
from pymaxwell import *
import sys
import os
from PyQt4 import QtCore, QtGui, uic
import change_mxm
uifile = 'F:/qtdesigner/maxwell script/maxwell tool/change mxm/change_mxm.ui'
Ui_MainWindow, QtBaseClass = uic.loadUiType(uifile)
class MyApp(QtGui.QMainWindow, change_mxm.Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
###########################################################################################
#buttons:
self.input_load_mxm
self.output_save_scene
self.open_old_scene.clicked.connect(self.openMxs)
self.save_new_scene.clicked.connect(self.save)
self.Open_mxm.clicked.connect(self.openMxm)
self.replace_mxm.clicked.connect(self.changeMaterialFromMXM)
self.mxm_item
###########################################################################################
def errorMessageOpen(self):
input = self.input_mxs_scene.text().toAscii().data()
if len(input) == 0:
msg = QtGui.QMessageBox.warning(self, 'Warning', 'Choose MXS Scene ', QtGui.QMessageBox.Ok);
return False;
return True;
def errorMessageSave(self):
input = self.output_save_scene.text().toAscii().data()
if len(input) == 0:
msg = QtGui.QMessageBox.warning(self, 'Warning', 'Save MXS Scene ', QtGui.QMessageBox.Ok);
return False;
return True;
def openMxs(self):
inPath = QtGui.QFileDialog.getOpenFileName(self, 'Open file', 'C:/', filter='*.mxs');
self.input_mxs_scene.setText(inPath);
self.mxm_list.clear()
if not self.errorMessageOpen():
return;
# loop over all mxs
mxsList = self.input_mxs_scene.text().toAscii().data()
scene = Cmaxwell(mwcallback);
ok = scene.readMXS(mxsList);
sceneMaterials = scene.getMaterialNames();
materialcount = int(scene.getMaterialsCount()[0])
for name in sceneMaterials:
scenematerial = scene.getMaterial(name)
#print scenematerial
self.mxm_list.addItem(name)
self.mxm_list.itemActivated.connect(self.printItemText)
#print self.mxm_list.count()
###########################################################################################
# loop over all objects in the scene
def changeMaterialFromMXM(self):
mxsList = self.input_mxs_scene.text().toAscii().data()
scene = Cmaxwell(mwcallback);
ok = scene.readMXS(mxsList);
for mxs in mxsList:
oldMatName = str(self.mxm_item.text())
print oldMatName
newMatPath = self.input_load_mxm.text().toAscii().data()
print newMatPath
oldMat = scene.getMaterial(oldMatName)
newMatName, descr, im, ok = CmaxwellMaterial.getMxmInfo(newMatPath)
newMat = scene.createMaterial(newMatName)
it = CmaxwellObjectIterator()
obj = it.first(scene)
while not obj.isNull():
# update object material
objMat = obj.getMaterial()[0]
if not objMat.isNull():
objMat.getName()
if objMat.getName() == oldMatName:
obj.setMaterial(newMat)
# update triangle group material
itGroup = CmaxwellObjectTrianglesGroupIterator()
group = itGroup.first(obj)
while not group.isNull():
# get group material from the 1st triangle of the group
triangles = group.getTriangles()
groupMat = obj.getTriangleMaterial(triangles[0])[0]
if groupMat.getName() == oldMatName:
group.setMaterial(newMat)
group = itGroup.next()
obj = it.next()
outputpath = self.output_save_scene.text().toAscii().data()
scene.writeMXS(outputpath)
#if not scene.writeMXS(outputpath):
#print('ERROR: cannot save ' + outputpath)
#return 0
#return 1
################################################################################################
def printItemText(self, item):
self.mxm_item.setText(str(item.text()))
def save(self):
outmxs = QtGui.QFileDialog.getSaveFileName(self, 'saveFlle', 'C:/', filter='Maxwell Scene (*.mxs)');
filepath = os.path.splitext(outmxs.toAscii().data())[0]
file_ext = os.path.splitext(outmxs.toAscii().data())[1]
outmxs_new = filepath + '_modified' + file_ext
if len(filepath) == 0:
self.output_save_scene.setText
self.output_save_scene.clear()
if not self.errorMessageSave():
return;
else:
self.output_save_scene.setText(outmxs_new)
def openMxm(self):
inPath = QtGui.QFileDialog.getOpenFileName(self, 'Open file', 'C:/', filter='*.mxm');
self.input_load_mxm.setText(inPath);
#self.mxm_list.clear()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyApp()
oldMatName = 'choose from list'
newMatPath = 'new material'
mxsList = ['input mxs scene']
outPath = 'save scene'
window.show()
sys.exit(app.exec_())