Page 1 of 1

relpace mxs ref : script to exe question

Posted: Wed Aug 30, 2017 10:53 pm
by seghier
hello
i tried to create exe from script called : change_mxs_reference.py
i have one problem with the new output mxs
when i change the place of the name group ( see image ) i got different errors ; the problem maybe there but i don't find solution

Image
Code: Select all
################################################################
# Change MXS references.
################################################################
from pymaxwell import *
import sys
import pymaxwell as mw
import qdarkstyle

from PyQt4 import QtGui, QtCore

class MXSRef(QtGui.QWidget):
    # SunEditor init method
	def __init__(self):
		super(MXSRef, self).__init__()
		self.initUI()

	# User interface initialization method
	def initUI(self):

		# Create labels
		label_mxs_folder = QtGui.QLabel('MXS Folder ')
		label_mxs_file = QtGui.QLabel('MXS File')
		label_old_mxsref = QtGui.QLabel('Old mxsRef')
		label_new_mxsref = QtGui.QLabel('New mxsRef')
		label_output = QtGui.QLabel('Output')

		# Create lineedits
		self.edit_mxs_folder = QtGui.QLineEdit()
		self.edit_mxs_file = QtGui.QLineEdit()
		self.edit_old_mxsref = QtGui.QLineEdit()
		self.edit_new_mxsref = QtGui.QLineEdit()
		self.edit_output = QtGui.QLineEdit()

		# Create buttons
		btn_mxsPath = QtGui.QPushButton('Open', self);
		btn_mxsFile = QtGui.QPushButton('Open', self);
		btn_old = QtGui.QPushButton('Open', self);
		btn_new = QtGui.QPushButton('Open', self);
		btn_outFolder = QtGui.QPushButton('Save', self);
		btn_run = QtGui.QPushButton('Run', self);

		btn_mxsPath.setGeometry(10, 10, 60, 35);
		btn_mxsFile.setGeometry(10, 10, 60, 35);
		btn_old.setGeometry(10, 10, 60, 35);
		btn_new.setGeometry(10, 10, 60, 35);
		btn_outFolder.setGeometry(10, 10, 60, 35);
		btn_run.setGeometry(10, 10, 60, 35);

		# Create grid layout
		grid = QtGui.QGridLayout()
		grid.setSpacing(10)

		# Populate grid with the labels, lineedits and buttons
		grid.addWidget(label_mxs_folder, 1, 1)
		grid.addWidget(self.edit_mxs_folder, 1, 2)
		grid.addWidget(btn_mxsPath, 1, 3)

		grid.addWidget(label_mxs_file, 2, 1)
		grid.addWidget(self.edit_mxs_file, 2, 2)
		grid.addWidget(btn_mxsFile, 2, 3)

		grid.addWidget(label_old_mxsref, 3, 1)
		grid.addWidget(self.edit_old_mxsref, 3, 2)
		grid.addWidget(btn_old, 3, 3)

		grid.addWidget(label_new_mxsref, 4, 1)
		grid.addWidget(self.edit_new_mxsref, 4, 2)
		grid.addWidget(btn_new, 4, 3)

		grid.addWidget(label_output, 5, 1)
		grid.addWidget(self.edit_output, 5, 2)
		grid.addWidget(btn_outFolder, 5, 3)


		grid.addWidget(btn_run, 7, 1)

		# Set main layout with grid above and resize window
		self.setLayout(grid)
		self.setFixedSize(640, 320)
		self.setWindowTitle('Mxs Ref Replace')

		# Connect buttons to methods to be called
		self.connect(btn_mxsPath, QtCore.SIGNAL('clicked()'), self.open1);
		self.connect(btn_mxsFile, QtCore.SIGNAL('clicked()'), self.open2);
		self.connect(btn_old, QtCore.SIGNAL('clicked()'), self.open3);
		self.connect(btn_new, QtCore.SIGNAL('clicked()'), self.open4);
		self.connect(btn_outFolder, QtCore.SIGNAL('clicked()'), self.save);
		self.connect(btn_run, QtCore.SIGNAL('clicked()'), self.replaceMxsReferenceByPath);

	# open scene
	def open1(self):
		infolder = QtGui.QFileDialog.getExistingDirectory(self, 'Open file', '');
		self.edit_mxs_folder.setText(infolder);

	def open2(self):
		inPath = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '', filter='*.mxs');
		self.edit_mxs_file.setText(inPath);

	def open3(self):
		inPath = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '', filter='*.mxs');
		self.edit_old_mxsref.setText(inPath);

	def open4(self):
		inPath = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '', filter='*.mxs');
		self.edit_new_mxsref.setText(inPath);

	# save scene
	def save(self):
		outfile = QtGui.QFileDialog.getExistingDirectory(self, 'saveFlle', '');
		self.edit_output.setText(outfile);


	def replaceMxsReferenceByPath(self):

		scene = Cmaxwell(mwcallback);

		inPath = scene.readMXS(self.edit_mxs_folder.text().toAscii().data());
		mxsList = [scene.readMXS(self.edit_mxs_file.text().toAscii().data())];
		old = scene.readMXS(self.edit_old_mxsref.text().toAscii().data());
		new = scene.readMXS(self.edit_new_mxsref.text().toAscii().data());
		outFolder = scene.readMXS(self.edit_output.text().toAscii().data());
		
		if not os.path.exists(outFolder):
			os.mkdir(outFolder)

		# loop over all mxs

		for mxs in mxsList:

			if not scene.readMXS(inPath + '/' + mxs):
				print('ERROR: cannot open ' + inPath + '/' + mxs)
				return 0

			# loop over all objects

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

			while not obj.isNull():
				mxsRef = obj.getReferencedScenePath()
				if mxsRef == old:
					# replace MXS reference path

					obj.setReferencedScenePath(new)
					print('Object: ' + obj.getName()[0] + '. MXS path replaced')

				obj = it.next()

			outputpath = outFolder + '/' + mxs
			if not scene.writeMXS(outputpath):
				print('ERROR: cannot save ' + outputpath)
				return 0

			print(outputpath)

		return 1


if __name__ == "__main__":
	# Create and show application
	app = QtGui.QApplication(sys.argv)
	app.setWindowIcon(QtGui.QIcon('C:/Users/archi/Desktop/script.ico'))
	window = MXSRef()
	app.setStyleSheet(qdarkstyle.load_stylesheet(pyside=False))
	window.show()
	sys.exit(app.exec_())

	# Replacing MXS references in all objects with the same MXS to replace

	#inPath = scene.readMXS(self.edit_mxs_folder.text().toAscii().data())
	#mxsList = [scene.readMXS(self.edit_mxs_file.text().toAscii().data())]
	#old = scene.readMXS(self.edit_old_mxsref.text().toAscii().data())
	#new = scene.readMXS(self.edit_new_mxsref.text().toAscii().data())
	#outFolder = scene.readMXS(self.edit_output.text().toAscii().data())

	#if replaceMxsReferenceByPath(inPath,mxsList,old,new,outFolder):
		#print 'OK'


Re: relpace mxs ref : script to exe question

Posted: Wed Aug 30, 2017 11:19 pm
by seghier
i find the problem
scene = mw.Cmaxwell(mw.mwcallback);
ok = scene.readMXS(self.edit_mxs_file.text().toAscii().data());
inPath = self.edit_mxs_folder.text().toAscii().data()
mxsList = ['scene.mxs']
old = self.edit_old_mxsref.text().toAscii().data()
new = self.edit_new_mxsref.text().toAscii().data()
outFolder = self.edit_output.text().toAscii().data()

how i can choose a file from folder ; when i do that with open button it take the full name with path

Re: relpace mxs ref : script to exe question

Posted: Thu Aug 31, 2017 12:25 am
by seghier
problem solved
Code: Select all
################################################################
# Change MXS references.
################################################################
from pymaxwell import *
import sys
import pymaxwell as mw
import qdarkstyle

from PyQt4 import QtGui, QtCore

class MXSRef(QtGui.QWidget):
    # SunEditor init method
	def __init__(self):
		super(MXSRef, self).__init__()
		self.initUI()

	# User interface initialization method
	def initUI(self):

		# Create labels
		label_mxs_folder = QtGui.QLabel('MXS Folder ')
		label_mxs_file = QtGui.QLabel('MXS File')
		label_old_mxsref = QtGui.QLabel('Old mxsRef')
		label_new_mxsref = QtGui.QLabel('New mxsRef')
		label_output = QtGui.QLabel('Output')

		# Create lineedits
		self.edit_mxs_folder = QtGui.QLineEdit()
		self.edit_mxs_file = QtGui.QLineEdit()
		self.edit_old_mxsref = QtGui.QLineEdit()
		self.edit_new_mxsref = QtGui.QLineEdit()
		self.edit_output = QtGui.QLineEdit()

		# Create buttons
		btn_mxsPath = QtGui.QPushButton('Open', self);
		btn_mxsFile = QtGui.QPushButton('Open', self);
		btn_old = QtGui.QPushButton('Open', self);
		btn_new = QtGui.QPushButton('Open', self);
		btn_outFolder = QtGui.QPushButton('Save', self);
		btn_run = QtGui.QPushButton('Run', self);

		btn_mxsPath.setGeometry(10, 10, 60, 35);
		btn_mxsFile.setGeometry(10, 10, 60, 35);
		btn_old.setGeometry(10, 10, 60, 35);
		btn_new.setGeometry(10, 10, 60, 35);
		btn_outFolder.setGeometry(10, 10, 60, 35);
		btn_run.setGeometry(10, 10, 60, 35);

		# Create grid layout
		grid = QtGui.QGridLayout()
		grid.setSpacing(10)

		# Populate grid with the labels, lineedits and buttons
		grid.addWidget(label_mxs_folder, 1, 1)
		grid.addWidget(self.edit_mxs_folder, 1, 2)
		grid.addWidget(btn_mxsPath, 1, 3)

		grid.addWidget(label_mxs_file, 2, 1)
		grid.addWidget(self.edit_mxs_file, 2, 2)
		grid.addWidget(btn_mxsFile, 2, 3)

		grid.addWidget(label_old_mxsref, 3, 1)
		grid.addWidget(self.edit_old_mxsref, 3, 2)
		grid.addWidget(btn_old, 3, 3)

		grid.addWidget(label_new_mxsref, 4, 1)
		grid.addWidget(self.edit_new_mxsref, 4, 2)
		grid.addWidget(btn_new, 4, 3)

		grid.addWidget(label_output, 5, 1)
		grid.addWidget(self.edit_output, 5, 2)
		grid.addWidget(btn_outFolder, 5, 3)


		grid.addWidget(btn_run, 7, 1)

		# Set main layout with grid above and resize window
		self.setLayout(grid)
		self.setFixedSize(640, 320)
		self.setWindowTitle('Mxs Ref Replace')

		# Connect buttons to methods to be called
		self.connect(btn_mxsPath, QtCore.SIGNAL('clicked()'), self.open1);
		self.connect(btn_mxsFile, QtCore.SIGNAL('clicked()'), self.open2);
		self.connect(btn_old, QtCore.SIGNAL('clicked()'), self.open3);
		self.connect(btn_new, QtCore.SIGNAL('clicked()'), self.open4);
		self.connect(btn_outFolder, QtCore.SIGNAL('clicked()'), self.save);
		self.connect(btn_run, QtCore.SIGNAL('clicked()'), self.replaceMxsReferenceByPath);

	# open scene
	def open1(self):
		infolder = QtGui.QFileDialog.getExistingDirectory(self, 'Open file', '');
		self.edit_mxs_folder.setText(infolder);

	def open2(self):
		inPath = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '', filter='*.mxs');
		self.edit_mxs_file.setText(inPath);

	def open3(self):
		inPath = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '', filter='*.mxs');
		self.edit_old_mxsref.setText(inPath);

	def open4(self):
		inPath = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '', filter='*.mxs');
		self.edit_new_mxsref.setText(inPath);

	# save scene
	def save(self):
		outfile = QtGui.QFileDialog.getExistingDirectory(self, 'saveFlle', '');
		self.edit_output.setText(outfile);


	def replaceMxsReferenceByPath(self):

		scene = mw.Cmaxwell(mw.mwcallback);
		ok = scene.readMXS(self.edit_mxs_file.text().toAscii().data());
		inPath = self.edit_mxs_folder.text().toAscii().data()
		mxsList = [os.path.basename(self.edit_mxs_file.text().toAscii().data())]
		old = self.edit_old_mxsref.text().toAscii().data()
		new = self.edit_new_mxsref.text().toAscii().data()
		outFolder = self.edit_output.text().toAscii().data()


		if not os.path.exists(outFolder):
			os.mkdir(outFolder)

		# loop over all mxs

		for mxs in mxsList:

			if not scene.readMXS(inPath + '/' + mxs):
				print('ERROR: cannot open ' + inPath + '/' + mxs)
				return 0

			# loop over all objects

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

			while not obj.isNull():
				mxsRef = obj.getReferencedScenePath()
				if mxsRef == old:
					# replace MXS reference path

					obj.setReferencedScenePath(new)
					print('Object: ' + obj.getName()[0] + '. MXS path replaced')

				obj = it.next()

			outputpath = outFolder + '/' + mxs
			if not scene.writeMXS(outputpath):
				print('ERROR: cannot save ' + outputpath)
				return 0

			print(outputpath)

		return 1


if __name__ == "__main__":
	# Create and show application
	app = QtGui.QApplication(sys.argv)
	app.setWindowIcon(QtGui.QIcon('C:/Users/archi/Desktop/script.ico'))
	window = MXSRef()
	app.setStyleSheet(qdarkstyle.load_stylesheet(pyside=False))
	window.show()
	sys.exit(app.exec_())

	# Replacing MXS references in all objects with the same MXS to replace

	#inPath = scene.readMXS(self.edit_mxs_folder.text().toAscii().data())
	#mxsList = [scene.readMXS(self.edit_mxs_file.text().toAscii().data())]
	#old = scene.readMXS(self.edit_old_mxsref.text().toAscii().data())
	#new = scene.readMXS(self.edit_new_mxsref.text().toAscii().data())
	#outFolder = scene.readMXS(self.edit_output.text().toAscii().data())

	#if replaceMxsReferenceByPath(inPath,mxsList,old,new,outFolder):
		#print 'OK'

Image

Re: relpace mxs ref : script to exe question

Posted: Thu Sep 07, 2017 12:06 pm
by seghier
hi ; the script read all files in the folder and replace the mxs ref but create new mxs ref and new name in every scene :
refA ; refA1 ; refA2 ...
how i can fix that ?
Code: Select all
        for mxs in mxsList:

            scene.readMXS(inPath+'/'+mxs)

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

            while not obj.isNull():
                mxsRef = obj.getReferencedScenePath()

                if mxsRef == old:
                    # replace MXS reference path
                    obj.setReferencedScenePath(new)
                    #obj.setName(newname)
                    print('Object: ' + obj.getName()[0] + '. MXS path replaced')


                obj = it.next()

            outputpath = outFolder + '/' + mxs
            scene.writeMXS(outputpath)

Re: relpace mxs ref : script to exe question

Posted: Thu Sep 07, 2017 3:16 pm
by Brany
I ran your code and I can't reproduce the problem. I don't see any problem there.

Re: relpace mxs ref : script to exe question

Posted: Thu Sep 07, 2017 3:20 pm
by seghier
Brany wrote:
Thu Sep 07, 2017 3:16 pm
I ran your code and I can't reproduce the problem. I don't see any problem there.
Thanks
The problem if i load a folder with many mxs files, the script change the mxsref in the first scene
In the second create other mxsref with name "mxref"1
In the third scene create two other mxref with names : "mxref"1, "mxref"2
....etc

Re: relpace mxs ref : script to exe question

Posted: Thu Sep 07, 2017 3:29 pm
by Brany
I see the problem now! There is an issue in readMXS(...), internally it does not the previous readed scene (in case any scene was readed), so you have to make sure that the scene is clean before calling readMXS, so you can solve this in two ways:

creating the scene variable on each loop iteration:
Code: Select all
for mxs in mxsList:
    scene = Cmaxwell(mwcallback)
    scene.readMXS(os.path.join(inDir,mxs))
    ...
cleaning the scene at the end of the loop:
Code: Select all
for mxs in mxsList:
    scene.readMXS(os.path.join(inDir,mxs))
    ...
    scene.freeScene()

Re: relpace mxs ref : script to exe question

Posted: Thu Sep 07, 2017 3:53 pm
by seghier
very clever ; thank you Brany
Code: Select all
        for mxs in mxsList:

            scene.readMXS(os.path.join(inPath, mxs))

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

            while not obj.isNull():
                mxsRef = obj.getReferencedScenePath()

                if mxsRef == old:
                    # replace MXS reference path
                    obj.setReferencedScenePath(new)
                    obj.setName(newname)
                    print('Object: ' + obj.getName()[0] + '. MXS path replaced')


                obj = it.next()

            outputpath = outFolder + '/' + mxs
            scene.writeMXS(outputpath)
            scene.freeScene()

Problem with mxsRef

Posted: Sat Sep 23, 2017 6:32 am
by seghier
Hello; before i tried many times the code and worked find but tonight didn't work ; than i tried the same code in maxwell folder but didn't work and the mxsref don't changed.
tested with pymaxwell for v3 and pymaxwell for v4
Code: Select all
################################################################
# Change MXS references.
################################################################

from pymaxwell import *
import os

def replaceMxsReferenceByPath(oldMxsRef,newMxsRef,inPath,mxsList,outFolder):
	
	scene = Cmaxwell(mwcallback)

	if not os.path.exists(outFolder):
		os.mkdir(outFolder)

	# loop over all mxs

	for mxs in mxsList:
		
		if not scene.readMXS(inPath+'/'+mxs):
			print('ERROR: cannot open '+inPath+'/'+mxs)
			return 0

		# loop over all objects

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

		while not obj.isNull():
			mxsRef = obj.getReferencedScenePath()
			if mxsRef == oldMxsRef:

				# replace MXS reference path

				obj.setReferencedScenePath(newMxsRef)
				print('Object: '+obj.getName()[0]+'. MXS path replaced')

			obj = it.next()

		outputpath = outFolder+'/'+mxs
		if not scene.writeMXS(outputpath):
			print('ERROR: cannot save '+outputpath)
			return 0

		print(outputpath)
	
	return 1


if __name__ == "__main__":


	# Replacing MXS references in all objects with the same MXS to replace

	inPath = 'C:/Users/archi/Desktop/examples/mxsRef'
	mxsList = ['test.mxs']
	oldMxsRef= 'C:/Users/archi/Desktop/examples/mxsRef/refA.mxs'
	newMxsRef = 'C:/Users/archi/Desktop/examples/mxsRef/refB.mxs'
	outFolder = 'C:/Users/archi/Desktop/examples/mxsRef/output'

	if replaceMxsReferenceByPath(oldMxsRef,newMxsRef,inPath,mxsList,outFolder):
		print 'OK'

Re: relpace mxs ref : script to exe question

Posted: Sat Sep 23, 2017 6:45 pm
by seghier
i think the problem solved because the script always read the mxsRef path
i make a change so it read the name of the mxsref
i will test more