[Maya] Manipulate objects imported via Python?

import maya.cmds as cm
if cm.file(path, q=True, ex=True ) == True:
cm.file(path, i=True ) #i=import, r=reference

Have been doing some searching but not having much luck with the keywords I’ve been using. With the above snippet I’m able to import an object from a file such as .obj, it may consider several objects in the file. I’ve tried to assign the command to a variable but in my test I get a string like “mat_str_01”, there are two materials named “mat_str_02” in hypershade, 2 objects with material each. Imports of the same asset would be bad duplicating the materials, yet with references I’m getting errors when it tries to reference the same asset again? If I assign the command to a variable but instead of importing the object, reference it, I get the file path to the asset returned.

I’d like to be able to reference an external asset file, instance it and then reposition that asset instance(and all the objects that belong to that instance). I’m not sure how to select the imported/referenced instance as a whole, at first I thought it was assigned to the variable and tried to alter var.tx = value which of course returned ‘unicode’ object has no attribute ‘tx’.

Is there a good Maya/Python resource/site that anyone here recommends?

I’m on my phone so sorry if any typos occur.

Maya’s commands will be returning regular strings, or to be more precise indeed they are ‘unicode’ objects. By default the file command doesn’t return the newly created nodes, but there’s an argument for it ‘returnNewNodes’ or something.

Then the command will return a list of object named which you’ll be able to move around. In your case you’d only like to move the root top level nodes of the import. You could check each node with ‘listRelatives’ to see if it has a parent or not. Or you could possibly query it through the ‘ls’ command with the assemblies argument set to True. (Assemblied in the ls command is what maya refers to as being top level root nodes in the DAG)

To move the actual objects you’ll have to use the ‘xform’ or ‘move’ command. For these you’ll pass the name of the object as first argument. If instead you’d like a more object oriented approach that’s more pythonic you’ll have to use pymel. (Read up on the differences to ensure what’s best for your needs)

-Roy

[QUOTE=BigRoyNL;27061]I’m on my phone so sorry if any typos occur.

Maya’s commands will be returning regular strings, or to be more precise indeed they are ‘unicode’ objects. By default the file command doesn’t return the newly created nodes, but there’s an argument for it ‘returnNewNodes’ or something.

Then the command will return a list of object named which you’ll be able to move around. In your case you’d only like to move the root top level nodes of the import. You could check each node with ‘listRelatives’ to see if it has a parent or not. Or you could possibly query it through the ‘ls’ command with the assemblies argument set to True. (Assemblied in the ls command is what maya refers to as being top level root nodes in the DAG)

To move the actual objects you’ll have to use the ‘xform’ or ‘move’ command. For these you’ll pass the name of the object as first argument. If instead you’d like a more object oriented approach that’s more pythonic you’ll have to use pymel. (Read up on the differences to ensure what’s best for your needs)

-Roy[/QUOTE]

Cheers I’ll look into that. I’m not sure how I’d refer to the imported/reference objects names if I don’t know them? I’m importing from several thousand file paths of .obj files, the same path to an obj will be repeated a bit just with different translate/rotate values. I’ve heard pymel isn’t too great performance wise when dealing with large loops so it might not be a good choice for this? The obj files are extracted from a games files, another programmer has handled the conversion to obj. Might want to convert these to another format and import/reference the 2nd file format(fbx/ma/mb?) as I’m wanting to add some of the json properties to the assets attributes in maya. As I’m still dealing with a large amount of .obj assets will still need to process those via Python I think.

I’ll look into the advice you’ve given and return with what I come up with :slight_smile: Cheers.

You won’t be able to add references to non-maya files. However you can do this in two passes: first, bulk convert your .objs to .ma, and then secondly use those as library objects (you can also do some useful things in this step: for example, you could save each obj file twice, once as an editable asset and once as a proxy file that is collapsed and has no hierarchy and maybe standardized names or other useful features).

To get the imported objects, you can do find them by collecting all the assemblies (top level nodes) in the scene before and after the import. For example:


before = set(cmds.ls(assemblies=True))
cmds.file('filepath', import=True)
after  =set(cmds.ls(assemblies=True))
imported = after.difference(before)

1 Like

[QUOTE=Theodox;27065]You won’t be able to add references to non-maya files. However you can do this in two passes: first, bulk convert your .objs to .ma, and then secondly use those as library objects (you can also do some useful things in this step: for example, you could save each obj file twice, once as an editable asset and once as a proxy file that is collapsed and has no hierarchy and maybe standardized names or other useful features).

To get the imported objects, you can do find them by collecting all the assemblies (top level nodes) in the scene before and after the import. For example:


before = set(cmds.ls(assemblies=True))
cmds.file('filepath', import=True)
after  =set(cmds.ls(assemblies=True))
imported = after.difference(before)

[/QUOTE]

Will give that a go, currently I have the following:

import maya.cmds as cm

if cm.file(path, q=True, ex=True ) == True:
cm.file(path, r=True, typ=“FBX”, op=“fbx”, ns=“world”, gr=True, gn=unit_data[“name_id”], mnc=True, shd=“shadingNetworks”, rnn=True )

cm.select( unit_data[“name_id”], r=True ) #r=replace current selection
cm.xform( a=True, piv=[0,0,0], t=(ex.tx, ex.ty, ex.tz), ro=(ex.rx-90, ex.ry, ex.rz) ) #pivot is being centered on object when objects are grouped, pivot should be at the origin, -90 on rx to fix rotation issue from conversion

Previously was importing obj files which worked great for most however some of the obj files had data issues that was a pain to fix manually. The other developer batch converted these to FBX via a tool I think? They import fine apart from being offset 90deg on an angle for some reason. The oddity I’m confused with is that if I import/reference/open the fbx file, the material/textures are fine. But when I bring them in with the code above Maya can’t seem to figure out where the textures are. Although…if I’ve manually loaded the fbx previously, even if I’ve started a new scene, the code above will bring them in with textures working??? Pretty confusing…