1/******************************************************************************
2	NintendoWare for CTR Maya Plugin
3
4	File:         NW4C_SetCombineGroup.mel
5	Description:  set combine group
6	Date:         2010/07/01
7
8	Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc.  All rights reserved.
9******************************************************************************/
10
11// UpdateWindow DoSearch
12
13/******************************************************************************
14	is scene anim object
15******************************************************************************/
16proc int IsSceneAnimObject(string $node)
17{
18	string $type = nodeType($node);
19	string $childs[] = `listRelatives -pa -s $node`;
20	if (size(`ls -cameras -lights -typ environmentFog $childs`) > 0 ||
21		$type == "lookAt")
22	{
23		return true;
24	}
25	return false;
26}
27
28/******************************************************************************
29	get selected transform (except camera & light)
30
31		return node size
32******************************************************************************/
33proc int GetSelectedXform(string $xforms[])
34{
35	clear($xforms);
36
37	string $nodes[] = `ls -sl -typ transform`;
38	for ($node in $nodes)
39	{
40		if (!IsSceneAnimObject($node))
41		{
42			$xforms[size($xforms)] = $node;
43		}
44	}
45
46	return size($xforms);
47}
48
49/******************************************************************************
50	combine group
51******************************************************************************/
52global proc int nw4cSetCombineGroup_Get_UseCombineGroup(string $node)
53{
54	return (`attributeQuery -n $node -ex "nw4cUseCombineGroup"`) ?
55		`getAttr ($node + ".nw4cUseCombineGroup")` : 0;
56}
57
58global proc nw4cSetCombineGroup_Add_UseCombineGroup(string $node)
59{
60	if (!`attributeQuery -n $node -ex "nw4cUseCombineGroup"`)
61	{
62		addAttr -ln "nw4cUseCombineGroup" -at "bool" -dv 0 -h 1 $node;
63	}
64}
65
66global proc nw4cSetCombineGroup_Set_UseCombineGroup(string $node, int $val)
67{
68	nw4cSetCombineGroup_Add_UseCombineGroup($node);
69	setAttr ($node + ".nw4cUseCombineGroup") $val;
70}
71
72proc SetJob_UseCombineGroup(string $node)
73{
74	if (!`attributeQuery -n $node -ex "nw4cUseCombineGroup"`)
75	{
76		return;
77	}
78	scriptJob -p nw4cSetCombineGroup_UseCombineGroup -rp -ac ($node + ".nw4cUseCombineGroup")
79		("if (`nw4cSetCombineGroup_Get_UseCombineGroup " + $node +
80		 "`) radioButtonGrp -e -sl 1 nw4cSetCombineGroup_UseCombineGroup;" +
81		 "else radioButtonGrp -e -sl 1 nw4cSetCombineGroup_DontCare;" +
82		 "control -e -en `nw4cSetCombineGroup_Get_UseCombineGroup " + $node + "` nw4cSetCombineGroup_CombineGroup;");
83}
84
85global proc nw4cSetCombineGroup_UseCombineGroupCB(string $mainNode)
86{
87	string $xforms[];
88	GetSelectedXform($xforms);
89
90	int $val = `radioButtonGrp -q -sl nw4cSetCombineGroup_UseCombineGroup`;
91	control -e -en $val nw4cSetCombineGroup_CombineGroup;
92	control -e -en $val nw4cSetCombineGroup_SetSequentialBtn;
93
94
95	for ($node in $xforms)
96	{
97		nw4cSetCombineGroup_Set_UseCombineGroup($node, $val);
98	}
99	SetJob_UseCombineGroup($mainNode);
100}
101
102/******************************************************************************
103	combine group number
104******************************************************************************/
105global proc int nw4cSetCombineGroup_Get_CombineGroup(string $node)
106{
107	return (`attributeQuery -n $node -ex "nw4cCombineGroup"`) ?
108		`getAttr ($node + ".nw4cCombineGroup")` : 0;
109}
110
111global proc nw4cSetCombineGroup_Add_CombineGroup(string $node)
112{
113	if (!`attributeQuery -n $node -ex "nw4cCombineGroup"`)
114	{
115		addAttr -ln "nw4cCombineGroup" -at "short" -min 0 -max 255 -dv 0 -h 1 $node;
116	}
117}
118
119global proc nw4cSetCombineGroup_Set_CombineGroup(string $node, int $val)
120{
121	nw4cSetCombineGroup_Add_CombineGroup($node);
122	setAttr ($node + ".nw4cCombineGroup") $val;
123}
124
125proc SetJob_CombineGroup(string $node)
126{
127	if (!`attributeQuery -n $node -ex "nw4cCombineGroup"`)
128	{
129		return;
130	}
131	scriptJob -p nw4cSetCombineGroup_CombineGroup -rp -ac ($node + ".nw4cCombineGroup")
132		("intField -e -v (`nw4cSetCombineGroup_Get_CombineGroup "
133			+ $node + "`) nw4cSetCombineGroup_CombineGroup");
134}
135
136global proc nw4cSetCombineGroup_CombineGroupCB(string $mainNode)
137{
138	string $xforms[];
139	GetSelectedXform($xforms);
140
141	int $val = `intField -q -v nw4cSetCombineGroup_CombineGroup`;
142	if ($val < 0)
143	{
144		$val = 0;
145		intField -e -v $val nw4cSetCombineGroup_CombineGroup;
146	}
147	else if ($val > 255)
148	{
149		$val = 255;
150		intField -e -v $val nw4cSetCombineGroup_CombineGroup;
151	}
152
153	for ($node in $xforms)
154	{
155		nw4cSetCombineGroup_Set_CombineGroup($node, $val);
156	}
157
158	SetJob_CombineGroup($mainNode);
159}
160
161/******************************************************************************
162	update window
163******************************************************************************/
164
165global proc nw4cSetCombineGroup_UpdateWindow()
166{
167	//-----------------------------------------------------------------------------
168	// get selection
169	string $xforms[];
170	GetSelectedXform($xforms);
171	int $enableFlag = (size($xforms) > 0);
172
173	//-----------------------------------------------------------------------------
174	// set node name
175	string $nodesName;
176	if ($enableFlag)
177	{
178		$nodesName = $xforms[0];
179		int $nodeSize = size($xforms);
180		for ($inode = 1; $inode < $nodeSize; ++$inode)
181		{
182			if ($inode == 3)
183			{
184				$nodesName += ", ... (" + $nodeSize + ")";
185				break;
186			}
187			$nodesName += ",  " + $xforms[$inode];
188		}
189	}
190	else
191	{
192		$nodesName = "(None)";
193	}
194	text -e -l $nodesName nw4cSetCombineGroup_NodeName;
195
196	//-----------------------------------------------------------------------------
197	// set current attr & change command
198	int $useCombineGroup = 0;
199	if ($enableFlag)
200	{
201		string $node = $xforms[0];
202
203		$useCombineGroup =
204			`nw4cSetCombineGroup_Get_UseCombineGroup $node`;
205		radioButtonGrp -e -sl (!$useCombineGroup)
206			-on1 ("nw4cSetCombineGroup_UseCombineGroupCB " + $node)
207			nw4cSetCombineGroup_DontCare;
208		radioButtonGrp -e -sl $useCombineGroup
209			-on1 ("nw4cSetCombineGroup_UseCombineGroupCB " + $node)
210			nw4cSetCombineGroup_UseCombineGroup;
211
212		intField -e -v `nw4cSetCombineGroup_Get_CombineGroup $node`
213			-cc ("nw4cSetCombineGroup_CombineGroupCB " + $node)
214			nw4cSetCombineGroup_CombineGroup;
215
216		// setAttr �ȂǂŕύX���ꂽ�ꍇ�ɁA
217		// �R���g���[���̒l���X�V���邽��
218		SetJob_UseCombineGroup($node);
219		SetJob_CombineGroup($node);
220	}
221
222	//-----------------------------------------------------------------------------
223	// set enable state
224	control -e -en $enableFlag nw4cSetCombineGroup_DontCare;
225	control -e -en $enableFlag nw4cSetCombineGroup_UseCombineGroup;
226	control -e -en ($enableFlag && $useCombineGroup) nw4cSetCombineGroup_CombineGroup;
227	control -e -en $enableFlag nw4cSetCombineGroup_Description;
228	control -e -en ($enableFlag && $useCombineGroup) nw4cSetCombineGroup_SetSequentialBtn;
229}
230
231/******************************************************************************
232	get hierarchy level
233******************************************************************************/
234proc int GetXformHierarchyLevel(string $node)
235{
236	string $buffer[];
237	string $longName = `longNameOf($node)`;
238	return `tokenize $longName "|" $buffer`;
239}
240
241
242/******************************************************************************
243	sort xforms
244******************************************************************************/
245proc SortXforms(string $xforms[], string $parents[], int $levels[])
246{
247	int $xfSize = size($xforms);
248	for ($isg = 0; $isg < $xfSize - 1; ++$isg)
249	{
250		for ($jsg = $isg + 1; $jsg < $xfSize; ++$jsg)
251		{
252			string $xf0   = $xforms[$isg];
253			string $prnt0 = $parents[$isg];
254			int $lv0      = $levels[$isg];
255			string $xf1   = $xforms[$jsg];
256			string $prnt1 = $parents[$jsg];
257			int $lv1      = $levels[$jsg];
258			int $cond     = 0;
259			int $cmp;
260
261			if ($lv0 == $lv1)
262			{
263				// ���x���������Ȃ玟�͐e�̖��O�Ń\�[�g����
264				$cmp = strcmp($prnt0, $prnt1);
265				if ($cmp == 0)
266				{
267					// �e�̖��O�������Ȃ�m�[�h���Ń\�[�g����
268					$cmp = strcmp($xf0, $xf1);
269				}
270				$cond = ($cmp > 0);
271			}
272			else
273			{
274				$cond = ($lv0 > $lv1);
275			}
276
277			if ($cond)
278			{
279				$xforms[$isg]  = $xf1;
280				$parents[$isg] = $prnt1;
281				$levels[$isg]  = $lv1;
282				$xforms[$jsg]  = $xf0;
283				$parents[$jsg] = $prnt0;
284				$levels[$jsg]  = $lv0;
285			}
286		}
287	}
288}
289
290
291
292
293
294
295/******************************************************************************
296	�O���[�v�ԍ���A���I�ɐݒ肷��
297******************************************************************************/
298global proc nw4cSetCombineGroup_SetSequential()
299{
300	string $xforms[];
301	string $parents[];
302	int $hierarchyLevels[];
303	int $numXforms;
304	int $i;
305
306	GetSelectedXform($xforms);
307	$numXforms = size($xforms);
308
309	// �O���[�v�ԍ����擾����
310	int $val = `intField -q -v nw4cSetCombineGroup_CombineGroup`;
311
312	// �擾�����O���[�v�ԍ��� xform �̐������� 255 ������΃G���[�Ƃ���
313	if (($val+$numXforms) <= 255)
314	{
315		// �e xform �̐e�m�[�h�ƊK�w�\���̃��x�����擾����
316		for ($i=0; $i<$numXforms; $i++)
317		{
318			$parents[$i] = `firstParentOf $xforms[$i]`;
319			$hierarchyLevels[$i] = `GetXformHierarchyLevel $xforms[$i]`;
320		}
321
322		// xform ���\�[�g����
323		// �K�w�\���̃��x��, �e�m�[�h�̖��O, �m�[�h�̖��O�̏��ԂŃ\�[�g����
324		SortXforms($xforms, $parents, $hierarchyLevels);
325
326		print("NW4C_SetCombineGroup: Set sequaltial\n");
327		for ($i=0; $i<$numXforms; $i++)
328		{
329			print("  " + $xforms[$i] + ": combine group " + ($val + $i) + "\n");
330			nw4cSetCombineGroup_Set_CombineGroup($xforms[$i], ($val + $i));
331		}
332	}
333	else
334	{
335		error "The combine group number exceeds the limitation (255).\n";
336	}
337}
338
339/******************************************************************************
340	search mode callback
341******************************************************************************/
342global proc nw4cSetCombineGroup_SearchModeCB()
343{
344	int $dontCare =
345		(`radioButtonGrp -q -sl nw4cSetCombineGroup_SearchDontCare` == 1);
346	int $useCombineGroup =
347		(`radioButtonGrp -q -sl nw4cSetCombineGroup_SearchUseCombineGroup` == 1);
348
349	control -e -en $useCombineGroup nw4cSetCombineGroup_SearchCompare;
350	control -e -en $useCombineGroup nw4cSetCombineGroup_SearchCombineGroup;
351	control -e -en ($dontCare || $useCombineGroup) nw4cSetCombineGroup_SearchBtn;
352}
353
354/******************************************************************************
355	search value callback
356******************************************************************************/
357global proc nw4cSetCombineGroup_SearchValueCB()
358{
359	int $val = `intField -q -v nw4cSetCombineGroup_SearchCombineGroup`;
360	if ($val < 0)
361	{
362		intField -e -v 0 nw4cSetCombineGroup_SearchCombineGroup;
363	}
364	else if ($val > 255)
365	{
366		intField -e -v 255 nw4cSetCombineGroup_SearchCombineGroup;
367	}
368}
369
370/******************************************************************************
371	do search
372******************************************************************************/
373global proc nw4cSetCombineGroup_DoSearch()
374{
375	int $searchUse =
376		(`radioButtonGrp -q -sl nw4cSetCombineGroup_SearchUseCombineGroup` == 1);
377	int $compare = `optionMenu -q -sl nw4cSetCombineGroup_SearchCompare` - 1;
378	nw4cSetCombineGroup_SearchValueCB(); // clamp value
379	int $searchVal = `intField -q -v nw4cSetCombineGroup_SearchCombineGroup`;
380
381	int $nodeCount = 0;
382	select -cl;
383	string $xforms[] = `ls -typ transform`;
384	for ($node in $xforms)
385	{
386		if (IsSceneAnimObject($node))
387		{
388			continue;
389		}
390		int $useCombineGroup = nw4cSetCombineGroup_Get_UseCombineGroup($node);
391		int $CombineGroup = nw4cSetCombineGroup_Get_CombineGroup($node);
392		int $match = 0;
393		if (!$searchUse)
394		{
395			$match = !$useCombineGroup;
396		}
397		else
398		{
399			if (!$useCombineGroup)
400			{
401				$match = 0;
402			}
403			else if ($compare == 0)
404			{
405				$match = ($CombineGroup < $searchVal);
406			}
407			else if ($compare == 1)
408			{
409				$match = ($CombineGroup <= $searchVal);
410			}
411			else if ($compare == 2)
412			{
413				$match = ($CombineGroup == $searchVal);
414			}
415			else if ($compare == 3)
416			{
417				$match = ($CombineGroup >= $searchVal);
418			}
419			else
420			{
421				$match = ($CombineGroup > $searchVal);
422			}
423		}
424		if ($match)
425		{
426			select -add $node;
427			++$nodeCount;
428		}
429	}
430
431	if ($nodeCount == 0)
432	{
433		print "Not found\n";
434	}
435	else
436	{
437		string $msg = "Found: " + $nodeCount + " transform node";
438		if ($nodeCount >= 2)
439		{
440			$msg += "s";
441		}
442		print ($msg + "\n");
443	}
444}
445
446/******************************************************************************
447	main
448******************************************************************************/
449global proc NW4C_SetCombineGroup()
450{
451	int $afterMaya2011Adjust = 0;
452	if (getApplicationVersionAsFloat() >= 2011)
453		$afterMaya2011Adjust = 1;
454
455	//-----------------------------------------------------------------------------
456	// create window
457	int $winW = 400;
458	int $winH = 385;
459	if (!`window -exists nw4cSetCombineGroup_Win`)
460	{
461		window -t "NW4C Set Combine Group" -wh $winW $winH -mxb 0 -mb 1
462			nw4cSetCombineGroup_Win;
463		menu -l "Help";
464		menuItem -l ("Help on " + `window -q -t nw4cSetCombineGroup_Win` + "...")
465			-c "NW4C_ShowHelp \"html/NW4C_SetCombineGroup.html\" \"\"";
466
467		columnLayout -adj 1 -cat "both" 4 -cal "center" -rs 4;
468
469		//-----------------------------------------------------------------------------
470		// node name
471		frameLayout -l "Selected Transform Node" -cll 0 -cl 0 -bv 1 -bs "etchedIn";
472			columnLayout -adj 1 -cal "center" -rs 8;
473
474			text -l "" nw4cSetCombineGroup_NodeName;
475
476			setParent ..; // columnLayout
477		setParent ..; // formLayout
478
479		//-----------------------------------------------------------------------------
480		// set
481		frameLayout -l "Set" -cll 0 -cl 0 -bv 1 -bs "etchedIn";
482			columnLayout -adj 1 -cal "center" -rs 0;
483			string $form2 = `formLayout -nd 400`;
484				radioButtonGrp -l "Combine Group" -nrb 1 -cw2 110 80
485					-l1 "Don't care"
486					nw4cSetCombineGroup_DontCare;
487
488				radioButtonGrp -l "" -nrb 1 -cw2 110 18
489					-l1 ""
490					-scl nw4cSetCombineGroup_DontCare
491					nw4cSetCombineGroup_UseCombineGroup;
492
493				intField -w 50 -v 0
494					nw4cSetCombineGroup_CombineGroup;
495
496				text -l "Setting range 0 - 255"
497					nw4cSetCombineGroup_Description;
498			setParent ..; // formLayout
499			formLayout -e
500				-af nw4cSetCombineGroup_DontCare "top" 8
501				-af nw4cSetCombineGroup_DontCare "left" 0
502				-an nw4cSetCombineGroup_DontCare "bottom"
503				-an nw4cSetCombineGroup_DontCare "right"
504
505				-ac nw4cSetCombineGroup_UseCombineGroup "top" 8 nw4cSetCombineGroup_DontCare
506				-af nw4cSetCombineGroup_UseCombineGroup "left" 0
507				-an nw4cSetCombineGroup_UseCombineGroup "bottom"
508				-an nw4cSetCombineGroup_UseCombineGroup "right"
509
510				-ac nw4cSetCombineGroup_CombineGroup "top" 4 nw4cSetCombineGroup_DontCare
511				-ac nw4cSetCombineGroup_CombineGroup "left" 0 nw4cSetCombineGroup_UseCombineGroup
512				-an nw4cSetCombineGroup_CombineGroup "bottom"
513				-an nw4cSetCombineGroup_CombineGroup "right"
514
515				-ac nw4cSetCombineGroup_Description "top" 4 nw4cSetCombineGroup_CombineGroup
516				-ac nw4cSetCombineGroup_Description "left" 4 nw4cSetCombineGroup_UseCombineGroup
517				-af nw4cSetCombineGroup_Description "bottom" 8
518				-an nw4cSetCombineGroup_Description "right"
519				$form2;
520			rowColumnLayout -nc 2 -cw 1 200 -cw 2 150;
521				text -l ""; // dummy
522				button -l "Set Sequential"
523					-c "nw4cSetCombineGroup_SetSequential"
524					nw4cSetCombineGroup_SetSequentialBtn;
525			setParent ..; // rowColumnLayout
526			separator -h 8 -st "none";
527
528			setParent ..; // columnLayout
529		setParent ..; // frameLayout
530
531		//-----------------------------------------------------------------------------
532		// search
533		frameLayout -l "Search" -cll 0 -cl 0 -bv 1 -bs "etchedIn";
534			columnLayout -adj 1 -cal "center" -rs 0;
535			string $form3 = `formLayout -nd 400`;
536				radioButtonGrp -l "" -nrb 1 -cw2 110 80
537					-l1 "Don't care"
538					-on1 "nw4cSetCombineGroup_SearchModeCB"
539					nw4cSetCombineGroup_SearchDontCare;
540
541				radioButtonGrp -l "" -nrb 1 -cw2 110 18
542					-l1 ""
543					-scl nw4cSetCombineGroup_SearchDontCare
544					-on1 "nw4cSetCombineGroup_SearchModeCB"
545					nw4cSetCombineGroup_SearchUseCombineGroup;
546
547				optionMenu -w 110
548					nw4cSetCombineGroup_SearchCompare;
549					menuItem -l "<  : ( under )";
550					menuItem -l "<= : ( below )";
551					menuItem -l "=  : ( exactly )";
552					menuItem -l ">= : ( above )";
553					menuItem -l ">  : ( over )";
554
555				intField -w 50 -h 26 -v 0
556					-cc "nw4cSetCombineGroup_SearchValueCB"
557					nw4cSetCombineGroup_SearchCombineGroup;
558			setParent ..; // formLayout
559			formLayout -e
560				-af nw4cSetCombineGroup_SearchDontCare "top" 8
561				-af nw4cSetCombineGroup_SearchDontCare "left" 0
562				-an nw4cSetCombineGroup_SearchDontCare "bottom"
563				-an nw4cSetCombineGroup_SearchDontCare "right"
564
565				-ac nw4cSetCombineGroup_SearchUseCombineGroup "top" 10 nw4cSetCombineGroup_SearchDontCare
566				-af nw4cSetCombineGroup_SearchUseCombineGroup "left" 0
567				-an nw4cSetCombineGroup_SearchUseCombineGroup "bottom"
568				-an nw4cSetCombineGroup_SearchUseCombineGroup "right"
569
570				-ac nw4cSetCombineGroup_SearchCompare "top" (4 + $afterMaya2011Adjust * 3) nw4cSetCombineGroup_SearchDontCare
571				-ac nw4cSetCombineGroup_SearchCompare "left" 0 nw4cSetCombineGroup_SearchUseCombineGroup
572				-an nw4cSetCombineGroup_SearchCompare "bottom"
573				-an nw4cSetCombineGroup_SearchCompare "right"
574
575				-ac nw4cSetCombineGroup_SearchCombineGroup "top" 4 nw4cSetCombineGroup_SearchDontCare
576				-ac nw4cSetCombineGroup_SearchCombineGroup "left" 4 nw4cSetCombineGroup_SearchCompare
577				-af nw4cSetCombineGroup_SearchCombineGroup "bottom" 8
578				-an nw4cSetCombineGroup_SearchCombineGroup "right"
579				$form3;
580			rowColumnLayout -nc 2 -cw 1 200 -cw 2 150;
581				text -l ""; // dummy
582				button -l "Search"
583					-c "nw4cSetCombineGroup_DoSearch"
584					nw4cSetCombineGroup_SearchBtn;
585			setParent ..; // rowColumnLayout
586			separator -h 8 -st "none";
587
588			setParent ..; // columnLayout
589		setParent ..; // formLayout
590		optionMenu -e -sl 3 nw4cSetCombineGroup_SearchCompare;
591
592		setParent ..; // columnLayout
593
594		//-----------------------------------------------------------------------------
595		// close button
596		string $closeCmd = "deleteUI nw4cSetCombineGroup_Win";
597		string $form = `formLayout -nd 100`;
598			string $closeBtn = `button -h 26 -l "Close" -c $closeCmd`;
599			formLayout -e
600				-af $closeBtn "top" 0
601				-af $closeBtn "left" 0
602				-af $closeBtn "bottom" 0
603				-af $closeBtn "right" 0
604				$form;
605		setParent ..; // formLayout
606
607		setParent ..; // columnLayout
608
609		setFocus $closeBtn;
610
611		//-----------------------------------------------------------------------------
612		// set selection change job
613		scriptJob -p nw4cSetCombineGroup_Win
614			-e "SelectionChanged" "nw4cSetCombineGroup_UpdateWindow";
615	}
616	if (`window -q -w nw4cSetCombineGroup_Win` < $winW)
617	{
618		window -e -w $winW nw4cSetCombineGroup_Win;
619	}
620	if (`window -q -h nw4cSetCombineGroup_Win` < $winH)
621	{
622		window -e -h $winH nw4cSetCombineGroup_Win;
623	}
624	//window -e -wh $winW $winH nw4cSetCombineGroup_Win;
625
626	//-----------------------------------------------------------------------------
627	// update window
628	nw4cSetCombineGroup_UpdateWindow;
629	nw4cSetCombineGroup_SearchModeCB;
630
631	//-----------------------------------------------------------------------------
632	// show window
633	showWindow nw4cSetCombineGroup_Win;
634}
635