Import OBJ and smooth normals [MEL] Import MA UV merge

Import OBJ and smooth normals [MEL] Import MA UV merge

MayaNZ01
Enthusiast Enthusiast
945 Views
7 Replies
Message 1 of 8

Import OBJ and smooth normals [MEL] Import MA UV merge

MayaNZ01
Enthusiast
Enthusiast

Hi there

I have a MEL script a I use to import obj to maya and I'm wondering if it's possible to make it smooth the normals of the imported object since everything from zbrush has hard normals.

 

I also have a script to import .MA which maintains creases from zbrush however for some reason the .MA zbrush generates splits UV's by faces so it would be good to incorporate UV merge command unless there is an easier fix. If it's easier in python I'm not sure but I use maya 2018

 

file -import -type "OBJ" -mergeNamespacesOnClash true -options "mo=1" -loadReferenceDepth "none" "E:/WORK/obj.obj";

 

file -import -type "mayaAscii" -mergeNamespacesOnClash true -options "mo=1" -loadReferenceDepth "none" "E:/WORK/ma.ma";

 

0 Likes
Accepted solutions (1)
946 Views
7 Replies
Replies (7)
Message 2 of 8

Kahylan
Advisor
Advisor

Hi!

 

So for your MA problem, this script should help:

string $allGeo[] = `ls -typ "mesh"`;
file -import -type "mayaAscii" -mergeNamespacesOnClash true -options "mo=1" -loadReferenceDepth "none" "E:/WORK/MA.ma";
string $newAllGeo[] = `ls -typ "mesh"`;
string $newGeo[] = stringArrayRemove($newAllGeo,$allGeo);

for ($g in $newGeo){
    select -r $g;

    polyMergeUV -d 0.01 -ch 0;
}

if you need a bigger/smaller threshhold you can adjust the -d flag in the polyMergeUV command.

 

Now, I'm not quite sure what you mean by "smooth the normals", what command in the UI would you normally use for this? If I know that I probably wont have a problem coding that for you as well.

 

I hope it helps!

Message 3 of 8

MayaNZ01
Enthusiast
Enthusiast

Thanks for that, although it didn't seem to merge the UV's, but it did import. I will attach the example MA if you want to test it. Not sure if it's just my version of maya (2018) I did try adjusting the threshold

 

For the normals it's 'soften edge' under 'mesh display'

 

I'm also curious, I have one for fbx too but it doesn't import or overwrite objects if there are name clashes. Is there a workaround for that too?

 

file -import -type "FBX" -mergeNamespacesOnClash true -options "mo=1" -loadReferenceDepth "none" "E:/WORK/fbx.fbx";

 

Appreciate the help 

0 Likes
Message 4 of 8

MayaNZ01
Enthusiast
Enthusiast

I should note that if I select the imported object and run your command below, it does merge the verts correctly. I assume it's not selecting the object for some reason (I guess it doesn't consider it new geo). I use maya 2018 if that helps. It would also be good if it replaced name clashed objects on import with the imported one (that would probably mess up the logic of your stringarrayremove

 

 

 

polyMergeUV -d 0.01 -ch 0;

 

 

This also works if everything is imported, however it doesn't distinguish what's old and new like yours does

 

string $allGeo[] = `ls -typ "mesh"`;

for ($g in $allGeo){
    select -r $g;

    polyMergeUV -d 0.01 -ch 0;
}

 

 

Message 5 of 8

Kahylan
Advisor
Advisor
Accepted solution

Oh, I actually just messed up the order of my stringArrayRemove command so it basically removed the entire list, my bad, this should work:

string $allGeo[] = `ls -typ "mesh"`;
file -import -type "mayaAscii" -mergeNamespacesOnClash true -options "mo=1" -loadReferenceDepth "none" "E:/WORK/ma.ma";
string $newAllGeo[] = `ls -typ "mesh"`;
string $newGeo[] = stringArrayRemove($allGeo,$newAllGeo);

for ($g in $newGeo){
    select -r $g;

    polyMergeUV -d 0.01 -ch 0;
}

 

Now for the OBJ import its the same logic just with polySoftEdge instead of polyMergeUV

string $allGeo[] = `ls -typ "mesh"`;
file -import -type "OBJ" -mergeNamespacesOnClash true -options "mo=1" -loadReferenceDepth "none" "E:/WORK/obj.obj";
string $newAllGeo[] = `ls -typ "mesh"`;
string $newGeo[] = stringArrayRemove($allGeo,$newAllGeo);
for ($g in $newGeo){
    select -r $g;

    polySoftEdge -a 180;
}

 

The replacing of matching objects makes this more difficult, the file option has a setting that doesn't prohibits the import of matching nodes, but that is obvioulsy not what you want, since you need the new objects. And as you already realsied, it messes with the logic of the stringArrayRemove in my script so I will need to find another way to do it.

I'll give it a try later today.

 

I hope it helps!

Message 6 of 8

MayaNZ01
Enthusiast
Enthusiast

Thanks so much, I added in some mel that clears history and other things after the edge soften. I think the overwriting is happening before import, it's just a shame maya doesn't seem to have the option to overwrite with fbx import so it's probably too ambitious anyways

 

Appreciate the help and I've marked yours as the solution

0 Likes
Message 7 of 8

Kahylan
Advisor
Advisor

I tried implementing the automatic deletion of matching nodes. I switched to python since handling lists is just a lot easier in that language and it's just the language I'm more confortable with. I tested the scripts in Maya 2018 so it shouldn't be a problem.

 

I use a Temporary namespace so the nodes don't get renamed in any way and I can check for matching names, after the matching nodes are deleted the Temp namespace is as well.

 

ma:

 

 

import maya.cmds as mc

path= "E:/WORK/ma.ma"

check= 0
nameSpace= "TempNamespace"

while check ==0:
    if mc.namespace(ex= nameSpace)== True:
        nameSpace= nameSpace + "1"
    else:
        check = 1
        
all = mc.ls()
nn = mc.file(path,i = True ,typ =  "mayaAscii", mergeNamespacesOnClash= True, rdn=True, ra= True, rnn= True, ns= nameSpace, options = "mo=1" ,loadReferenceDepth = "none")

for n in nn:
    if mc.objExists(n) ==True:
        if mc.objectType(n)== "mesh":
            mc.select(n, r=True)
            mc.polyMergeUV(d=0.01, ch=0)
        if "initialShadingGroup" in n:
            mc.delete(n)
            nn.remove(n)
    else:
        nn.remove(n)
        
nd = mc.ls(ud=True)
for n in all:
    np = "|{0}:{1}".format(nameSpace,n)
    nwp = "{0}:{1}".format(nameSpace,n)
    if np in nn or nwp in nn:
        if n not in nd:
            try:
                mc.delete(n)
            except:
                pass

for n in nn:
    if mc.objExists(n) ==False:
        nn.remove(n)

mc.namespace(rm=nameSpace,mnr= True)

 

 

 

obj:

 

 

import maya.cmds as mc

path= "E:/WORK/obj.obj"

check= 0
nameSpace= "TempNamespace"

while check ==0:
    if mc.namespace(ex= nameSpace)== True:
        nameSpace= nameSpace + "1"
    else:
        check = 1
        
all = mc.ls()
nn = mc.file(path,i = True ,typ =  "OBJ", mergeNamespacesOnClash= True, rdn=True, ra= True, rnn= True, ns= nameSpace, options = "mo=1" ,loadReferenceDepth = "none")

for n in nn:
    if mc.objExists(n) ==True:
        if mc.objectType(n)== "mesh":
            mc.select(n, r=True)
            mc.polySoftEdge(a=180, ch=0)
        if "initialShadingGroup" in n:
            mc.delete(n)
            nn.remove(n)
    else:
        nn.remove(n)
        
for n in nn:
    if mc.objExists(n) ==False:
        nn.remove(n)
        
    
        
nd = mc.ls(ud=True)
for n in all:
    np = "|{0}:{1}".format(nameSpace,n)
    nwp = "{0}:{1}".format(nameSpace,n)
    if np in nn or nwp in nn:
        if n not in nd:
            mc.delete(n)

mc.namespace(rm=nameSpace,mnr= True)

 

 

 

I should mention that I had to single out the initialShadingGroup, where the new one is deleted as the one in the scene is not deletable, when deleting the shading group, the assosiated nodes get deleted as well which is why I needed another loop to remove them from the list.

 

I hope it helps!

Message 8 of 8

MayaNZ01
Enthusiast
Enthusiast

Both of these work great and will be really useful, I'll swap out the original mel ones. I really appreciate the help once again. Thank you