Mushroom creator

This is my first script,
it’s a mushroom creator…

Have fun !


#############################################################
##
##  _mushroom_ 0.12
##  
##  _Jimmy Haze_  24/9/2003
##
#############################################################

import Blender
from Blender import *
from Blender.Draw import *
from Blender.BGL import *
from math import *
from whrandom import random

#############################################################
#
#############################################################
#
resolc=Create(32)
#
cw=Create(1.5)
cl=Create(0.75)
cs=Create(1.0)
#
csinb=Create(0.0)
csinc=Create(0.0)
csind=Create(0.0)
#
che=Create(0.0)

#####

resols=Create(32)
#
sl=Create(2.0)
st=Create(0.5)
ss=Create(1.0)
#
ssinb=Create(0.0)
ssinc=Create(0.0)
ssind=Create(0.0)
#
randmc=Create(0.0)
randms=Create(0.0)
#
smooth=Create(0.0)
#
############################################################# 
# Main function                                             # 
############################################################# 
def create(): 
   global  resolc, cs, cw, cl, randmc, che
   global  resols, ss, sl, st, randms
   global  csinb, csinc, csind, ssinb, ssinc, ssind
   global smooth
#############################################################################
#  cap
########################
   mec=NMesh.GetRaw()
   i=0.0
   j=0.0
   n=resolc.val
###########################
   for i in range(0,n):
       for j in range(0,n):
           #
           fx=0.1*csinb.val*sin(i*csinc.val+csinc.val+j*csind.val+csind.val)
           #
           x=sin(j*pi*2/(n-1))*cos(-pi/2+i*pi/(n-1))*cw.val
           x+= fx +0.1*randmc.val*random()
           y=cos(j*pi*2/(n-1))*cos(-pi/2+i*pi/(n-1))*cw.val
           y+= fx +0.1*randmc.val*random()
           z=sin(-pi/2* cs.val *i*pi/(n+1))*cl.val
           #
           z=z+sl.val*ss.val+ss.val +che.val
           #
           #
           v=NMesh.Vert(x,y,z)
           mec.verts.append(v)

###############################################
   n0=len(range(0,n))
   for i in range(0,n-1):
       for j in range(0,n-1):
           #
           f=NMesh.Face()
           #
           if smooth.val: f.smooth=1
           #
           f.v.append(mec.verts[i*n0+j])
           f.v.append(mec.verts[i*n0+j+1])
           f.v.append(mec.verts[(i+1)*n0+j+1])
           f.v.append(mec.verts[(i+1)*n0+j])
           #
           mec.faces.append(f)
           #
   NMesh.PutRaw(mec,"cap",1)

#############################################################################
# stem  
#######################
   mes=NMesh.GetRaw()
   i=0.0
   j=0.0
   n=resols.val
###########################
   for i in range(0,n):
       for j in range(0,n):
           #
           fxs=0.1*ssinb.val*sin(i*ssinc.val+ssinc.val+j*ssind.val+ssind.val)
           #
           x=sin(j*pi*2/(n-1))*cos(-pi/2+i*pi/(n-1))*st.val
           x+= fxs +0.1*randms.val*random()
           y=cos(j*pi*2/(n-1))*cos(-pi/2+i*pi/(n-1))*st.val
           y+= fxs +0.1*randms.val*random()
           z=sin(-pi/2-0.8+ss.val*i*pi/(n+1))*sl.val+fxs

           #
           z=z+sl.val
           #
           v=NMesh.Vert(x,y,z)
           mes.verts.append(v)
           
########################################
   n0=len(range(0,n))
   for i in range(0,n-1):
       for j in range(0,n-1):
           #
           f=NMesh.Face()

           if smooth.val: f.smooth=1

           f.v.append(mes.verts[i*n0+j])
           f.v.append(mes.verts[i*n0+j+1])
           f.v.append(mes.verts[(i+1)*n0+j+1])
           f.v.append(mes.verts[(i+1)*n0+j])
           #
           mes.faces.append(f)


   NMesh.PutRaw(mes,"mstem",1)
   Blender.Redraw()


############################################################# 
# Gui                                                       # 
############################################################# 
def Warn(): 
   BGL.glRasterPos2d(5, 360) 
   Blender.Window.Redraw(Blender.Window.Const.TEXT) 
###########    
def draw(): 
   global  resolc, cs, cw, cl, randmc, che
   global  resols, ss, sl, st, randms
   global  csinb, csinc, csind, ssinb, ssinc, ssind
   global  smooth
####
   BGL.glClearColor(0.6, 0.6, 0.6, 1) 
   BGL.glColor3f(1,1,1) 
   BGL.glClear(BGL.GL_COLOR_BUFFER_BIT) 
##
   glColor3f(0.7, 0.0, 0.0)
   glRectf(5, 200, 215, 345)

   glColor3f(0.8, 0.8, 0.8)
   glRectf(5, 45, 215, 190)
##
   BGL.glColor3f(0.0, 0.0, 0.0)
   BGL.glRasterPos2d(5, 360) 
   Text("mushroom creator")
####
   cw=Number("cap width = ",2,10, 325, 200, 15, cw.val, 0.2, 4.0)
   cl=Number("cap length = ",2,10, 300, 200, 15, cl.val, 0.2, 4.0)
   cs=Number("cap sharpness = ",2,10, 275, 200, 15, cs.val, 0.7, 1.0)

   csinb=Number("sin=",2,10, 250, 60, 15, csinb.val, 0.0, 16.0,"sinus size")
   csinc=Number("s b=",2,80, 250, 60, 15, csinc.val, 0.0, 32.0,"sinus x freq")
   csind=Number("s c=",2,150, 250, 60, 15, csind.val, 0.0, 32.0,"sinus y freq")

   randmc=Number("randomise cap =",2,10, 230, 130, 15, randmc.val, 0.0, 2.0)

   che=Number("-Z+",2,150, 205, 60, 40, che.val, -12.0, 12.0,"cap Z offset")

   resolc=Number("Resolution cap=",2,10, 205, 130, 20, resolc.val, 8, 64)

####
   sl=Number("stem length = ",2,10, 170, 200, 15, sl.val, 0.2, 4.0)
   st=Number("stem thikness = ",2,10, 145, 200, 15, st.val, 0.1, 2.0)
   ss=Number("stem shape = ",2,10, 120, 200, 15, ss.val, 0.75, 1.5)

   ssinb=Number("sin=",2,10, 95, 60, 15, ssinb.val, 0.0, 16.0," sinus size ")
   ssinc=Number("s b = ",2,80, 95, 60, 15, ssinc.val, 0.0, 32.0," sinus x freq")
   ssind=Number("s c = ",2,150, 95, 60, 15, ssind.val, 0.0, 32.0," sinus y freq")

   randms=Number("randomise stem =",2,10, 75, 130, 15, randms.val, 0.0, 2.0)

   resols=Number("Resolution stem=",2,10, 50, 130, 20, resols.val, 8, 64)

####
   smooth=Toggle("smooth", 2, 10, 10, 60, 25, smooth.val)

   Button("Exit ", 1, 150, 10, 60, 25, "EXIT") 

####
def event(evt, val):
   if (evt== QKEY and not val):
      Exit()

def bevent(evt):
   if   (evt== 1):
      Exit() 
   elif (evt== 2):
      create()
      Draw()   

####

create()
Register(draw, event, bevent)
Blender.Redraw()

#############################

26/9/2003 updated

from:
fx=0.1csinb.valsin(icsinc.val+csind.valjcsinc.val+csind.val)
to:
fx=0.1
csinb.valsin(icsinc.val+csinc.val+j*csind.val+csind.val)

I cant solve problem with normals
you have to recalc normals (edit mode, sellect all verts, hit Ctrl-N)

Really funny! I like it.

MWA HA HA HA! just what i need for my Mario Animation… now all i need is mario, and an animation… and backgrounds… and a story… and everythign else.

Good work :smiley:

MacBlender

as any other pythonscript…
I can’t make it work…
aaaah… I hate when they don’t work… :<

_cool one, indeed, but there seems to be a problem when applying randomization - generated meshes aren’t seamless. There were some cool scripts around here lately, I’ll forget almost everything about modelling …

This is a neat script, just tried it with instant success! Too bad I can’t continue to play as I am off to work. :x

jester71:
It didn’t worked for me when i copied the script from Konqueror. When i copied it from Mozilla it worked just fine. Could be something with ASCII-encoding.

D.

Thank you all for your positive reactions…
(sorry,if you cant get the script to work.)

It would be nice if someone post a musroom image.

here is mine… just a small, cheap one :). the script is good! only thing is… the shape of the mushrooms I made was a bit unnatural. maybe you could differentiate upside from backside of the cap. cuz some mushrooms have a smooth, white side and a dark one.

http://files.blender3d.ch/temporary/mushroom.jpg

Thanks for the quick response Chimera,
i know the script is not perfect, it is actualy the first script i ever wrote
it is just a experiment. (i wrote this script in Blender 2.23)
I knew nothing about scripting or programming stuff untill a few months
ago i started to get interested in writing python scripts.
if someone can improve this script it’s oke whith me…just post it here so everyone can enjoy.

well… acctualy I only know the basics… stuff like obj.LocZ = whatever ;). but I nearly every script (except dynamica) works on my machine. which is exactly what I want from python :).

funky!

Very fun! Keep up the good work.

:smiley: I just discovered this while searching for somthing else. Cute JimmyHaze. I added to my scripts menu for nothing other then some humor after a long modeling session :slight_smile:

Copying and pasting scripts with a browser tends to hose the indentation. We run into this all the time when people post test cases in the Bug Tracker. Downloading from a link is a better way.

Don’t know anything about scripts or scripting, but when I load this script (saved it to a text file called mushroomscript.py) into the text window and try to ‘Run as script’ I get an error and it highlights ‘from math import *’

Any ideas?

Tal

Yes: Install Python 2.3 and try again. If it still doesn’t work, read How to set your PYTHONPATH 101

Good work.
I noticed it leave a seem, maybe it could be possible to link last vertice whit first one to make a full shape. IT’s just a mathat of kepping a list of the first line and attach the last line to it.