From 4987caf99a802d7100602ff2d558a0b1591a3b10 Mon Sep 17 00:00:00 2001 From: AlbertoGZ-dev Date: Sat, 13 May 2023 00:55:31 +0200 Subject: [PATCH] Added grouping --- CHANGELOG.md | 6 +++- cloneReference.py | 77 ++++++++++++++++++++++++++++++++++++----------- 2 files changed, 64 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1dcc87..cb432c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ ## Changelog ### v0.1.0 -Initial release. \ No newline at end of file +Initial release. + +### v0.1.1 +- Added ability to grouping copies from selected items as bundle group. +- Fixed issue in match transforms with some herarchies by forcing to use topnode only \ No newline at end of file diff --git a/cloneReference.py b/cloneReference.py index f280256..5592f4a 100644 --- a/cloneReference.py +++ b/cloneReference.py @@ -164,18 +164,19 @@ def __init__(self, parent=getMainWindow()): self.separator1.setFixedHeight(5) self.separator2 = QtWidgets.QWidget() self.separator2.setFixedHeight(5) + self.separator3 = QtWidgets.QWidget() + self.separator3.setFixedHeight(5) - # Namespace type self.namespaceLabel = QtWidgets.QLabel('Namespace: ') self.namespaceComboBox = QtWidgets.QComboBox() self.namespaceComboBox.addItems(['From selection', 'Custom']) + self.namespaceComboBox.currentIndexChanged.connect(self.namespaceType) self.namespaceCustomText = QtWidgets.QLineEdit('', self) self.namespaceCustomText.setPlaceholderText('namespace') self.namespaceCustomText.setVisible(False) self.namespaceCustomText.setMinimumWidth(140) - self.namespaceComboBox.currentIndexChanged.connect(self.namespaceType) - + # Offset inputs self.offsetXLabel = QtWidgets.QLabel('Offset X: ') self.offsetXSpinBox = QtWidgets.QDoubleSpinBox() @@ -195,7 +196,15 @@ def __init__(self, parent=getMainWindow()): self.copiesSpinBox.setMinimum(1) self.copiesSpinBox.setValue(1) self.copiesSpinBox.setObjectName('copies') - + + # Grouping controls + self.groupingLabel = QtWidgets.QLabel('Group copies: ') + self.groupingCheckBox = QtWidgets.QCheckBox('') + self.groupingCheckBox.clicked.connect(self.groupingCheck) + self.groupingNameText = QtWidgets.QLineEdit('myGroup01', self) + self.groupingNameText.setVisible(False) + self.groupingNameText.setMinimumWidth(100) + # Clone Reference button self.cloneBtn = QtWidgets.QPushButton('Clone Reference') self.cloneBtn.setFixedHeight(85) @@ -235,6 +244,12 @@ def __init__(self, parent=getMainWindow()): layout2.addWidget(self.copiesLabel, 8,0) layout2.addWidget(self.copiesSpinBox, 8,1) + layout2.addWidget(self.separator1, 9,0) + + layout2.addWidget(self.groupingLabel, 10,0) + layout2.addWidget(self.groupingCheckBox, 10,1) + layout2.addWidget(self.groupingNameText, 11,1) + layout3.addWidget(self.cloneBtn) self.resize(winWidth, winHeight) @@ -355,17 +370,23 @@ def namespaceType(self): self.namespaceCustomText.setVisible(False) - - + def groupingCheck(self): + if self.groupingCheckBox.isChecked(): + self.groupingNameText.setVisible(True) + else: + self.groupingNameText.setVisible(False) + + ### CLONE REFERENCE (main function) # def clone(self): + suffixID = '_c0001' copies = self.copiesSpinBox.value() offset = [self.offsetXSpinBox.value(), self.offsetYSpinBox.value(), self.offsetZSpinBox.value()] + groupName = self.groupingNameText.text() newItems = [] - newItemsCleaned = [] if len(itemSelected) < 1: self.statusBar.showMessage('Must be selected at least one item in the list', 4000) @@ -384,11 +405,11 @@ def clone(self): # Get namespace from each reference or use custom name if self.namespaceComboBox.currentText() == 'Custom': - refNS = self.namespaceCustomText.text() + refNS = self.namespaceCustomText.text()+suffixID else: - refNS = ref.split(':')[0] + refNS = ref.split(':')[0]+suffixID - print('--- Ref: '+str(ref)) + print('\n--- Ref: '+str(ref)) for n in range(copies): @@ -397,8 +418,9 @@ def clone(self): # Get the topnode only new = cmds.ls(new, assemblies=True)[0] - print('--- New: '+str(new)) - + print('\n--- New: '+str(new)) + + # Match transforms from initial reference to new reference cmds.matchTransform(new, ref) @@ -411,13 +433,32 @@ def clone(self): cmds.xform(new, ws=1, t=(newPosX, newPosY, newPosZ) ) - ## testing for grouping - #newItems.append(new[2].split(':')[0]) - #print(newItems) - #cmds.group(new[2], name='grp1') - + # Fill list and sorting + newItems.append(new) + newItemsSorted=sorted(newItems, key=lambda x: ''.join(reversed(x.split(':')[0]))) + + + # Divide list in chunks from the origin references selection amount + def divide_chunks(l, n): + for i in range(0, len(l), n): + yield l[i:i + n] + + n = len(itemSelected) + newItemsGrouped = list(divide_chunks(newItemsSorted, n)) + + + # Create groups for each bundle of selection + if self.groupingCheckBox.isChecked(): + for items in newItemsGrouped: + cmds.group(items, name=groupName) + + + # Display log for results + print('\n--- New Items: '+str(newItems)) + print('\n--- New Items Sorted: '+str(newItemsSorted)) + print('\n--- New Items Grouped: '+str(newItemsGrouped)) - print('--- '+ str(len(itemSelected)) + ' items cloned successfully!') + print('\n--- '+ str(len(itemSelected)) + ' items cloned successfully!') self.statusBar.showMessage(''+ str(len(itemSelected)) + ' items cloned successfully!', 4000) self.statusBar.setStyleSheet('background-color:' + green)