def _populate_model(self, modules, expand_new_modules=True): new_module_items = [] new_joint_items = [] for module in modules: module_item = self._create_module_item(module) new_module_items.append((module, module_item)) for joint in module.deform_joints: joint_item = self._create_joint_item(module, joint) new_joint_items.append((joint, joint_item)) if not Rig().is_built.get(): for guide in module.guide_nodes: guide_item = self._create_control_item(module, guide) new_joint_items.append((guide, guide_item)) else: for control in module.controllers: control_item = self._create_control_item(module, control) new_joint_items.append((control, control_item)) root = self.model.invisibleRootItem() for module, item in new_module_items: self._auto_parent_module_item(module, item, root) if expand_new_modules: source_index = self.model.indexFromItem(item) index = self.proxy.mapFromSource(source_index) self.tree_view.setExpanded(index, True) for joint, item in new_joint_items: self._auto_parent_joint_item(joint, item)
def _show_colors_recursively(self, parent): for item in self._iter_items_recursively(parent): if self.model.is_module_item(item): module = Rig().get_module(item.text()) else: module = self._joint_parent_module(item.text()) item.setForeground(self._colors[module.side.get()])
def _mirror_module(self): if not self.modules: return rig = Rig() new_modules = [] for module in self.modules: new_module = rig.mirror_module(module) if new_module is not None: new_modules.append(new_module) publish("modules-created", new_modules)
def _duplicate_module(self): """Duplicate the selected module.""" if not self.modules: return rig = Rig() new_modules = [] for module in self.modules: new_module = rig.duplicate_module(module) new_modules.append(new_module) publish("modules-created", new_modules)
def _on_selection_changed(self, selected, deselected): selection = self.tree_view.selectionModel() selected = selection.selectedRows() source_indices = map(self.proxy.mapToSource, selected) items = [self.model.itemFromIndex(index) for index in source_indices] joints = [item.text() for item in items if not self.model.is_module_item(item)] modules = [ Rig().get_module(item.text()) for item in items if self.model.is_module_item(item) ] if joints: cmds.select(joints) publish("selected-modules-changed", modules)
def _update_buttons_enabled(self): rig = Rig() if rig.is_published.get(): self.build_button.setEnabled(False) self.unbuild_button.setEnabled(False) self.publish_button.setEnabled(False) return elif rig.is_built.get(): self.build_button.setEnabled(False) self.unbuild_button.setEnabled(True) self.publish_button.setEnabled(True) else: self.build_button.setEnabled(True) self.unbuild_button.setEnabled(False) self.publish_button.setEnabled(False)
def _save_expanded_modules(self): modules = [] if not self.model: return modules for item in self._iter_items_recursively(self.model.invisibleRootItem()): if not self.model.is_module_item(item): continue source_index = self.model.indexFromItem(item) index = self.proxy.mapFromSource(source_index) if self.tree_view.isExpanded(index): module = Rig().get_module(item.text()) modules.append(module) return modules
def _generate_model(self): selection_model = self.tree_view.selectionModel() if selection_model: selection_model.selectionChanged.disconnect(self._on_selection_changed) self.model = ModulesModel() self.proxy = ModulesFilter() self.proxy.setSourceModel(self.model) self.proxy.setDisplayMode(self._display_mode) self._populate_model(Rig().rig_modules, expand_new_modules=False) self.tree_view.setModel(self.proxy) self.tree_view.expandAll() selection_model = self.tree_view.selectionModel() selection_model.selectionChanged.connect(self._on_selection_changed)
def _delete_module(self): """Delete the selected module.""" if not self.modules: return button = QtWidgets.QMessageBox.warning( self, "mop - Delete Module", "You are about to delete %d module(s). Continue ?" % len(self.modules), QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, ) if button != QtWidgets.QMessageBox.Yes: return rig = Rig() modules = self.modules[:] for module in self.modules: if module.name.get() == "root": logger.warning("Cannot delete root module.") modules.remove(module) continue rig.delete_module(module.node_name) publish("modules-deleted", modules)
def _update_display_names(self): if Rig().is_built.get(): self.joints_mode_control.setText("Controls") else: self.joints_mode_control.setText("Guides")
def _on_modules_updated(self, modified_fields): selected_items, current_item = self._save_selection() expanded_modules = self._save_expanded_modules() parents_have_changed = False sides_have_changed = False for module, modified_values in modified_fields.iteritems(): if "node_name" in modified_values: was_renamed = True module_item_name = modified_values["node_name"][0] module_item = self._item_for_name(module_item_name) module_item.setText(module.node_name) else: was_renamed = False module_item = self._item_for_name(module.node_name) if "parent_joint" in modified_values: parent_has_changed = True self._handle_reparenting(module, module_item) else: parent_has_changed = False parents_have_changed = parents_have_changed or parent_has_changed joints = module.deform_joints.get() if Rig().is_built.get(): controls = module.controllers.get() else: controls = module.guide_nodes.get() if "joint_count" in modified_values: old_joint_count = modified_values["joint_count"][0] new_joint_count = modified_values["joint_count"][1] joint_items = [ module_item.child(row) for row in xrange(module_item.rowCount()) if self.model.is_joint_item(module_item.child(row)) ] if new_joint_count > old_joint_count: self._fill_missing_joint_items(module, joints, joint_items) if Rig().is_built.get(): controls = module.controllers.get() control_items = [ module_item.child(row) for row in xrange(module_item.rowCount()) if self.model.is_control_item(module_item.child(row)) ] else: controls = module.guide_nodes.get() control_items = [ module_item.child(row) for row in xrange(module_item.rowCount()) if self.model.is_guide_item(module_item.child(row)) ] self._fill_missing_control_items(module, controls, control_items) else: self._remove_unused_items(module_item) if was_renamed: self._rename_child_joint_items(module_item, joints, controls) if "side" in modified_values: side_has_changed = True else: side_has_changed = False sides_have_changed = sides_have_changed or side_has_changed if parents_have_changed: self._restore_selection(selected_items, current_item) if sides_have_changed and self._color_by_side: self._show_colors_recursively(self.model.invisibleRootItem()) if expanded_modules: self._restore_expanded_modules(expanded_modules)
def _joint_parent_module(self, joint): modules = cmds.listConnections(joint + ".module", source=True) if not modules: raise AttributeError("Joint %s is not connected to a module !" % joint) module = modules[0] return Rig().get_module(module)