I have just recently started writing Python code to be used in Maya.
My code looks like this:
import maya.cmds as cmds;
import random as rand;
for x in range (0,10):
cmds.polyCube(cmds.translateX == rand(0,100));
x += 1;
Maya then gives the error, 'module' has no attribute 'translateX'
I am not sure what is going on. Thank you!
translateX isn't a command or part of the argument for a polyCube.
What you want is something more like
import maya.cmds as cmds;
import random as rand;
for x in range (10):
# create the cube at origin
cmds.polyCube()
# move it to a random X
cmds.xform( t = (rand.randrange(0,100), 0, 0) )
When you create the polyCube it will be selected, so issuing the xform() immediately afterward will only affect the most recently created cube. You could also use cmds.setAttr(".translateX" = rand(0,100)) but that's less flexible if you also want to set the Y or Z directions
Related
I am new to python and I am trying to do a simple script to find all the floatConstant nodes in the Hypershade named "rangeImput#" and update all the values at once.
However, it returns this error: 'NoneType' object is not iterable #
The funny thing is; if I create the script to change what is selected manually it works, but selecting the node by its name doesn't.
Any help is much appreciated.
from maya import cmds
selection = cmds.select("*rangeImput*", allDagObjects=False, noExpand=True)
newRange= 30
for x in selection:
cmds.setAttr (x +".inFloat", newRange)
select just marks the objects as selected and returns None (Maya docs). Try this:
from maya import cmds
# mark objects as selected
cmds.select("*rangeImput*", allDagObjects=False, noExpand=True)
# get selected objects
selected_objects = cmds.ls( selection = True )
newRange = 30
for x in selected_objects:
cmds.setAttr (x +".inFloat", newRange)
I am looking the way How to create Entanglement, Superposition and interference? Can someone help to show how to code in qiskit? and the docs related to these mentioned above?
Thanks
Here you go
from qiskit import * # importing qiskit
# instead of importing everything you could import things separately
def circ_superposition(number):
# raising errors
try:
number = int(number)
except:
return "Number must be an integer"
circ = QuantumCircuit(number, number) # creating the circuit
for i in range(number):
circ.h(i)
return circ
You might want to use the below to see the circuit
print(f'{circ_superposition(6)}') # add any integer in place of 6
and to create entanglement do the following
entanglement = QuantumCircuit(n, n) # replace n by any integer
# apply h gates the qubits
for i in range(n): # replace n by any integer
entanglment.h(i)
# apply cx gate
entanglement.cx(control, target) # replace control and target by control qubit and target qubit respectively
from further assistance refer to:
Circuit Basics
And Getting Stared With Qiskit
Also consider visiting:
Super Dense Coding And Quantum Teleportation for applications of Quantum Interference.
I'm fairly new to OOP in python and i cant seem to figure out if I'm doing this the right way. I have a first script called TP2_dat.py in which I solve a problem in AMPL and then at the end i retrieve the variable X as follows (I simplified the code):
import amplpy
import os
class Solver:
def __init__(self, set_k, larg_on, poids):
self.set_K = set_k
self.larg_on = larg_on
self.poids = poids
def solve(self):
...
X = ampl.getVariable('X')
X_val = X.getValues()
X_val_dic = X_val.toDict()
# print(X_val_dic)
return X_val_dic
I tried to do a print instead of a return from the first script and it worked. But now i want to print it from another script as follows so i dont get the None at the end of the print from the second script:
from TP2_dat import Solver
set_K = [1,2,3,4,5,6]
larg_on = [4,3,5,4,6,5]
poids = [0,4,5,2,3,6,4,0,4,3,5,3,5,4,0,4,7,8,2,3,4,0,3,3,3,5,7,3,0,5,6,3,8,3,5,0]
affichage = Solver(set_K, larg_on, poids)
affichage.solve()
print(X_val_dic)
The part that i dont understand is can i do something similar to this? Am i right in thinking to return the value of X_val_dic or should i do something else?
Thanks a lot for your help !!
Since the class method solve returns a value you need to set it equal to a variable or print it directly:
X_val_dic = affichage.solve()
print(X_val_dic)
or
print(affichage.solve())
See this article for more information on pythons variable scoping.
I wrote some code (named exercise 2) where I define a function (named is_divisible) and it has worked perfectly.
Afterwards to learn how to import functions, I wrote the same code but without the defined function, and created a second module (named is_divisible). But whenever I import this module into the original "exercise 2" I get
No module named 'is_divisible'
I have checked that both python files are in the same folder, the name of the file is correct, and I know the code is well written because it has worked before and it is from a lecturer's of mine. I have also attempted to name the module and the function differently and to instead write:
from divis import is_divisible
but this was also unsuccessful.
Where am I going wrong? I will leave the code below:
import random
import math
import numpy as np
random_list=[]
for i in range (0,5):
r=random.randint(0,10)
random_list.append(r)
print(random_list) #five numbers from 0 to 10 are chosen and appended to a list
new_result=[print('right' for x in random_list if round(np.cosh(x)**2 - np.sinh(x)**2,2) == 1]
#checking the numbers following a maths rule
import is_divisible #trying to import the function is_divisible
divisor=3
idx = is_divisible(random_list, divisor)
for i in idx:
print(f'Value {random_list[i]} (at index {i}) is divisible by {divisor}')
the code for the function is_divisible is:
def is_divisible(x, n):
""" Find the indices of x where the element is exactly divisible by n.
Arguments:
x - list of numbers to test
n - single divisor
Returns a list of the indices of x for which the value of the element is
divisible by n (to a precision of 1e-6 in the case of floats).
Example:
>>> is_divisible([3, 1, 3.1415, 6, 7.5], 3)
[0, 3]
"""
r = []
small = 1e-6
for i, m in enumerate(x):
if m % n < small:
r.append(i)
return r
I know this question has been answered multiple times, but none of the answers seem to work for me or maybe I am not doing it correctly.
Generally, when you type import <Module> the module is the name of the file. So, if you had the function is_divisible inside a Python file named a.py, then to import it you will write from a import is_divisible. If instead, you would like to import the whole file, then you'd write import a.py, then to use the function you would use a.is_divisible(random_list, divisor).
You should also make sure that both files are in the same folder.
I have 10 objects, I would like to place these objects randomly within a range x, y, z (with minimum and maximum). Something like that:
import bpy
import bpy_extras
import random
import math
bpy.data.objects["Sasso_1"].select_set(True)
bpy.ops.rigidbody.objects_add(type='ACTIVE')
bpy.context.object.rigid_body.mass = 0.25
for obj in bpy.data.collections['Sassi'].all_objects:
obj.select_set(True)
bpy.ops.rigidbody.object_settings_copy()
bpy.ops.transform.translate(value=random.uniform(12,27), orient_axis='X')
bpy.ops.transform.translate(value=random.uniform(-15,15), orient_axis='Y')
bpy.ops.transform.translate(value=random.uniform(13,28), orient_axis='Z')
for obj in bpy.data.collections['Sassi'].all_objects:
bpy.ops.transform.rotate(value=random.uniform(0,360), orient_axis='X')
bpy.ops.transform.rotate(value=random.uniform(0,360), orient_axis='Y')
In my script, the objects "sassi" rotate and translate in a random way, but not in a range that I want.
The result is that the objects are unpredictable.
Tnx for the attention. :)
The translate operator does a relative movement:
https://docs.blender.org/api/blender_python_api_2_67_1/bpy.ops.transform.html
In your case you want to set an absolute position. How to do that is explained here:
https://blender.stackexchange.com/a/89581
bpy.data.objects["Cube"].location.x = 3.0
bpy.data.objects["Cube"].location.y = 2.0
bpy.data.objects["Cube"].location.z = 1.0
bpy.context.scene.update()
I resolve the problem with the randomize trasform function.
this is the code for do it:
bpy.ops.object.randomize_transform(random_seed = random.randint(0,100), loc=(6, 6, 6), scale=(1, 1, 1))