Object Location Change on Blender Python - python

I was following an animation example with Python in Blender 2.69, by typing a line by line.
obj = bpy.context.object
obj.location[2] = 0.0
obj.keyframe_insert(data_path="location", frame=10.0, index=2)
obj.location[2] = 1.0
obj.keyframe_insert(data_path="location", frame=20.0, index=2)
But I have encountered an error on the 3rd line, which is saying
Traceback (most recent call last):
File "<blender_console>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'location'
I am confused because I just followed a simple example.
Why is it saying the object has no attribute 'location'?
I'll be appreciated for your help, thanks.

You'll find that the error would be reported after the second line because the variable obj has not been set. Most likely this would be from a small typo.
You can verify this by looking at the type of the variable in the python console. When getting the error you will see -
>>> type(obj)
<class 'NoneType'>
While if it had been set correctly you will get -
>>> type(obj)
<class 'bpy_types.Object'>

Related

python-pcl Segmentation_PointXYZI' object has no attribute 'set_MaxIterations'

I'm new in C++. So, I'm trying to use python-pcl, but I got an error:
AttributeError: 'pcl._pcl.Segmentation_PointXYZI' object has no attribute 'set_MaxIterations'
I'm trying to create the segmentation object for the planar model and set the parameters with the PointXYZI type. I have to use PointXYZI. How can I solve this problem?
My code:
def cluster_extraction(self,data):
print("Type1: ", type(data))
cloud_filtered = self.downsampling(data,0.3)
print("Type2: ", type(cloud_filtered))
seg = cloud_filtered.make_segmenter()
seg.set_optimize_coefficients (True)
seg.set_model_type (pcl.SACMODEL_PLANE)
seg.set_method_type (pcl.SAC_RANSAC)
seg.set_MaxIterations (100)
seg.set_distance_threshold (0.02)
Output:
('Type1: ', <type 'pcl._pcl.PointCloud_PointXYZI'>)
('Type2: ', <type 'pcl._pcl.PointCloud_PointXYZI'>)
[ERROR] [1596926303.890116]: bad callback: <bound method sub_pub_node.callback of <__main__.sub_pub_node object at 0x7f154be44ad0>>
Traceback (most recent call last):
File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
cb(msg)
File "node.py", line 154, in callback
downsampled_data = self.processing(pcl2_data)
File "node.py", line 103, in processing
processing.cluster_extraction(pcl2_data)
File "node.py", line 43, in cluster_extraction
seg.set_MaxIterations (100)
AttributeError: 'pcl._pcl.Segmentation_PointXYZI' object has no attribute 'set_MaxIterations'
I am not sure from where you got your python-pcl package, but I will assume that you used this one, therefore, and because there is no method called set_MaxIterations(int ) in the Segmentation_PointXYZI class (Sample Consensus), you can try to replace it with setMaxIterations(int ).
Inside the definition of the PointCloud_PointXYZI class, you can find that the Segmentation method used for this type of point clouds is an instance from pcl_seg.SACSegmentation_PointXYZI_t which defines the method for setting the max number of iterations as setMaxIterations(int ).
Please check the documentation provided here and check the functions that you are using and how are they defined. (I know that can be tedious but it is necessary).
I hope this helped in solving the issue.
According to strawlab's official example the correct call is:
seg.set_max_iterations(100)

How to get a function's name as string?

In Python, how do I get a function's name as a string?
I want to get the name of the str.capitalize() function as a string. It appears that the function has a __name__ attribute. When I do
print str.__name__
I get this output, as expected:
str
But when I run str.capitalize().__name__ I get an error instead of getting the name "capitalize".
> Traceback (most recent call last):
> File "string_func.py", line 02, in <module>
> print str.capitalize().__name__
> TypeError: descriptor 'capitalize' of 'str' object needs an argument
Similarly,
greeting = 'hello, world'
print greeting.capitalize().__name__
gives this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute '__name__'
What went wrong?
greeting.capitalize is a function object, and that object has a .__name__ attribute that you can access. But greeting.capitalize() calls the function object and returns the capitalized version of the greeting string, and that string object doesn't have a .__name__ attribute. (But even if it did have a .__name__, it'd be the name of the string, not the name of the function used to create the string). And you can't do str.capitalize() because when you call the "raw" str.capitalize function you need to pass it a string argument that it can capitalize.
So you need to do
print str.capitalize.__name__
or
print greeting.capitalize.__name__
Let's start from the error
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'str' object has no attribute 'name'
Specific
AttributeError: 'str' object has no attribute 'name'
You are trying
greeting = 'hello, world'
print greeting.capitalize().__name__
Which will capitalize hello world and return it as a string.
As the error states, string don't have attribute _name_
capitalize() will execute the function immediately and use the result whereas capitalize will represent the function.
If you want to see a workaround in JavaScript,
Check the below snippet
function abc(){
return "hello world";
}
console.log(typeof abc); //function
console.log(typeof abc());
So, don't execute.
Simply use
greeting = 'hello, world'
print greeting.capitalize.__name__
You don't need to call this function and simply use name
>>> str.capitalize.__name__

why am I getting AttributeError: 'function' object has no attribute 'list'

ok so I'm trying to write this simple call request for YouTube channel data but it seems I'm still too much of a noob to fully understand what I'm doing wrong I understand that there is some type of syntax error but what I want to fully understand is why is there a syntax error so I can easily solve this issue in the future. I've spent too many hours trying to figure out the error here I know an experienced coder could solve this issue in a matter of minute so can someone help please.
Here is the full list of error terminal spits back
line 8, in channel_list_scrape
list_channel_attr = youtube.channels.list(id=youtube_channel).execute()
AttributeError: 'function' object has no attribute 'list'
line 11, in <module>
channel_list_scrape(youtube_channel = 'CNN')
Code:
from apiclient.discovery import build
import csv
def channel_list_scrape(youtube_channel):
DEVELOPER_KEY = 'string_would_go_in_here'
youtube = build('youtube', 'v3', developerKey=DEVELOPER_KEY)
list_channel_attr = youtube.channels.list(id=youtube_channel).execute()
return(list_channel_attr)
channel_list_scrape(youtube_channel = 'CNN')
I do not know the specifics of the api you are using, but from the traceback it sounds like you need to do something like this:
list_channel_attr = youtube.channels().list(id=youtube_channel).execute()
From what it it looks like, you want to call the list() method of whatever object is returned by youtube.channels(). What you are doing now is calling list() on the youtube.channels method object itself, rather than calling it on the object that method returns.
To further illustrate, observe the following interactive session with explanations in the comments:
In [1]: def foo(): # This function returns a list
...: return [1, 2, 3]
...:
...:
In [2]: [1, 2, 3].pop() # lists have a pop method
Out[2]: 3
In [3]: foo().pop() # so the return value of the function also has a pop method
Out[3]: 3
In [4]: foo.pop() # but the function itself does not have a pop method
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-4-20ec23cbc1ac> in <module>()
----> 1 foo.pop()
AttributeError: 'function' object has no attribute 'pop'

Pyephem: 'Titan' object has no attribute 'mag'

I tried to determine the magnitude of Titan, but the result is this error message:
AttributeError: 'Titan' object has no attribute 'mag'
>>> import ephem
>>> t = ephem.Titan()
>>> t.compute()
>>> t.ra
15:55:10.52
>>> t.mag
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Titan' object has no attribute 'mag'
Doesn't Titan have the attribute magnitude? Why? I can determine the magnitude for Uranus, or the Moon, but not for Titan. At least not with the 'mag' attribute.
What would be the way?
edit:
With versions 3.7.5.3 and 3.7.5.1 of ephem.
According to the PyEphem Homepage Docs
The ephem.Body type is the only type with a .mag attribute
Titan is classified as a ephem.PlanetMoon object and so does not have a .mag attribute
The current list of ephem.Body objects are:
Jupiter,Mars,Mercury,Moon,Neptune,Pluto,Saturn,Sun,Uranus,Venus.
I am not aware of any way in ephem to calculate the .mag of an ephem.PlanetMoon object

Python: TypeError: 'float' object is not callable

I am trying to join 2 strings using this code:
def __get_temp(self):
return float(self.ask('RS'))
def __set_temp(self, temp):
set = ('SS' + repr(temp))
stat = self.ask(set)
return self.check(stat)
temp = property(__get_temp, __set_temp)
Once together, I then send a signal over a serial bus using PyVisa. However, when I try to call the function, I get
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
chil.temp(13)
TypeError: 'float' object is not callable
I've tried looking around for explanation of this error, but none of them make any sense. Anyone know what is going on?
It looks like you are trying to set the property temp, but what you're actually doing is getting the property and then trying to call it as function with the parameter 13. The syntax for setting is:
chil.temp = 13

Categories