aboutsummaryrefslogtreecommitdiff
path: root/mayaPlug/shaveVraySharedFunctions.cpp
blob: f6748f3bfaceabd2b12af8396d21487283656dad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Shave and a Haircut
// (c) 2019 Epic Games
// US Patent 6720962

/**********************************************************************
 *<
	FILE:			shaveVraySharedFunctions.h  

	DESCRIPTION:	Various shared functions

	HISTORY:		created  21-08-2008 ( as part of 3ds Max + VRay hair shaders)
					merged   31-03-2010 

 *>
 **********************************************************************/

#include "shaveVraySharedFunctions.h"

#include <maya/MItDependencyGraph.h>
#include <maya/MDagPath.h>
#include <maya/MArgList.h>
#include <maya/MSelectionList.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnDagNode.h>
#include <maya/MPlugArray.h>
#include <maya/MItSelectionList.h>
#include <maya/MItDependencyGraph.h>
#include <maya/MItDependencyNodes.h>

#include <assert.h>

#ifdef USE_VRAY

MObject getNodeByName(const char *nodeName) 
{
	MString name(nodeName);
	MSelectionList sList;
	MGlobal::getSelectionListByName(name, sList);
	if (sList.length() < 1) return MObject();
	MObject obj;
	sList.getDependNode(0, obj);
	return obj;
}

bool getSourcePlugFromPlug(const MPlug destPlug, MPlug &plug)
{
	MPlugArray plugSources;
	destPlug.connectedTo(plugSources, true, false);

	bool foundSource = false;
	int len=plugSources.length();
	if (len == 1) 
	{
		foundSource = true;
		plug = plugSources[0];
	}
	return foundSource;
}

bool FindCurrentShaveShape(MObject &shShape)
{
	MStatus stat = MStatus::kSuccess;

	MDagPath nodePath;
	MObject component;
	MSelectionList list;
	
	MGlobal::getActiveSelectionList( list );

	
	for ( MItSelectionList listIter( list ); !listIter.isDone(); listIter.next() )
	{
		MObject depNode;
		listIter.getDependNode( depNode );

		if( FindConnectedShaveShape ( depNode, shShape))
			return true;

		if(depNode.hasFn( MFn::kDagNode ))
		{
			MFnDagNode dagFn(depNode);
			for(unsigned int i = 0; i < dagFn.childCount(); i++)
				if( FindConnectedShaveShape( dagFn.child(i), shShape))
					return true;
		}
	}

#ifdef _DEBUG
	MGlobal::displayInfo(MString("shaveVray: shaveShape not found."));
#endif
	return false;

}

bool FindConnectedShaveShape(MObject depNode, MObject &shShape)
{
	MStatus stat;
	{
		MItDependencyGraph graphIt(depNode,
									MFn::kInvalid,
									MItDependencyGraph::kUpstream,
									MItDependencyGraph::kDepthFirst,
									MItDependencyGraph::kNodeLevel,
									&stat);

		if(stat == MStatus::kSuccess)
		{
			for ( ; ! graphIt.isDone(); graphIt.next() )
			{
				MObject itNode = graphIt.thisNode();
				
				MFnDependencyNode itFn(itNode);
				
				if(itFn.typeId() == MTypeId(0x001029B7)) //shaveHairShape type id
				{
					shShape = itNode;
	#ifdef _DEBUG
					MGlobal::displayInfo(MString("shaveVray: shaveShape found: ") + itFn.name());
	#endif
					return true;
				}
				
			}
		}
	}
	{
		MItDependencyGraph graphIt(depNode,
									MFn::kInvalid,
									MItDependencyGraph::kDownstream,
									MItDependencyGraph::kDepthFirst,
									MItDependencyGraph::kNodeLevel,
									&stat);

		if(stat == MStatus::kSuccess)
		{
			for ( ; ! graphIt.isDone(); graphIt.next() )
			{
				MObject itNode = graphIt.thisNode();
				
				//if(itNode.apiType() == MFn::kShape)
				{
					MFnDependencyNode itFn(itNode);
					
					if(itFn.typeId() == MTypeId(0x001029B7)) //shaveHairShape type id
					{
						shShape = itNode;
	#ifdef _DEBUG
						MGlobal::displayInfo(MString("shaveVray: shaveShape found: ") + itFn.name());
	#endif
						return true;
					}
				}
			}
		}
	}
	return false;
}

void FindNodesByTypeId(MTypeId nodeType, MObjectArray& nodes)
{
	MItDependencyNodes	iter;

	nodes.clear();

	for (; !iter.isDone(); iter.next())
	{
		MObject				node = iter.item();
		MFnDependencyNode	nodeFn(node);

		if (nodeFn.typeId() == nodeType) nodes.append(node);
	}
}

void FindNodesByApiType(MFn::Type type, MObjectArray& nodes)
{
	MItDependencyNodes	iter;

	nodes.clear();

	for (; !iter.isDone(); iter.next())
	{
		MObject	node = iter.item();
		if (node.hasFn(type)) nodes.append(node);
	}
}

bool FindAllShaveShapes(MObjectArray& shShapes)
{
	 FindNodesByTypeId(MTypeId(0x001029B7),shShapes);
	 return shShapes.length() != 0;
}

bool EnumDownNodes(MObject thisNode, MObjectArray& nodes)
{
	MStatus stat;
	MItDependencyGraph graphIt(thisNode,
								MFn::kInvalid,
								MItDependencyGraph::kDownstream,
								MItDependencyGraph::kDepthFirst,
								MItDependencyGraph::kNodeLevel,
								&stat);

	if(stat == MStatus::kSuccess)
	{
		for ( ; ! graphIt.isDone(); graphIt.next() )
		{
			MObject itNode = graphIt.thisNode();
			nodes.append(itNode);
		}
	}
	return nodes.length() != 0;
}

bool GetLibPathEnv(const MString vrayVersion, MString &var)
{
	MStatus stat;
#if MAYA_API_VERSION < 20180000
	int apiMajor = MAYA_API_VERSION/100;
#else
	int apiMajor = MAYA_API_VERSION/10000;
#endif
	char envVarBuf[256] = {'\0'};
    int vrayMajorVersion = vrayVersion.substring(0, vrayVersion.index('.')-1).asInt();

    if (vrayMajorVersion < 4)
    {
	    sprintf(envVarBuf,"VRAY_FOR_MAYA%i_PLUGINS_x64",apiMajor); 
    }
    else
    {
	    sprintf(envVarBuf,"VRAY_FOR_MAYA%i_PLUGINS",apiMajor); 
    }

	//printf("V-Ray plug-ins dir eviornment variable %s\n",envVarBuf);

	var = envVarBuf;
	return true;
}

bool GetLibPath(const MString vrayVersion, MString &path)
{
	MStatus stat;
    MString pathVar;

    GetLibPathEnv(vrayVersion, pathVar);

	char* vrayPlugDir = getenv(pathVar.asChar());
	if(!vrayPlugDir)
		return false;

	//printf("V-Ray plug-ins dir  %s\n",vrayPlugDir);
	
	path = MString(vrayPlugDir);

	return true;
}

#endif //USE_VRAY