Maya Python polySeparate error - python

import maya.cmds as cmds
cmds.polyChipOff(ltz=0.1, kft=False)
cmds.polySeparate()
When running the polySeparate command, I get the error "polySeparate needs exactly 1 polygonal obkect or polygonal faces from one object" despite having a face duplicated and selected.
Total noob here, wondering why this is the case?

Looks like you need to pass an object to the polySeparate function.
Since you already have a face selected you can use cmds.ls to get the selected object:
import maya.cmds as cmds
cmds.polyChipOff(ltz=0.1, kft=False)
cmds.polySeparate(cmds.ls(sl=True, objectsOnly=True)[0])
Works as expected when testing with new objects.

Related

Toggle X-Ray Mode in Maya using Python

I'm trying to bind some Python code to a key so I can toggle X-Ray mode in Maya.
One thing that's confusing me is that when I run this line of code...
def xrayQuery():
cmds.modelEditor('modelPanel4', q=True, xr=True)
xrayQuery()
no result is returned, even though I've queried xray mode. But when I run just the command without the function...
cmds.modelEditor('modelPanel4', q=True, xr=True)
I get what I expected the first time, which is a boolean result based on whether or not xray mode is enabled. Can anyone tell me why this is?
I'm very new to python inside Maya, so any help will be much appreciated! Thanks!
You need to call return if you want the user-defined function to return the same output as called inside.
Like below:
def xrayQuery():
return cmds.modelEditor('modelPanel4', q=True, xr=True)
On a side note, if you could explain the purpose to write a function instead of calling the original function, it would be helpful to understand the use-case
So I've figured out a way to simplify what I was trying to achieve, which was a few lines of code to toggle the x-ray view mode on and off for a specific viewport. I was able to eliminate the need for if else statements by using the 'not' operator in this block of code:
import maya.cmds as cmds
def xray_toggle():
result = cmds.modelEditor('modelPanel4', q=True, xr=True)
cmds.modelEditor('modelPanel4', e=True, xr=not result)
xray_toggle()

Getting a selection in 3ds Max into a list in Python

I am writing in Python, sometimes calling certain aspects of maxscript and I have gotten most of the basics to work. However, I still don't understand FPValues. I don't even understand while looking through the examples and the max help site how to get anything meaningful out of them. For example:
import MaxPlus as MP
import pymxs
MPEval = MP.Core.EvalMAXScript
objectList = []
def addBtnCheck():
select = MPEval('''GetCurrentSelection()''')
objectList.append(select)
print(objectList)
MPEval('''
try (destroyDialog unnamedRollout) catch()
rollout unnamedRollout "Centered" width:262 height:350
(
button 'addBtn' "Add Selection to List" pos:[16,24] width:88 height:38
align:#left
on 'addBtn' pressed do
(
python.Execute "addBtnCheck()"
)
)
''')
MP.Core.EvalMAXScript('''createDialog unnamedRollout''')
(I hope I got the indentation right, pretty new at this)
In the above code I successfully spawned my rollout, and used a button press to call a python function and then I try to put the selection of a group of objects in a variable that I can control through python.
The objectList print gives me this:
[<MaxPlus.FPValue; proxy of <Swig Object of type 'Autodesk::Max::FPValue *' at 0x00000000846E5F00> >]
When used on a selection of two objects. While I would like the object names, their positions, etc!
If anybody can point me in the right direction, or explain FPValues and how to use them like I am an actual five year old, I would be eternally grateful!
Where to start, to me the main issue seems to be the way you're approaching it:
why use MaxPlus at all, that's an low-level SDK wrapper as unpythonic (and incomplete) as it gets
why call maxscript from python for things that can be done in python (getCurrentSelection)
why use maxscript to create UI, you're in python, use pySide
if you can do it in maxscript, why would you do it in python in the first place? Aside from faster math ops, most of the scene operations will be orders of magnitude slower in python. And if you want, you can import and use python modules in maxscript, too.
import MaxPlus as MP
import pymxs
mySel = mp.SelectionManager.Nodes
objectList = []
for each in mySel:
x = each.Name
objectList.append(x)
print objectList
The easiest way I know is with the
my_selection = rt.selection
command...
However, I've found it works a little better for me to throw it into a list() function as well so I can get it as a Python list instead of a MAXscript array. This isn't required but some things get weird when using the default return from rt.selection.
my_selection = list(rt.selection)
Once you have the objects in a list you can just access its attributes by looking up what its called for MAXscript.
for obj in my_selection:
print(obj.name)

Maya/Arnold scripting, rendering ambient occlusion to the map using arnold utilities

I using the following action to generate ambient occlusion maps for models in maya:
Create and assign aiAmbientOcclusion to my model (the one I want to generate oa maps for).
Then, I go Arnold>Utilities>Render Selection To Texture.
Since this process is always the same I want to write a python script to automate it unfortunately I haven't found many useful examples about writing scripts for Arnold.
To add this functionality I must:
import mtoa.renderToTexture
that script is located in
the_way_to_my_install_folder/solidangle/mtoa/2017/scripts/mtoa
I saw that the script defines the class MtoARenderToTexture and I should pass an object to it. Now.
What kind of object I mush use and is there some sort of documentation for MtoARenderToTexture class?
I was able to do what I wanted using ether this tutorial and extending MtoARenderToTexture class.
I do not iclude all my scripts that load scene and manage scenes files as they very specific to my needs, but still think it's a good idea to share some very basic and fundamental elements that may be useful for some new entries as myself.
This is how my extended class looks like
import mtoa.renderToTexture as renderToTexture
import maya.cmds as cmds
class rkMtoaRtoT(renderToTexture.MtoARenderToTexture):
def __init__(self):
renderToTexture.MtoARenderToTexture.__init__(self)
self.dFolder = '~'
self.dResolution = 1024
self.dCameraSamples = 5
def doAutomaticExport(self):
renderToTexture.MtoARenderToTexture.create(self)
cmds.textFieldButtonGrp('outputFolder', e=True, tx=self.dFolder)
cmds.intFieldGrp('resolution', e=True, v1=self.dResolution)
cmds.intFieldGrp('aa_samples', e=True, v1=self.dCameraSamples)
renderToTexture.MtoARenderToTexture.doExport(self)

Unbind skin in Autodesk Maya

Despite following the Maya command documentation shown here:
import maya.cmds as cmds
cmds.bindSkin( unbind=True ) # While my object is selected.
or
cmds.bindSkin( 'mySelectedObj', unbind=True ) # Specifying which object to unbind
results in:
Error: RuntimeError: file line 1: No skin partition
found in scene.
I'm also getting the same error in MEL. But the script editor's history shows a doDetachSkin command - and searching on it just keeps leading me back to the bind skin command.
How should I correctly write this command when following the example on the documentation is giving me the error message?
P.S.: My selection is a geo mesh that is skinned to a few joints.
Did you tried with selection ?
mySelectedObj = cmds.ls(sl=True) or []
if mySelectedObj:
cmds.bindSkin(mySelectedObj[0], unbind=True )
import maya.mel as mel
skinC = mel.eval('doDetachSkin "2" { "1","1" }')
Decided to work around the issue instead by just invoking the mel command that I see in the script editor history. Not ideal but serves my purposes for now.
If anyone knows of a better way or can clue in why following the documentation isn't working, please feel free to chime in.
If Unbind Skin Python command doesn't work:
import maya.cmds as cmds
cmds.bindSkin( unbind=True, bp=False )
Try its great old MEL equivalent:
DetachSkin;
It looks like bindSkin can only delete the jointCluster, and you can try skinCluster.
import pymel.core as pm
pm.skinCluster(objname, edit=True, unbind=True)enter code here

# Error: ImportError: No module named samples.lightIntensity #

It's my script:
import maya.cmds as cmds
def changeLightIntensity(percentage=1.0):
"""
Changes the intensity of each light the scene by percentage.
Parameters:
percentage - Percentage to change each light's intensity. Default value is 1.
Returns:
Nothing.
"""
#The is command is the list command. It is used to list various nodes
#in the current scene. You can also use it to list selected nodes.
lightInScene = cmds.ls(type='light')
#If there are no lights in the scene, there is no point running this script
if not lightInScene:
raise RunTimeError, 'There are no lights in the scene!'
#Loop through each light
for light in lightInScene:
#The getAttr command is used to get attribute values of a node
currentIntensity = cmds.getAttr('%s.intensity' % light)
#Calculate a new intensity
newIntensity = currentIntensity * percentage
#Change the lights intensity to the new intensity
cmds.setAttr('%s.intensity' % light, newIntensity)
#Report to the user what we just did
print 'Changed the intensity of light %s from %.3f to %.3f' % (light, currentIntensity, newIntensity)
import samples.lightIntensity as lightIntensity
lightIntensity.changelightIntensity(1.2)
My error's:
ImportError: No module named samples.lightIntensity
What's the problem? Can I do to this? What's the solution?
Thanks!
It seems you're following this tutorial. What you've misunderstood is that the last two lines in your code sample are not supposed to be part of the script, but they are meant to run the previous code in the interpreter. If you take another look at the tutorial, you'll see that there's a header above the main code sample saying lightIntensity.py, while the second, smaller code sample is preceded by "To run this script, in the script editor type..."
So, this:
import samples.lightIntensity as lightIntensity
lightIntensity.changelightIntensity(1.2)
should not be in your file in that form.
There are two things you could do. Both should solve the problem and allow you to run the code, though I would prefer the second for ease of use.
Save the code without the last two lines as lightIntensity.py, and in the python shell, (start python, on the command line, IDLE, whatever you're using), and after the prompt, type import lightIntensity to import your script, and lightIntensity.changelightIntensity(1.2) to call the function in the script.
Alternatively, you can fix the script so that it runs without trying to import itself. To do so, remove the import line and change the last line to changelightIntensity(1.2)

Categories

Resources