Creating a Directional light with pymel through a saved json

Hi,

Some time ago I learned about Pymel/python programming in Maya making this light manager using a tutorial/online resources, and it’s been a while but I could never find a solution to this, I can create, save and import all lights except the directional light that cannot be imported, I think it has something to do with how Maya light type work but I cannot find online what I should put for directional light type, if you have any idea how to solve it or why it’s not working that would be great,

thank you,
PS:i couldn’t find a similar question but i apologize if i missed it!

here is my code/maya report and the json save file:

Json:
{
“AA”: {
“translate”: [
0.0,
0.0,
0.0
],
“rotation”: [
0.0,
0.0,
0.0
],
“lightType”: “areaLight”,
“intensity”: 1.0,
“color”: [
1.0,
1.0,
1.0
],
“name”: “AA”
},
“BB”: {
“translate”: [
0.0,
0.0,
0.0
],
“rotation”: [
0.0,
0.0,
0.0
],
“lightType”: “pointLight”,
“intensity”: 1.0,
“color”: [
1.0,
1.0,
1.0
],
“name”: “BB”
},
“CC”: {
“translate”: [
0.0,
0.0,
0.0
],
“rotation”: [
0.0,
0.0,
0.0
],
“lightType”: “spotLight”,
“intensity”: 1.0,
“color”: [
1.0,
1.0,
1.0
],
“name”: “CC”
},
“VV”: {
“translate”: [
0.0,
0.0,
0.0
],
“rotation”: [
0.0,
0.0,
0.0
],
“lightType”: “volumeLight”,
“intensity”: 1.0,
“color”: [
1.0,
1.0,
1.0
],
“name”: “VV”
},
“RR”: {
“translate”: [
0.0,
0.0,
0.0
],
“rotation”: [
0.0,
0.0,
0.0
],
“lightType”: “directionalLight”,
“intensity”: 1.0,
“color”: [
1.0,
1.0,
1.0
],
“name”: “RR”
}
}

forget about PyMEL “PyMEL’s dead”.
use maya.cmds instead:

import maya.cmds as cmds

cmds.CreateDirectionalLight

I guess it would work with cmds but then do you have any website or knowledge to direct me on how to read a Json file to change the attribute of the created light?? i save the light’s type and attribute and on import read the file but I have no idea on how to do that without pymel

thank you

do you know how to do this with MEL? Well, it’s almost the same with cmds…

for attr in cmds.listAttr("directionalLight1"):
    try:
        print(attr, " ", cmds.getAttr("directionalLight1." + attr))
    except:
        print("Error reading data")

To develop for Maya, the first thing you need to know is MEL. Sometimes, that’s even enough. :slight_smile:

actually i learned the veeeeery basics of Mel only so I’ll have to research a bit but I’ll try to make it work with the rest of the code, thanks for the tip :slight_smile:

that’s good to know!
if you have a recommended website/youtube place to start learning i’m interested

the best and most helpful MEL leaning resource is the old-school MEL help :sunglasses:

that makes sense :wink: , I’ll leave the subject open for a bit if someone has the solution with Pymel cause I’m still curious what I should have done for it to work with it, also If i cant figure out how to do it in with cmds I’ll ask again :slight_smile:

My guess is that line 53 creates issue, when you’re creating LightTypes class attribute:

"Direction Light": partial(pm.shadingNode, 'directionalLight, asLight=True),

since that, in conjuction with line 176:

if ('%sLight' % lt.split()[0].lower() == lightType:

gives you false comparison. You compare ‘directionLight’ with lightType you get from JSON file, instead of ‘directionalLight’.

Just fix line 53 to (Directional instead of Direction):

"Directional Light": partial(pm.shadingNode, 'directionalLight, asLight=True),

and you should be fine (unless there are more errors, which is pretty hard to guess without actual code that we could copy/paste and try it; hardly anyone will type manually all those lines; so, next time, you should probably, instead of screenshot-ing code, paste it in some textual form, rather than attaching images).

P.S. Also, there’s wrapper for creating directional light too in pymel, so you could directly make that type of light using pm.directionalLight() method, like you do for point and spot lights.

IT WORKS! such a stupid mistake but I feel like those ones are the ones that are harder to find for me, next time I’ll copy paste the code yeah :slight_smile:
Thanks a lot!

I knew about the wrapper but thought there was something wrong with it so decided not to use it :sweat_smile: