How should one start learning scripting?

def something():
print I keep spending hours of trying figure out basics but i cannot get anywhere at learning to write python that is usable in doing different kind of blender projects. How should i start, all i can get done with python now is a headache, a good headache, its stable and never crashes

something()

edit>
Here is something for another noob

in blender´s text editor
write:

def simple():
print “this is simple”

simple()

then file>run python script <- from text editor menu.
and check the console, its the other blender window which is minimized.
There should be text: this is simple

There are THAT many posts like this on the forum you do well to use the search function.

  • Re-Request a sticky thread for this :wink:

Hopefully, Stivs Summer of Doc projects will answer your problem.
http://mediawiki.blender.org/index.php/Blender_Summer_of_Documentation

In the meanwhile, what about discussing here some bit of code that you are attempting to make work so problems could be identified and solutions found ?
I’d love to help you this way.

@Campbell
Your are right, this should be a sticky. But it’d need content and it is my guess that nobody has come around with some. Would you mind to start it of leave bread crumbs for others less competent to follow ?

I will try leave bread crumbs to follow so here is few steps:
here is place to load some python stuff:
http://wiki.python.org/moin/BeginnersGuide/Download
ones I got that i started the python shell and started
testing out following stuff with some www site help:
1+1 E press enter=E
2
x=5 E
y=7 E
x+y E
12
_ E prints the the answer for the last calcution=_
12

I am stuck at trying to generate a random number and generating
of count down or count up. Actually i did it couple years ago but gave
up since i did not get modules to function well.

>>> def dice():
import random
print randint(1,6)

<-that makes this line a comment

is random the module needed, does this need a math module, how to end
the dice scripting?

I will try leave bread crumbs to follow so here is few steps:
here is place to load some python stuff:
http://wiki.python.org/moin/BeginnersGuide/Download

This page tells you how to download and install Python.

Notice that you do not need a Python install for Blender as a minimal Python comes with it. If you use the on board text editor you can already program and make Blender scripts work.
This being said a full install is practical for learning.
Be careful that it’ll be the same version than that against which your installed Blender was compiled : version 2.3 or 2.4

You’ll find more tutorial help on this page though :
http://wiki.python.org/moin/BeginnersGuide/NonProgrammers?highlight=%28BeginnersGuide%2F%29

ones I got that i started the python shell and started
testing out following stuff with some www site help:
1+1 E press enter=E
2
x=5 E
y=7 E
x+y E
12
_ E prints the the answer for the last calculation=_
12

OK… this tells me that you are at the very beginning of your apprenticeship.
There are two mode in Python : there you were using the interactive mode. When you load a script and run it you are in program mode.

You need to read along a good basic tutorial :
which to choose is a matter of preference but have a look at those you can find under the link I suggested above and choose one.

I have no objection at answering you questions about general Python here but we must recognize that this is not the purpose of this forum. PM me at will though.
The best help a beginner can find help is form the tutor mailing list :
http://mail.python.org/mailman/listinfo/tutor

I am stuck at trying to generate a random number and generating
of count down or count up. Actually i did it couple years ago but gave
up since i did not get modules to function well.

(I’ll explain it to you but there are serious chances that you won’t understand much of what I’ll say : please read along anyway until I come back to this problem at the end.)

You need to load a module to generate random numbers.

In interactive mode, you would write :
>> import random
or
>> from random import *
The first from forces you to use fully qualified names for the functions in the ‘random’ module.
For example, the random() function in that module would have to be called by writing :
>> r = random.random()
which is: the variable ‘r’ will point to the value returned by theNameOfTheModule(dot operator)theNameOfTheFunction
The second for let you write:
>> r = random()
In this case there is a chance for names clashes if there is another ‘random()’ function in your program, one you may have programmed or which came from another imported module using *

If you didn’t get any or parts of what I wrote then you should be convinced that you need to read along and at full at least one of the beginner’s tutorial. Wait for Stivs’ if you need it to be Blender related to understand ; otherwise choose one in the list.

how to end
the dice scripting?

What is ‘dice scripting’ ? Never read that expression before.

I’m looking forward to seeing Stivs Summer of Doc, I’m sure it will explain a lot for many who fumble about this forum like a blind mime. Anyway I’ll add a little more, I’m just learning to use Python aswell and I have no programming experience whatsoever. I can do a few simple things, and have a few ideas on what I want to create with Python- I think of the maths involved and how the gui might look etc. Although I won’t be producing anything worth celebrating about anytime soon.


When learning you really should read over scripts, scripts that do similar things to what you may want to do. A few scripters have notes and comments in their scripts which make things a little clearer. Modify the code a little so you can see what your changes do.


Do a google search for Stani’s Python Editor - In this I copy and paste python and add ‘def’ comments into a ‘dead script’ - that way I can quickly search for what I want. This editor does a lot more, I’m still learning my way around it. It has special Blender functions too, but yeah …

Ripstings Gui Designer- Make GUI for your scripts easily. You actually feel like you are getting somewhere when you have a gui!


Hope that might help you move a long a little further.

I’m a total amateur- I think a sticky thread would be awesome and much appreciated by us fumblers, maybe a little workshop type thing, or a few short script examples with thorough notes. (just an idea, not a request)
Although there is plenty of info to read through on the web.

Thanks for posting and helping out.

Hi miffwhite
Thanks for posting. And thanks again Shiftingclouds.
Here is some bread crumbs. I will write see about writing a simple program
that uses this stuff when i have taken a break from learning python.

i am worn out at this moment

while J is learning python:

J leaves bread crumbs

if J is not learning python:

J is smoking a cigarette or he has gone to jogging

#J is trying to quit smoking

elif J seems to be not at blenderartist.org:

J has gone to a summer cottage where he has to make lot of firewood

elif J desides not to make the firewood before next winter:

J did a month´s work and it was wasted since he did not complete

making the firewood and the wood got ruined.

else:

#in here could be written what if J was not doing anything of above.

edit added>

print “i have a guestion for you.”
x = raw_input(“what do you like to drink?”)
if x == “coffee”:
print “coffee can wake one up, you can loose sleep if you drink it at night”
elif x== “milk”:
print “milk is good for you”
elif x== “juice”:
print “If its pure its got lot of vitamines”
else:
print “i don´t know what that drink is”

some explanations:

== <- equal, not equal would be -> != or <>

“hi” <- word hi there is a string. Variable is for example x in x=1

x = raw_input(“some text, maybe question?”) raw_input for string and to do that but with numbers you would do this for example: x = input(“a string in here”)

How about…


 
print "i have a guestion for you."
x = raw_input("what do you like to drink?")
if x is "coffee":
print x, " can wake one up, you can loose sleep if you drink it at night"
elif x is "milk":
print x, " is good for you"
elif x is "juice":
print "If its pure its got lot of vitamines"
else:
print "i don´t know what that drink is"
 

All I did was
Have the variable print out as well.
And change “==” to “is”
“is” is faster than “==” but will only work if both values (the thing on the left and the right) are exactly the same type and value. Check cambo’s website for the exact how and why (He told me about it this weekend).

hi, that is better definitely. and thanks for -> is
I will go back to earlier thing a bit.

countdown

x = 1000
while x > 0:
print x
x = x - 1

or, just to give an alternative (diffent comparison in the while loop and other subtraction method):


# countdown
x = 1000
while not x is 0:
print x
x -= 1

It works, but i do not get the logic why it works.

x -= 1

#bread crumbs for other python noobs
#michellaneous,      in case you are using idle for writing python 
#save your document
#documentname.py   and , you will
#get colored code.
#It makes it easier
#to see if code works
# def = define a function
# most clear example of using functions i came up with is this volume function
d = input("depth of a box?")
w = input("width of a box?")
h = input("height of a box?")
def volumeofabox(depth,width,height):
    return depth*width*height
print "volume of the box is:",volumeofabox(d,w,h)

This is a decrementing shortcut.
There is no logic since it is just a syntactic convention, in other words, an agreement on an alternative way to write an instruction.
It has the same result as :
x = x - 1

In other words :

x -= 1 means : “take the value that the variable x holds and subtract 1 from it ; then re-assing the result to x”

e.g.
If x hold (or ‘point to’) an integer, let’s say 90
then after
x -= 1
x = 89.

Actually I learnt a new trick from cambo, this works even better.


# countdown
x = 1000
while x:
print x
x -= 1

This will only work if x holds an int (meaning a normal nr 1 or 2, not a float 1.1, 2.0, or text), apparently if then x is 0 that is the same as x is False and if x is not 0 that is the same as x is True.

@Shiftingclauds I got it now, thanks.

@Macouno That is real confusing.

Bread crumbs. I went back to earlier thing a again, the dice:

 
import random
keyinput = 1
t = 1
q = 0
while keyinput is 1: 
      print "type t and press enter to through a dice."
      keyinput = input("type q and press enter to quit the program:")
      if keyinput is 1:
       list = ["zero","one","two","three","four","five","six"]
       x = random.randint(1,6)
       print "you got",list[x]
      elif keyinput is 0:
       pass


Import random

Imports the random module
at later point in the program

x = random.randint(1,6)

assigns to x value of 1,2,3,4,5 or 6.
following lines convert numbers to words using list


list = ["zero","one","two","three","four","five","six"]
print "you got",list[x]

Keyinput variable is set to 1 so that that these lines run at start up


print "type t and press enter to through a dice."
keyinput = input("type q and press enter to quit the program:")

this exits the program if q is entered and enter is pressed.


elif keyinput is 0:
  pass

J: just think like this…

When x is 0 that means it is false so the while loop will terminate, in all other cases x is true (it is something other than 0) so the loop will continue.

So when you do…
while x:

That loop will continue untill x is 0

Not really.

Try this :


x = 0
print x is 0

You should get ‘True’ as an output.

J.

Bread crumbs. I went back to earlier thing a again, the dice:

import random
keyinput = 1
t = 1
q = 0
while keyinput is 1:
print “type t and press enter to through a dice.”
keyinput = input(“type q and press enter to quit the program:”)
if keyinput is 1:
list = [“zero”,“one”,“two”,“three”,“four”,“five”,“six”]
x = random.randint(1,6)
print “you got”,list
elif keyinput is 0:
pass

Hey, I learned a few things, simply because your taking paths that are unusual to me. This program has a nice interaction with the user.

Allow me some remarks if you will :

  • you indentation is irregular, 6 spaces after ‘while’ and only one after ‘if’. This is something I didn’t know one could do so I learned something. However, I prefer to stick to a regular pattern of 4 spaces for indentation. I find that it imporves the readability of the code

  • the last 2 lines aren’t needed :


       elif keyinput is 0:
        pass

Whenever keyinput will be different from 1 the loop will be exited.

  • your use of ‘is’ is something new to me. I mean I knew the operator ‘is’ existed and I use it for tests like :
if a is in list:

but I’d rather tend to replace it by ‘==’ in your program. It is more standard through out most programming languages that I know.

Regards

Jean

ShiftingClouds, Don’t be nasty… I didn’t mean “x is 0” as in in the code… there were no code tags… I wrote an explanation without code in the actual explanation… You can’t assume that any time anyone writes “is” they mean the code… I meant the word.

If you really want to try what I meant try this:


x = 0
 
print "is x true? ", x == True
print "or is x flase? ", x == False

But I really didn’t want to go into it this deeply.

I am sorry that you are judging me so harshly.
I just wanted something to be clear that wasn’t at all, whatever you may say.

J.


&gt;&gt;&gt; print 0 == True
False
&gt;&gt;&gt; print 0 == False
True

so number zero is equal to false. Yesterday i got into conclusion when testing that the zero would be equal to true and negative one number would end the while command, but now i am sure zero is equal to false.

@Shiftingclouds
I will try to remember and do the four spaces indentations.
Actually i will do it rightaway.

breadcrumbs
The following is something i wanted to make about week ago but after trying about seven different methods to do it i concluded that i needed to learn more new code to do it. Last night i learned little of code that made it work. Its pingpong, well i call this program pingpong.


a = 0
def up():
    for x in range(1,11):
        print x
def down():
    for x in range(1,9):
        print 10-x
        looptimes = input("Enter number of number loops:")
while a &lt; looptimes:
    a = a + 1
    up()
    down()