Add vray material id

1,816 views
Skip to first unread message

shapeofmotion

unread,
Oct 14, 2013, 11:49:26 AM10/14/13
to python_in...@googlegroups.com
Hi!

Does anyone know how to add a vray material id attribute to a material with python or pymel?

best regards
# J

Raphael Gaudin

unread,
Oct 14, 2013, 1:43:07 PM10/14/13
to python_in...@googlegroups.com
try this :

import maya.cmds as mc
import maya.mel as mel

for s in mc.ls( sl=1 , type='VRayMtl' ):

    mel.eval('string $s = "%s";' % s)
    mel.eval( "vray addAttributesFromGroup $s vray_material_id 1" )



cheers,

Raphael


2013/10/14 shapeofmotion <jo...@petfactory.se>

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/1f90a9ce-6c56-4e23-a266-d51ca48dc841%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

shapeofmotion

unread,
Oct 28, 2013, 12:38:42 PM10/28/13
to python_in...@googlegroups.com
Thank you,

But I would like to set the id after creation.
like so:

import maya.cmds as cmds
import maya.mel as mel

# add vray material ID
mel
.eval("vray addAttributesFromGroup VRayMtl1 vray_material_id 1")
cmds
.setAttr('VRayMtl1.vrayMaterialId', 2)
# Error: RuntimeError: file <maya console>
# line 1: setAttr: No object matches name: VRayMtl1.vrayMaterialId

It seems like that the command to add the vray material attribute is not done before maya tries to set the attr id, and it cant find the attr.
I thought that the commands were executed in turn, can I add callback or can I somehow make sure that the attr is allowed to be created before we move on ?

Thanks,
Johan


Andrew Chan

unread,
Oct 28, 2013, 2:16:31 PM10/28/13
to python_in...@googlegroups.com
Hi Johan,

I have written a tool at work that creates material IDs, and multi-mattes of every shader in the scene. Are you on the latest version of Vray because we have no problems setting the attribute immediately afterwards. Maybe there is an issue with older versions of vray in maya.

I am using Maya 2012 and VRay 2.25.01.22904

Below is an example that I have successfully run on my end. You will just need to create a vray material named,  'VRayMtl1'

import maya.cmds as mc
import maya.mel as mm

shaderName = 'VRayMtl1'
ID = 1001
mm.eval('vray addAttributesFromGroup %s vray_material_id 1;' % shaderName)
mc.setAttr (('%s.vrayMaterialId' % shaderName), ID)

shapeofmotion

unread,
Oct 28, 2013, 3:25:43 PM10/28/13
to python_in...@googlegroups.com
Hi Andrew,

Thank you for your answer. I tried what you suggested but I still get the same errors.
I am trying this on my mac book, here are the specs:

V-Ray version: 2.30.02
Mac OSX 10.8.5
Maya 2014 Extension pack

I will try it at work tomorrow where I run win7, and Maya 2014 Extension pack, and post the results.

Best Regards,
Johan



Christopher Stewart

unread,
Oct 28, 2013, 6:41:21 PM10/28/13
to python_in...@googlegroups.com
I needed to use a lambda to have it work reliably (yes, an older version of V-Ray). I suppose now one would use partial:








--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
----
Christopher Stewart
Vancouver, BC
3D TD | VFX  IT

johan Borgström

unread,
Oct 30, 2013, 4:19:40 AM10/30/13
to python_in...@googlegroups.com
Hi Christopher!

Thanks for your answer, I tried to use partial but got the same error, so If you can show how you did it that would be great.

I looked through the echoed history of what happens when I manually add the vray material attr and found some lines of code that looked interesting. If I add the line "vrayAddAttr VRayMtl1 vrayMaterialId" I was able to set the material id without the error I described earlier.
Hmmm, not clear what that line does ... but somehow it make Maya aware that the attr exists :)

(Btw It looks like the line that contains  "vrayAddAttr VRayMtl1 vrayColorId" is not necessary)

Br,
johan


mat = 'VRayMtl1'
id
= 4
mel
.eval('vray addAttributesFromGroup {0} vray_material_id 1;'.format(mat))
#mel.eval('vrayAddAttr {0} vrayColorId;'.format(mat))
mel
.eval('vrayAddAttr {0} vrayMaterialId;'.format(mat))
cmds
.setAttr (('{0}.vrayMaterialId'.format(mat)), id)

Christopher Stewart

unread,
Oct 30, 2013, 2:52:38 PM10/30/13
to python_in...@googlegroups.com
All good, I need to fix up something here for objectID's anyway.

I'm a big fibber. I used evalDeferred (thanks to a far more savvy TD recommending it):


import maya.cmds as cmds
import maya.mel as mel

myObj = cmds.ls (sl=True, dag=True, type='shape')
myObj = myObj[0]
idNumber = 5

for obj in myObj:

    mel.eval('vray addAttributesFromGroup ' + myObj + ' vray_objectID 1')
    cmds.evalDeferred(lambda object=myObj,object_id=idNumber: cmds.setAttr(object + '.vrayObjectID', idNumber))


I used to loop over all the selected objects adding incremental ID's. Divide by three then create that many multimattes render elements. As long as I sorted the initial list properly (and the target's object count did not change), I could automate matte creation in a crude fashion. Same for Material ID's (though that tends to be tricker because of double-sided/blend material use and the like).

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

johan Borgström

unread,
Nov 4, 2013, 4:34:58 AM11/4/13
to python_in...@googlegroups.com
Hi,

I ended up using the snippet from my previous post. And it seems to work great. Thanks for all input. Here is the snippet for completion of the thread.
(Out of curiosity, is it possible to mark your own post as complete ?)

mat = 'VRayMtl1'
id
= 4
mel
.eval('vray addAttributesFromGroup {0} vray_material_id 1;'.format(mat))

mel
.eval('vrayAddAttr {0} vrayMaterialId;'.format(mat))
cmds
.setAttr (('{0}.vrayMaterialId'.format(mat)), id)

Best Regards,
Johan

Dillon B

unread,
Nov 12, 2013, 6:01:02 PM11/12/13
to python_in...@googlegroups.com
This is perfect!!! I have been trying to solve this problem by running some functions in line (procedurally) and having a call to all VRayMtls and adding vray_material_id to them all then executing another function to assign appropriate IDs to VRayMtls.

E.g.,

assignVrayMaterialIDs()
assignCustomIDsToMaterials()

but your solution using the format() method is soooo much better!!!

Thanks!
Dillon
Reply all
Reply to author
Forward
0 new messages