Inside Sabertooth
Learn how Sabertooth uses 3ds Max to create 3D interactive projects, including HBO Go’s Game of Thrones interactive experience
  • 1/3
You are here: Forum Home / Autodesk® Maya® / MEL / How to programmatically create new material?
  RSS 2.0 ATOM  

How to programmatically create new material?
Rate this thread
 
62876
 
Permlink of this thread  
avatar
  • blackears
  • Posted: 15 December 2011 09:53 PM
  • Total Posts: 13
  • Joined: 29 October 2011 04:29 PM

I’m writing a file importer and running into difficulty trying to create a new material. 

The idea is pretty simple - I’ve already created my new mesh object and added it.  I have a string with a texture name and want to create a lambert shader with it’s color channel set to a file with that name.  To further complicate things, the mesh was created using the API’s MFnMesh::create() method, so it does not have a shading group assigned to it.

How can I assign a shader to my new mesh?  I’m doing this in C++, but if you know how to do it with Python or even Mel, it would be helpful to know how.



Replies: 0
avatar

I don’t have a ready-made example of exactly what you are trying to do, but making a basic shader is easy:

string $sName = `shadingNode -asShader "lambert" -n MyShader`;

You can mix MEL in with your C++ using MGlobal::executeCommand().

I believe the part you need that I don’t have handy would use createNode() to create the file node and you would use connectAttr() to hook the nodes up. Just look in the hyperShade (or better yet, if you have it, the new Node Editor) to see the needed connections.

To complete the task of putting the shader on your mesh object you can use

sets --fe $sName "myObjectName"

Given the edit/compile/copy/test cycle time involved in C development, I would think it would be most efficient to prototype this in MEL (or python) by putting it all in a file and loading that and executing it. If you want to later make an all-C++ single-file solution you would then have a working outline of what needs done. Given the speed of most machines, MEL is actually not a bad solution for things like this, but you cannot get access to most of what you need to really do a good job making anything but simple meshes without using the API (C++ or python).

I don’t know what your tool needs are, I have buried MEL code like that in a translator plug-in, which integrates into Maya using load/save/import/export/export all. I have also written MPxCommand plug-ins in which I created a new MEL command that used “-” flags to load or save a private format of mesh parts as a pipeline tool. On that project, I used a lot of MEL, which made shelf buttons, assigned shaders and so on, but the low-level meshFn stuff was all done in C. That give great flexibility because most of the scope creep (gee, what if I added...) was all able to be done in MEL.

<* Wes *>



Replies: 0