Skip to content

Instantly share code, notes, and snippets.

@johnpneumann
Last active August 29, 2015 14:18
Show Gist options
  • Save johnpneumann/10e2bf90e9422f377252 to your computer and use it in GitHub Desktop.
Save johnpneumann/10e2bf90e9422f377252 to your computer and use it in GitHub Desktop.
Maya, QT and You code
/*
object_creator_tutorial
Original file creation date: 2010-07-18
Concept and scripting by John P. Neumann (john@animateshmanimate.com) http://animateshmanimate.com
This work is licensed under the 3-clause BSD ("New BSD License") license.
Licensing information is included in the download.
What is it:
Usage:
Limitations:
Bugs:
ToDo:
Changelog:
0.1
*/
global proc objectCreator(){
//If Maya's version is greater than or equal to 2011 go ahead
if (getApplicationVersionAsFloat() >= 2011){
//set the scripts directory so we can load our ui file
string $scriptsDirectory = `internalVar -usd`;
//load the ui file
string $objectCreatorWindow = `loadUI -uiFile ($scriptsDirectory+"/object_creator_tutorial.ui")`;
//show the window
showWindow $objectCreatorWindow;
//find all objects in the scene and load their mesh transform name
updateObjectsInScene;
}else{
//You aren't using Maya 2011 if this pops up. The upgrade is worth it! :)
print "You're using a version of Maya that is incompatible with this script.\n";
}
}
global proc updateObjectsInScene(){
//to get the meshes sets available
string $listObjects[] = `ls -type mesh`;
//get the transform node
string $topLevelListObjects[] = `listRelatives -parent -type "transform" $listObjects`;
//clear the scroll list so no duplicates show up
textScrollList -e -ra objectScrollList;
//iterate through and add all the meshes
for($item in $topLevelListObjects){
//update the items in the scroll list
textScrollList -e -a $item objectScrollList;
}
}
//change the object(s) name
global proc objectRenamer(){
//select all the items that are active in the list
string $sel[] = `textScrollList -q -si objectScrollList`;
//get the users input for the new name
string $objectNewName = `textField -q -text objectNewName`;
//rename the object
rename $sel $objectNewName;
//deselect everything in the scrollList so we don't change things we don't want to next time
textScrollList -e -deselectAll objectScrollList;
//clear all selections
select -cl;
//update the scroll list since we just renamed the object
updateObjectsInScene;
}
//create the object
global proc objectCreation(){
//get the object we want to create
string $createObject = `optionMenu -q -v typeOfObject`;
//get the name of the object we want to create
string $objectCreateName = `textField -q -text objectNewName`;
//create the object
eval $createObject;
//get the selected object
string $sel[] = `ls -sl`;
if($objectCreateName == ""){
//do nothing
}else{
//rename the selected object
rename $sel $objectCreateName;
}
//update the scroll list since we just created an object
updateObjectsInScene;
}
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>538</width>
<height>282</height>
</rect>
</property>
<property name="windowTitle">
<string>Object Creator</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QWidget" name="gridLayoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>511</width>
<height>221</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QGroupBox" name="objectCreator">
<property name="title">
<string>Object Creator</string>
</property>
<widget class="QListWidget" name="objectScrollList">
<property name="geometry">
<rect>
<x>10</x>
<y>40</y>
<width>201</width>
<height>131</height>
</rect>
</property>
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="dragEnabled">
<bool>true</bool>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::InternalMove</enum>
</property>
<property name="defaultDropAction">
<enum>Qt::MoveAction</enum>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>141</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Objects In Our Scene</string>
</property>
</widget>
<widget class="QLineEdit" name="objectNewName">
<property name="geometry">
<rect>
<x>310</x>
<y>80</y>
<width>120</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>220</x>
<y>80</y>
<width>91</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Object Name:</string>
</property>
</widget>
<widget class="QComboBox" name="typeOfObject">
<property name="geometry">
<rect>
<x>220</x>
<y>40</y>
<width>131</width>
<height>26</height>
</rect>
</property>
<item>
<property name="text">
<string>polySphere</string>
</property>
</item>
<item>
<property name="text">
<string>polyCube</string>
</property>
</item>
<item>
<property name="text">
<string>polyCylinder</string>
</property>
</item>
<item>
<property name="text">
<string>polyCone</string>
</property>
</item>
<item>
<property name="text">
<string>polyPlane</string>
</property>
</item>
<item>
<property name="text">
<string>polyTorus</string>
</property>
</item>
<item>
<property name="text">
<string>polyPrism</string>
</property>
</item>
<item>
<property name="text">
<string>polyPyramid</string>
</property>
</item>
<item>
<property name="text">
<string>polyPipe</string>
</property>
</item>
<item>
<property name="text">
<string>polyHelix</string>
</property>
</item>
</widget>
<widget class="QPushButton" name="createObjectButton">
<property name="geometry">
<rect>
<x>220</x>
<y>180</y>
<width>113</width>
<height>32</height>
</rect>
</property>
<property name="text">
<string>Create Object</string>
</property>
<property name="-command" stdset="0">
<string>objectCreation</string>
</property>
</widget>
<widget class="QPushButton" name="reloadObjectsButton">
<property name="geometry">
<rect>
<x>10</x>
<y>180</y>
<width>131</width>
<height>32</height>
</rect>
</property>
<property name="text">
<string>Reload Objects</string>
</property>
<property name="-command" stdset="0">
<string>updateObjectsInScene</string>
</property>
</widget>
<widget class="QPushButton" name="renameObjectButton">
<property name="geometry">
<rect>
<x>340</x>
<y>180</y>
<width>121</width>
<height>32</height>
</rect>
</property>
<property name="text">
<string>Rename Object</string>
</property>
<property name="-command" stdset="0">
<string>objectRenamer</string>
</property>
</widget>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>538</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment